MySQL Regular Expression

Source: Internet
Author: User


A regular expression describes a set of strings. The simplest regular expression is a regular expression without any special characters. For example, the regular expression hello matches hello. Extraordinary Regular Expressions use special structures so that they can match more than one string. For example, the regular expression hello | word matches the string hello or string word. Www.2cto.com is a more complex example. The regular expression B [an] * s matches any of the following strings: Bananas, Baaaaas, Bs, and any other string that starts with B, ends with s, and contains any number of a or n characters. For the REGEXP operator, the regular expression can use any of the following special characters and structures: · ^ matches the start part of a string. Mysql> SELECT 'fo \ nfo 'regexp '^ fo $';-> 0 mysql> SELECT 'fofo 'regexp '^ fo '; -> 1 · $ match the end part of the string. Mysql> SELECT 'fo \ no' REGEXP '^ fo \ no $';-> 1 mysql> SELECT 'fo \ no' REGEXP '^ fo $';-> 0 ·. match any character (including carriage return and new line ). Mysql> SELECT 'fofofo' REGEXP '^ f. * $ ';-> 1 mysql> SELECT 'fo \ r \ nfo 'regexp' ^ f. * $ ';-> 1 · a * matches any sequence of 0 or multiple a characters. Mysql> SELECT 'Ban 'regexp' ^ Ba * n';-> 1 mysql> SELECT 'baa' regexp' ^ Ba * n '; -> 1 mysql> SELECT 'bn 'regexp '^ Ba * n';-> 1 · a + matches any sequence of one or more a characters. Mysql> SELECT 'Ban 'regexp' ^ Ba + N';-> 1 mysql> SELECT 'bn 'regexp' ^ Ba + N';-> 0 ·? Matches 0 or 1 a character. Mysql> SELECT 'bn 'regexp '^ Ba? N';-> 1 mysql> SELECT 'Ban 'regexp '^ Ba? N';-> 1 mysql> SELECT 'baan 'regexp '^ Ba? N';-> 0 · de | abc matching sequence de or abc. Mysql> SELECT 'Pi 'regexp 'Pi | apa ';-> 1 mysql> SELECT 'axe' REGEXP 'Pi | apa '; -> 0 mysql> SELECT 'apa 'regexp 'pi | apa';-> 1 mysql> SELECT 'apa 'regexp '^ (pi | apa) $ '; -> 1 mysql> SELECT 'Pi 'regexp '^ (pi | apa) $';-> 1 mysql> SELECT 'pix 'regexp '^ (pi | apa) $ '; -> 0 · (abc) * matches zero or multiple instances of the sequence abc. Mysql> SELECT 'Pi 'regexp' ^ (pi) * $ ';-> 1 mysql> SELECT 'pip 'regexp' ^ (pi) * $ '; -> 0 mysql> SELECT 'pipi' REGEXP '^ (pi) * $';-> 1 · {1}, {2, 3} {n} or {m, the n} symbol provides a more common way to write regular expressions and can match many of the aforementioned atoms (or "parts") in the pattern "). Both m and n are integers. O a * can be written as a {0 ,}. O a + can be written as a {1 ,}. O? Can be written as a {0, 1 }. More accurately, a {n} exactly matches n instances of. A {n,} matches n or more instances of. A {m, n} matches m ~ of ~ N instances, including m and n. M and n must be between 0 and ~ The range of RE_DUP_MAX (255 by default) includes 0 and RE_DUP_MAX. If both m and n are given, m must be less than or equal to n. Mysql> SELECT 'abcde' REGEXP 'a [bcd] {2} E';-> 0 mysql> SELECT 'abcde' REGEXP 'a [bcd] {3} E '; -> 1 mysql> SELECT 'abcde' REGEXP 'a [bcd] {1, 10} E';-> 1 · [a-dX], [^ a-dX] matches any character that is (or is not, if ^ is used) a, B, c, d, or X. The "-" character between two other characters constitutes a range, and matches all characters starting from 1st characters to 2nd characters. For example, [0-9] matches any decimal number. To include the text character "]", it must be followed by the brackets. To contain the text character "-", it must be written first or last. For any character that does not define any special meaning for [], it only matches itself. Mysql> SELECT 'axbc' REGEXP '[a-dXYZ]';-> 1 mysql> SELECT 'axbc' REGEXP '^ [a-dXYZ] $ '; -> 0 mysql> SELECT 'axbc' REGEXP '^ [a-dXYZ] + $ '; -> 1 mysql> SELECT 'axbc' REGEXP '^ [^ a-dXYZ] + $ '; -> 0 mysql> SELECT 'gheis 'regexp '^ [^ a-dXYZ] + $ '; -> 1 mysql> SELECT 'gheisa 'regexp '^ [^ a-dXYZ] + $';-> 0 ·[. characters.] in the brackets expression (using [and]), match the character sequence used for checking the element. The character is a single character or a new line or another character name. In the regexp/cname. h file, you can find the complete list of character names. Mysql> SELECT '~ 'Regexp' [[...] ';-> 1 mysql> select '~ 'Regexp '[[. tilde.] ';-> 1 · [= character_class =] in the brackets (using [and]), [= character_class =] indicates the same type. It matches all characters with the same value, including itself. For example, if both o and (+) are similar members, [[= o =], [[= (+) =], and [o (+)] are synonyms. And so on. · [: Character_class:] in the brackets expression (use [and]), [: character_class:] indicates the character class that matches all characters in the term class. The standard class names are: they represent the character classes defined on the ctype (3) manual page. Other class names may be provided for specific regions. Character classes cannot be used as endpoints of a range. Mysql> SELECT 'justnums' REGEXP '[[: alnum:] +';-> 1 mysql> select '!! 'Regexp' [[: alnum:] + ';-> 0 · [[: <:], [[: >:]], which indicates the word boundary. They match the start and end of word respectively. Word is a series of character characters, with no character at the front and back. The character is a letter, digit, or underscore (_) in the alnum class (_). Www.2cto.com mysql> SELECT 'A word a' REGEXP '[[: <:] word [[: >:]]'; -> 1 mysql> SELECT 'a xword a' REGEXP '[[: <:] word [[: >:]]'; -> 0 to use a special character text instance in a regular expression, add two backslash (\) characters before it. The MySQL parser is responsible for interpreting one of them, and the regular expression library is responsible for interpreting the other. For example, to match the string "1 + 2" that contains the special character "+", in the following regular expression, only the last one is correct: mysql> SELECT '1 + 2' REGEXP '1 + 2';-> 0 mysql> SELECT '1 + 2' REGEXP '1 \ + 2 '; -> 0 mysql> SELECT '1 + 2' REGEXP '1 \ + 2';-> 1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.