Bookmark now! An article to thoroughly learn regular expressions

Why do you need regular expressions?

complex processing of text

Advantages and uses of regular expressions?

  • A powerful and flexible text processing tool

  • Most programming languages, databases, text editors, and development environments support regular expressions

regular expression syntax

1. Ordinary characters

Letters, numbers, Chinese characters, underscores, and punctuation marks without special definitions are all "common characters". Ordinary characters in the expression, when matching a string, match the same character.

2. Simple escape characters

Special fields are used for their own purposes. Special characters cannot be used directly. They need to be escaped before they can be used. In the last two lines of the table, "/" needs to be added to express their own characters after escaping.

3. Standard character set

  • An expression capable of matching 'multiple characters'

  • Note that case is sensitive, uppercase means the opposite

picture

4. Custom character set

[]Square bracket matching mode, which can match any character in the square brackets

picture

  • The special symbols of regular expressions, if they are included in square brackets, lose their special meaning, except for ^, -

  • The standard character set, except for the decimal point, if it is enclosed in square brackets, the custom character set will include the set. For example: [\d.\-+] will match: numbers, decimal points, +, -

5. Quantifier

Special symbols that modify the number of matches

picture

  • Greedy mode in the number of matches (the more characters to match, the better, this mode is used by default)

  • Non-greedy mode in the number of matches (the fewer matching characters the better, add a "?" after the special symbol that modifies the number of matches)

6. Character boundaries

(This group of tags matches not characters but positions, positions that meet certain conditions)

picture

  • If ^ and $ are together, it must be an exact match.

  • \b matches such a position: the preceding character and the following character are not all \w

Regular expression matching pattern

  • IGNORECASE ignore case pattern

Case is ignored when matching.

By default, regular expressions are case sensitive.

  • SINGLELINE single line mode

The entire text is regarded as a string with only one beginning and one end.

Make the decimal point "." match any character including a newline character (\n).

  • MULTILINEMultiline mode

Each line is a string with a beginning and an end.

After specifying MULTILINE, if you need to match only the beginning and end of the string, you can use \A and \Z

7. Selectors and grouping

picture

8. Backreference (\nnn)

  • Each pair of () will be assigned a number, and the captures using () will be automatically numbered from 1 according to the order of the opening brackets.

  • By means of backreferences, references can be made to strings already captured by the group.

9. Pre-search (zero-width assertion)

  • Only sub-expression matching is performed, the matching content is not included in the final matching result, and it is zero-width

  • This location should meet a certain condition. Determine whether the characters before and after the current position meet the specified conditions, but do not match the characters before and after. is a match on position.

  • During the regular expression matching process, if the subexpression matches the character content instead of the position and is saved in the final matching result, then the subexpression is considered to be character-occupied; if the subexpression matches only is the position, or the matched content is not saved into the final matching result, then the subexpression is considered to be zero-width. Occupying characters or zero width depends on whether the matched content is saved in the final matching result.

picture

List of common regular expressions:

picture

Finally: The complete software testing video tutorial below has been sorted out and uploaded, and friends who need it can get it by themselves [Guaranteed 100% free]

Software Testing Interview Documentation

We must study to find a high-paying job. The following interview questions are the latest interview materials from first-tier Internet companies such as Ali, Tencent, and Byte, and some Byte bosses have given authoritative answers. Finish this set The interview materials believe that everyone can find a satisfactory job.

Guess you like

Origin blog.csdn.net/AI_Green/article/details/132583345