Regular expression

Regular expression

1. Regular expression single character

Single character type Description Example
Specific characters

td>

A specific character ‘1’, a
Characters in the range Single character[] Numerical characters: [0-9],[259]
Lowercase characters:[az]
Reverse characters: [^0-9 ]
any character represents any character ‘.’
[[emailprotected] tmp]# grep '1' passwd.bak #Find the line with '1'[[emailprotected] tmp]# grep'[AZ]' passwd .bak #Find all capital letters[[emailprotected] tmp]# grep'[a-zA-Z]' passwd.bak #Find all letters[[emailprotected] tmp]# grep'[,:_/] 'passwd.bak #Find special characters[[emailprotected] tmp]# grep'[^0-9]' passwd.bak #Find lines that do not include numbers [[emailprotected] tmp]# grep'\.' passwd. bak #Find. This character is escaped

2: Representation of other characters

Boundary characters: first and last characters:

^: ^root – —-Begin with root

$:false$ ——End with false

^$: represents a blank line

[[e mail protected] tmp]# grep'^root' passwd.bak[[emailprotected] tmp]# grep'false$' passwd.bak

3: Other characters of regular expression

< p>*Metacharacter: Represents ordinary characters or other characters

Metacharacters Description
\w lowercase Match any word character, including underscore ([AZ a-z0-9])
\W uppercase matches any non-word character, including underscore ([ ^AZ a-z0-9])
\b represents the separation of words
[[emailprotected] tmp]# grep'x' passwd.bak #will match x in the x[password] field and the word [[emailprotected] tmp]# grep'\bx\b' passwd.bak #put x is regarded as a word and will not match the x in the word

4: the character combination of regular expressions

type< /th>

Expression Example
String ‘root’,’1000′,’m..c’,'[ AZ] [az] ” [0-9] [0-9]’
[[email protected] tmp]# grep '1000' pas swd.bak oracle:x:1000:1000:oracle:/home/oracle:/bin/bash[[emailprotected] tmp]# grep'[AZ][az]' passwd.bak #match one uppercase next to one Lowercase-a total of 2 characters [[email protected] tmp]# grep'[0-9][0-9]' passwd.bak #match the next 2 digits-a total of 2 digits and greedy match 

5: Repeat

? : Match the preceding character or sub-expression 0 or 1 time

+: Match the preceding character or sub-expression 1 or more times

*: 0 or more times Match the preceding character or sub-expression

[[emailprotected] tmp]# grep'\(mail\)\?' passwd.bak #Note that \?,\+ is Match repetition

{n, m}: Repeat a specific number of times

? :{0,1}

+:{1,}

*:{0,}

[[emailprotected] tmp ]# grep'[0-9]\{2,3\}' passwd.bak #: Repeat 2 to 3 times, except for (), all others must be escaped\{\}

6: Any string

.*: represents any string eg: ^.* m.*c ——– represents the beginning of m and the end of c, any length

? m..c—–represents the beginning of m and the end of c, but only 4 digits in length


7: logical representation

|:’ /bin(false|true)’

Common matching patterns:

Share pictures

Leave a Comment

Your email address will not be published.