Database Articles

  Home arrow Database Articles arrow Page 2 - Pattern Matching with Strings
DATABASE ARTICLES

Pattern Matching with Strings
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 5
    2007-09-13

    Table of Contents:
  • Pattern Matching with Strings
  • Pattern Matching with Regular Expressions continued
  • 5.12 Controlling Case Sensitivity in Pattern Matching
  • 5.13 Breaking Apart or Combining Strings
  • Breaking Apart or Combining Strings

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement

    Pattern Matching with Strings - Pattern Matching with Regular Expressions continued


    (Page 2 of 5 )

    Regular expressions do not match NULL values. This is true both for REGEXP and for NOT REGEXP:

      mysql> SELECT NULL REGEXP '.*', NULL NOT REGEXP '.*';

     

    NULL REGEXP '.*' | NULL NOT REGEXP '.*' 

     

     

     NULL NULL

     

    The fact that a regular expression matches a string if the pattern is found anywhere in the string means you must take care not to inadvertently specify a pattern that matches the empty string. If you do, it will match any non-NULL value. For example, the pattern a* matches any number of a characters, even none. If your goal is to match only strings containing nonempty sequences of a characters, use a+ instead. The + requires one or more instances of the preceding pattern element for a match.

    As with SQL pattern matches performed using LIKE, regular expression matches performed with REGEXP sometimes are equivalent to substring comparisons. The ^ and $ metacharacters serve much the same purpose as LEFT() or RIGHT(), at least if you’re looking for literal strings:

    Pattern match Substring comparison
    str REGEXP '^abc' LEFT(str,3) = 'abc'
    str REGEXP 'abc$' RIGHT(str,3) = 'abc'

    For nonliteral strings, it’s typically not possible to construct an equivalent substring comparison. For example, to match strings that begin with any nonempty sequence of digits, you can use this pattern match:

      str REGEXP '^[0-9]+'

    That is something that LEFT() cannot do (and neither can LIKE, for that matter).

    A limitation of regular expression (REGEXP) matching compared to SQL pattern (LIKE) matching is that REGEXP works only for single-byte character sets. It cannot be expected to work with multibyte character sets such as utf8 or sjis.

    More Database Articles Articles
    More By O'Reilly Media

    blog comments powered by Disqus

    DATABASE ARTICLES ARTICLES

    - Completing a Book Inventory Management System
    - Uploading Images for a Book Inventory Manage...
    - Finishing the Add Book Story for a Book Inve...
    - Integration Testing for a Book Inventory Man...
    - User Stories for a Book Inventory Management...
    - Unit Testing a Book Inventory Management Sys...
    - Testing a Book Inventory Management System
    - Implementing Models for a Book Inventory Man...
    - Book Inventory Application: Publishers and B...
    - Handling Publishers in a Book Inventory Mana...
    - Publisher Administration for Book Inventory ...
    - Book Inventory Management
    - Using the SQL Reference Manual
    - Using Oracle SQL Developer with SQL Statemen...
    - Fixing Errors with Oracle SQL Developer


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 6 - Follow our Sitemap