'Pattern'에 해당되는 글 2건

  1. 2011.08.26 Pattern Matching by CEOinIRVINE
  2. 2011.04.20 Search the repeated pattern and Show what were matched. by CEOinIRVINE

Pattern Matching

IT 2011. 8. 26. 04:45

MySQL provides standard SQL pattern matching as well as a form of pattern matching based on extended regular expressions similar to those used by Unix utilities such as vi, grep, and sed.

SQL pattern matching enables you to use “_” to match any single character and “%” to match an arbitrary number of characters (including zero characters). In MySQL, SQL patterns are case-insensitive by default. Some examples are shown here. You do not use = or <> when you use SQL patterns; use the LIKE or NOT LIKE comparison operators instead.

To find names beginning with “b”:

mysql> SELECT * FROM pet WHERE name LIKE 'b%';
+--------+--------+---------+------+------------+------------+
| name   | owner  | species | sex  | birth      | death      |
+--------+--------+---------+------+------------+------------+
| Buffy  | Harold | dog     | f    | 1989-05-13 | NULL       |
| Bowser | Diane  | dog     | m    | 1989-08-31 | 1995-07-29 |
+--------+--------+---------+------+------------+------------+

To find names ending with “fy”:

mysql> SELECT * FROM pet WHERE name LIKE '%fy';
+--------+--------+---------+------+------------+-------+
| name   | owner  | species | sex  | birth      | death |
+--------+--------+---------+------+------------+-------+
| Fluffy | Harold | cat     | f    | 1993-02-04 | NULL  |
| Buffy  | Harold | dog     | f    | 1989-05-13 | NULL  |
+--------+--------+---------+------+------------+-------+

To find names containing a “w”:

mysql> SELECT * FROM pet WHERE name LIKE '%w%';
+----------+-------+---------+------+------------+------------+
| name     | owner | species | sex  | birth      | death      |
+----------+-------+---------+------+------------+------------+
| Claws    | Gwen  | cat     | m    | 1994-03-17 | NULL       |
| Bowser   | Diane | dog     | m    | 1989-08-31 | 1995-07-29 |
| Whistler | Gwen  | bird    | NULL | 1997-12-09 | NULL       |
+----------+-------+---------+------+------------+------------+

To find names containing exactly five characters, use five instances of the “_” pattern character:

mysql> SELECT * FROM pet WHERE name LIKE '_____';
+-------+--------+---------+------+------------+-------+
| name  | owner  | species | sex  | birth      | death |
+-------+--------+---------+------+------------+-------+
| Claws | Gwen   | cat     | m    | 1994-03-17 | NULL  |
| Buffy | Harold | dog     | f    | 1989-05-13 | NULL  |
+-------+--------+---------+------+------------+-------+

The other type of pattern matching provided by MySQL uses extended regular expressions. When you test for a match for this type of pattern, use the REGEXP and NOT REGEXP operators (or RLIKE and NOT RLIKE, which are synonyms).

The following list describes some characteristics of extended regular expressions:

  • .” matches any single character.

  • A character class “[...]” matches any character within the brackets. For example, “[abc]” matches “a”, “b”, or “c”. To name a range of characters, use a dash. “[a-z]” matches any letter, whereas “[0-9]” matches any digit.

  • *” matches zero or more instances of the thing preceding it. For example, “x*” matches any number of “x” characters, “[0-9]*” matches any number of digits, and “.*” matches any number of anything.

  • A REGEXP pattern match succeeds if the pattern matches anywhere in the value being tested. (This differs from a LIKE pattern match, which succeeds only if the pattern matches the entire value.)

  • To anchor a pattern so that it must match the beginning or end of the value being tested, use “^” at the beginning or “$” at the end of the pattern.

To demonstrate how extended regular expressions work, the LIKE queries shown previously are rewritten here to use REGEXP.

To find names beginning with “b”, use “^” to match the beginning of the name:

mysql> SELECT * FROM pet WHERE name REGEXP '^b';
+--------+--------+---------+------+------------+------------+
| name   | owner  | species | sex  | birth      | death      |
+--------+--------+---------+------+------------+------------+
| Buffy  | Harold | dog     | f    | 1989-05-13 | NULL       |
| Bowser | Diane  | dog     | m    | 1989-08-31 | 1995-07-29 |
+--------+--------+---------+------+------------+------------+

If you really want to force a REGEXP comparison to be case sensitive, use the BINARY keyword to make one of the strings a binary string. This query matches only lowercase “b” at the beginning of a name:

mysql> SELECT * FROM pet WHERE name REGEXP BINARY '^b';

To find names ending with “fy”, use “$” to match the end of the name:

mysql> SELECT * FROM pet WHERE name REGEXP 'fy$';
+--------+--------+---------+------+------------+-------+
| name   | owner  | species | sex  | birth      | death |
+--------+--------+---------+------+------------+-------+
| Fluffy | Harold | cat     | f    | 1993-02-04 | NULL  |
| Buffy  | Harold | dog     | f    | 1989-05-13 | NULL  |
+--------+--------+---------+------+------------+-------+

To find names containing a “w”, use this query:

mysql> SELECT * FROM pet WHERE name REGEXP 'w';
+----------+-------+---------+------+------------+------------+
| name     | owner | species | sex  | birth      | death      |
+----------+-------+---------+------+------------+------------+
| Claws    | Gwen  | cat     | m    | 1994-03-17 | NULL       |
| Bowser   | Diane | dog     | m    | 1989-08-31 | 1995-07-29 |
| Whistler | Gwen  | bird    | NULL | 1997-12-09 | NULL       |
+----------+-------+---------+------+------------+------------+

Because a regular expression pattern matches if it occurs anywhere in the value, it is not necessary in the previous query to put a wildcard on either side of the pattern to get it to match the entire value like it would be if you used an SQL pattern.

To find names containing exactly five characters, use “^” and “$” to match the beginning and end of the name, and five instances of “.” in between:

mysql> SELECT * FROM pet WHERE name REGEXP '^.....$';
+-------+--------+---------+------+------------+-------+
| name  | owner  | species | sex  | birth      | death |
+-------+--------+---------+------+------------+-------+
| Claws | Gwen   | cat     | m    | 1994-03-17 | NULL  |
| Buffy | Harold | dog     | f    | 1989-05-13 | NULL  |
+-------+--------+---------+------+------------+-------+

You could also write the previous query using the {n} (“repeat-n-times”) operator:

mysql> SELECT * FROM pet WHERE name REGEXP '^.{5}$';
+-------+--------+---------+------+------------+-------+
| name  | owner  | species | sex  | birth      | death |
+-------+--------+---------+------+------------+-------+
| Claws | Gwen   | cat     | m    | 1994-03-17 | NULL  |
| Buffy | Harold | dog     | f    | 1989-05-13 | NULL  |
+-------+--------+---------+------+------------+-------+

Section 11.5.2, “Regular Expressions”, provides more information about the syntax for regular expressions.

'IT' 카테고리의 다른 글

using More Than one Table  (0) 2011.08.26
Counting Rows  (0) 2011.08.26
Data Calculation 12 + 1 = 13?  (0) 2011.08.26
2011-08-25 Date Calculations  (0) 2011.08.26
mysql  (0) 2011.08.25
Posted by CEOinIRVINE
l


#!/usr/bin/perl

#perl-grep3.pl

my $pattern = shift @ARGV;
my $regex=eval {qr/$pattern/};
die "Check your pattern! $@" if $@;

while( <> )
        {
        print "$_\t\tmatched >>>$&<<<\n" if m/$regex/;
        }

[root@blackhole:/tmp]$ perldoc -t perl | perl-grep3.pl "\b(\S)\S\1\b"
    support online at <http://www.perl.org/>.
                matched >>>www<<<
        perluniprops        Index of Unicode Version 5.2.0 properties in Perl
                matched >>>.2.<<<
        perl595delta        Perl changes in version 5.9.5
                matched >>>.9.<<<
        perl594delta        Perl changes in version 5.9.4
                matched >>>.9.<<<
        perl593delta        Perl changes in version 5.9.3
                matched >>>.9.<<<
        perl592delta        Perl changes in version 5.9.2
                matched >>>.9.<<<
        perl591delta        Perl changes in version 5.9.1
                matched >>>.9.<<<
        perl590delta        Perl changes in version 5.9.0
                matched >>>.9.<<<
        perl589delta        Perl changes in version 5.8.9
                matched >>>.8.<<<
        perl588delta        Perl changes in version 5.8.8
                matched >>>.8.<<<
        perl587delta        Perl changes in version 5.8.7
                matched >>>.8.<<<
        perl586delta        Perl changes in version 5.8.6
                matched >>>.8.<<<
        perl585delta        Perl changes in version 5.8.5
                matched >>>.8.<<<
        perl584delta        Perl changes in version 5.8.4
                matched >>>.8.<<<
        perl583delta        Perl changes in version 5.8.3
                matched >>>.8.<<<
        perl582delta        Perl changes in version 5.8.2
                matched >>>.8.<<<
        perl581delta        Perl changes in version 5.8.1
                matched >>>.8.<<<
        perl58delta         Perl changes in version 5.8.0
                matched >>>.8.<<<
        perl573delta        Perl changes in version 5.7.3
                matched >>>.7.<<<
        perl572delta        Perl changes in version 5.7.2
                matched >>>.7.<<<
        perl571delta        Perl changes in version 5.7.1
                matched >>>.7.<<<
        perl570delta        Perl changes in version 5.7.0
                matched >>>.7.<<<
        perl561delta        Perl changes in version 5.6.1
                matched >>>.6.<<<
     http://www.perl.org/       the Perl homepage
                matched >>>www<<<
     http://www.perl.com/       Perl articles (O'Reilly)
                matched >>>www<<<
     http://www.cpan.org/       the Comprehensive Perl Archive
                matched >>>www<<<
     http://www.pm.org/         the Perl Mongers
                matched >>>www<<<


'Perl' 카테고리의 다른 글

Search repeated pattern by PERL  (0) 2011.04.20
Simple Perl Code (Input REGEX, Search through files by that REGEX)  (0) 2011.04.20
Posted by CEOinIRVINE
l