Database Articles

  Home arrow Database Articles arrow Page 3 - 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 - 5.12 Controlling Case Sensitivity in Pattern Matching


    (Page 3 of 5 )

    Problem

    A pattern match is case-sensitive when you don’t want it to be, or vice versa.

    Solution

    Alter the case sensitivity of the strings.

    Discussion

    The case sensitivity of a pattern match operation is like that of a string comparison. That is, it depends on whether the operands are binary or nonbinary strings, and for nonbinary strings, it depends on their collation. See Recipe 5.9 for discussion of how these factors apply to comparisons.

    The default character set and collation are latin1 and latin1_swedish_ci, so pattern match operations are not case-sensitive by default:

      mysql> SELECT 'a' LIKE 'A', 'a' REGEXP 'A';
     
    +--------------+----------------+
      | 'a' LIKE 'A' | 'a' REGEXP 'A' |
      +--------------+----------------+
      |            1 |              1 |
      +--------------+----------------+

    Note that a REGEXP operation that is not case-sensitive can lead to some unintuitive results:

      mysql> SELECT 'a' REGEXP '[[:lower:]]', 'a' REGEXP '[[:upper:]]';

     

     'a' REGEXP '[[:lower:]]' 'a' REGEXP '[[:upper:]]'

     

     1  1

     

    Both expressions are true because [:lower:] and [:upper:] are equivalent when case sensitivity doesn’t matter.

    If a pattern match uses different case-sensitive behavior from what you want, control it the same way as for string comparisons: convert the strings to binary or nonbinary as necessary or change the collation of nonbinary strings.

    To make a pattern match case-sensitive, use a case-sensitive collation for either operand. For example, with the latin1 character set, use a collation of latin1_general_cs:

      mysql> SET @s = 'a' COLLATE latin1_general_cs;
     
    mysql> SELECT @s LIKE 'A', @s REGEXP 'A';
      +-------------+---------------+
      | @s LIKE 'A' | @s REGEXP 'A' |
      +-------------+---------------+
      |           0 |             0 |
      +-------------+---------------+

    Use of a case-sensitive collation also has the effect of causing [:lower:] and [:upper:] in regular expressions to match only lowercase and uppercase characters, respectively. The second expression in the following statement yields a result that really is true only for uppercase letters:

      mysql> SET @s = 'a', @s_cs = 'a' COLLATE latin1_general_cs;
     
    mysql>
    SELECT @s REGEXP '[[:upper:]]', @s_cs REGEXP '[[:upper:]]';

     

     @s REGEXP '[[:upper:]]'  @s_cs REGEXP '[[:upper:]]' 

     

     1  0

     

     

    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 11 - Follow our Sitemap