Match data with regular expressions

Introduction to regular expressions

The front allows data to be found using matches, comparisons, and wildcards. However, as the complexity of the filtering conditions increases, the previous filtering methods have failed. This requires the use of more powerful matching methods-regular expressions

Regular expressions are used to match special strings (character sets). For example, if you want to extract phone numbers from a text, you can use regular expressions.

All kinds of programming languages, text editors, and operating systems support regular expressions. It is widely used. Like any language, regular expressions also have grammar

 

Use MySQL regular expressions 

As mentioned earlier, regular expressions are used to match text. Compare a regular expression with a text string. MySQL provides preliminary support for regular expressions with the WHERE clause, allowing you to specify regular expressions to filter the data retrieved by the SELECT. Of course, MySQL only supports a subset of regular expressions, and regular expressions actually have many other functions.

 

Use of regular expressions

  • Basic character matching (retrieve all lines in the column age containing text 20)
SELECT * FROM user WHERE age REGEXP '20'

 

  •  Match any character (. Is a special character in a regular expression, it means match any character)
SELECT * FROM user WHERE name REGEXP '.飞'

 

  • OR matching (using regular expression special characters | to search for one of the two strings, or this string, or another string)
SELECT * FROM user WHERE age REGEXP '20|30'

 

  •  Match one of several strings ([123] defines a set of characters, which means match 1 or 2 or 3, which can also be understood as [1 | 2 | 3])
  • [] Is another form of OR
SELECT * FROM user WHERE name REGEXP '[123]小'

 

  •  Matching range (to simplify [123456789] this type of collection, you can use-to define a range)
  • [1-9], also can be a letter range [a-z]
SELECT * FROM user WHERE name REGEXP '[1-3]小'

  

  •  Match special characters (regular expression language consists of special characters, if you need to match these characters, you need to use the escape character \\)
  • Most regular expressions use slash \ escape special characters, but MySQL requires two
SELECT * FROM user WHERE name REGEXP '\\.'

  

 

  •  Match character class
  • For convenience, you can use a predefined character set (also known as character class), as shown below

    

 

  •  Match multiple instances
  • So far, all matching characters have appeared in a single occurrence. If you want to control the number of matches, you need to use repeated metacharacters
Metacharacters         Explanation
* 0 or more matches
+ 1 or more matches (equal to {1,})
0 or 1 match (equal to {0, 1})
{n} A specified number of matches
{n,0} Not less than the specified number of matches
{n,m} Matching number range (m does not exceed 255)

 

SELECT * FROM user WHERE name REGEXP 'ticks?'

 

  •  Locator (so far all matching a string is text at an arbitrary position, in order to match the text at a specific position, you need to use a locator)
  • For example, you want to find out everything that starts with a number or decimal point

 

Metacharacters Explanation
^ Start of text
$ End of text
[[:<:]] The beginning of the word
[[:>:]] End of word

         

SELECT * FROM user WHERE name REGEXP '^[0-9\\.]'

 

 

Published 138 original articles · praised 34 · 150,000 views

Guess you like

Origin blog.csdn.net/bbj12345678/article/details/105472944