SoFunction
Updated on 2025-04-06

A brief analysis of regular expressions

The so-called regular expression is to use a class of metacharacters (which does not represent its own meaning, but rather a unified or other meaning), group

Compatible with other characters that can match the characters that meet the conditions.
Regular expressions have two categories: basic regular expressions and extended regular expressions.
Let me introduce some common characters for basic regular expressions and extended regular expressions.

Basic regular expressions:

^                                                                                                                              �
$
^$
Match any single character
*         Match any time the characters are in front of it
.*
[]        Match any character in the specified range
[^]      Match any character outside the specified range
\?        Match the word immediately before it 0 or 1
\{m,n\}  Matches at least m times, up to n times
\{0,n\}  Matches the characters before it 0-n times
\{m,\}   Match at least m times
\{m\}    Exact match m times
\<
\>         Anchor word end
\(\)     Grouping

Common options for grep

--color=auto   Automatically add color to matching characters
-v                                                              �
-o                                                              �
-i                                                                                                                              �
-A  n           When displaying the matching rows, display the n lines following them.
-B  n
-C  n
-E                                                                                                                              �

Extended regular expressions (here to list the differences)

? 0 or 1 time appears. The regular expression is \?
{m,n}                                                            �
()                                                                                                                              �
a|b

1. The line in the anchor /etc/passwd file with the first line is root
     grep  “^root” /etc/passwd
2. Anchor the line with the end of the line of the /etc/passwd file is sh
     grep “sh$” /etc/passwd
3. Find blank lines
     grep “^$” /etc/passwd
4. Match a line followed by any single character
     grep “a.” /etc/passwd
5. Match a line with a character of any length after it
     grep “a*” /etc/passwd
4. Match a and any character of any length followed by a, and then follow the line b
     grep “a.*b” /etc/passwd
5. Match a line with any number followed by any letter.
     grep “a[0-9][a-zA-Z]” /etc/passwd
6. Match a line with any number or letter followed by a
     grep “a[0-9a-zA-Z]” /etc/passwd
7. Match 0 or 1 a row followed by b
     grep “a\?b” /etc/passwd
8. Match at least one a, at most three a rows followed by a b
     grep “a\{1,3\}b” /etc/passwd
9. The line of anchoring the word admin
     grep “\<admin\>” /etc/passwd
10. The line that matches the number of ads will appear once, and the number of ads will appear at most 3 times.
     grep “\(ab\)\{1,3\}” /etc/passwd