SoFunction
Updated on 2025-04-09

A brief discussion on regular expressions (Regular Expression)

1. What is a regular expression?

Simply put: Regular Expression is a language that deals with string matching;

Regular expressions describe a string matching pattern that can be used to check whether a string contains a certain substring and perform "take out" or "replace" operations on the matching substring.

2. Application of regular expressions

Regular expressions are very practical in the actual development process and can quickly solve some complex string processing problems. Below I will make some simple classifications of the application of regular expressions:

The first type: data verification
For example, if you want to verify whether a string is correct EMail, Telphone, Ip, etc., then it is very convenient to use regular expressions.

The second type: content search
For example, if you want to grab an image of a web page, you must find the <img> tag. At this time, you can use regular expressions to accurately match it.

The third type: content replacement
For example, if you want to hide the four digits in the middle of the mobile phone number and turn it into this mode, 123****4567, then using regular expressions will be very convenient.

3. What are the regular expressions

Below I will briefly introduce regular expressions:

1. Several important concepts of regular expressions
•Subexpression: In regular expressions, if the content enclosed with "()" is used, it is called "subexpression"
•Capture: The result matched by the subexpression will be placed in the buffer by the system. This process, we call it "capture".
• Backreference: We use "\n", where n is a number, which represents the reference between a buffer before, which we call "backreference"
2. Quantity qualifier
•X+ means: 1 or more
•X* means: 0 or more
•X? Indicates: 0 or 1
•X{n} represents: n
•X{n,} means: at least n
•X{n,m} means: n to m, the principle of greed will match as many as possible; if one is added later? , it is the principle of non-greed
Note: X means the character to be searched

3. Character qualifier
•\d means: match a numeric character, [0-9]
•\D means: match a non-numeric character, [^0-9]
•\w means: match word characters including underscores, [0-9a-zA-Z_]
•\W means: match any non-word character, [^0-9a-zA-Z_]
•\s means: match any whitespace characters, spaces, carriage return, tab characters
•\S means: match any non-whitespace characters
•. Representation: Match any single character
In addition, there are the following types:

Range characters: [a-z], [A-Z], [0-9], [0-9a-z], [0-9a-zA-Z]
Any character: [abcd], [1234]
Characters that are not included: [^a-z], [^0-9], [^abcd]

4. Positioner
•^ means: the beginning logo
•$ represents: ending identifier
•\b   Represents: word boundaries
•\B Represents: Non-word boundary
5. Escape symbol
•\ Used to match certain special characters
6. Select the matching character
•| Can match multiple rules
7. Special usage
•(?=): Forward pre-check: Match the string that ends with the specified content
•(?!): Negative pre-check: Match strings that do not end with the specified content
•(?:): Do not put the content of the selected match character into the buffer

4. How to use regular expressions in Javascript

There are two ways to use regular expressions in Javascript:

The first method: Use the RegExp class
The methods provided are:

•test(str): Return true/false when string matches whether there is a matching pattern.
•exec(str): Returns the string matched by the matching pattern. If there is, return the corresponding string, and no, return null;
//If there are subexpressions in the regular expression, when using the exec method

//The return is: result[0] = match result, result[1] = match result of subexpression 1...

The second method is: use the String class
The methods provided are:

•search: Returns the location where the string that matches the pattern appears. If not, return -1
•match: Returns the string matched by the matching pattern. If there is, return the array, no, return null
•replace: replaces the string matched by the matching pattern
•split: Separate the string matching pattern as a delimiter and return the array

5. How to use regular expressions in PHP

There are two functions that use regular expressions in PHP:

The first is: Perl regular expression function
The methods provided are:

•preg_grep -- Returns the array unit that matches the pattern
•preg_match_all -- perform global regular expression matching
•preg_match -- perform regular expression matching
•preg_quote -- escape regular expression characters
•preg_replace_callback -- Use callback function to perform search and replace regular expressions
•preg_replace -- Perform search and replacement of regular expressions
•preg_split -- split strings with regular expressions
The second type is: POSIX regular expression function
The methods provided are:

•ereg_replace -- replace regular expressions
•ereg -- Regular expression matching
•eregi_replace -- Replace regular expressions insensitively
•eregi -- case-insensitive regular expression matching
•split -- Use regular expressions to split strings into arrays
•spliti -- Use regular expressions to split strings into arrays insensitively
•sql_regcase -- Generate regular expressions for matches that do not distinguish between sizes

6. Summary

Regular expressions are a tool for us to implement a certain function, this tool:

1. Powerful
Different combinations of various qualifiers in regular expressions will implement different functions. Sometimes implementing a complex function requires writing a very long regular expression. How to accurately match it will test a programmer's ability.

2. Concise and convenient
Usually when we are searching for string content, we can only search for a specific string, but regular expressions can help us perform fuzzy searches, which are faster and more convenient, and we only need a regular expression string.

3. All languages ​​basically support it
Currently, mainstream languages ​​such as JAVA, PHP, Javascript, C#, C++, etc. all support regular expressions.

4. Learning is simple, application is profound
Learning regular expressions is quick and easy, but how to write efficient and precise regular expressions in actual development still requires long-term trial and accumulation.

If you want to get started quickly, please check this article:

30-minute tutorial on getting started with regular expressions