A regular expression is a pattern that describes a set of strings, and there are many character sets that consist of and metacharacters characters of ordinary words. The primary function of regular expressions is to perform text searches and string processing, and a regular expression matches a character or string.
The character set type of the regular expression:
Character Set name |
Meaning |
[: Alnum:] |
Match alphabetic and numeric characters; equal to a-z,a-z,0-9 |
[:d Igit:] |
Match number 0-9 |
[: Alpha:] |
Match all letters with A-Z,A-Z |
[: Lower:] |
Match all lowercase letters A-Z |
[: Upper:] |
Match all uppercase letters A-Z |
[: Space:] |
Match white space characters |
The metacharacters of the basic regular expression:
Meta character type |
Meaning
|
Example |
. |
Match any character except for a line break |
112. Match 112+ at least one word such as 1122, 112d, but does not match 112
|
[] |
Match a single character in any range |
[ABC] will match any of the characters a, B, C; [A-z] will match any of the lowercase letters; |
[^] |
Match a single character outside any range |
[^ABC] matches any character except A, B, C |
* |
Matches a string or regular expression that precedes it, including 0 times at any time |
such as 112* will match 112+ multiple or 0 2, such as 112, 1122, 1122222, etc. |
\? |
Matches 0 or more times with the preceding string, and its preceding character is optional |
Like 112\? Will match 11, 11211 |
\{m\} |
Specifically match the characters in front of it to M times |
he\{3\} match Heee
|
\{m,n\} |
Match the characters in front of it to be between m times and N times |
he\{0,3\} matches Heee, Hee, he, h |
\{m,\} |
Match the characters in front of it at least n times |
He\{3,\} matches heee, HEEEEE, etc. |
\{0,m\} |
Match its preceding characters up to n times |
He\{0,1\} matches H, he |
.* |
Any character of any length |
A.*b matches all strings ending with a beginning B |
^ |
Line line matches, the specified character must appear at the beginning of the line |
^root matches only rows that start with root |
$ |
End-of-line match, specified character must appear at end of row |
/bash$ matches lines ending with "/bash" |
^$ |
Blank Line |
|
\< \b |
The first anchor of the word, written at the left end of the word |
\<abc |
\> \b |
Final anchor, written at the right end of the word |
Abc\> |
\(\) |
Grouping, the contents of a pattern in a grouping, which can be remembered in memory by the regular expression engine and then referenced |
\ (ab\) * |
\#
|
Reference that represents a # reference to the content in a group |
|
To extend the regular expression:
. [] [^] |
The meaning is the same as the basic regular expression |
* ? {m} {m,} {M,n} {0,m} |
As with the basic regular expression, just? and {} do not need \ for symbol escaping |
+ |
Match the preceding character at least once
|
^ $ \< \b \> |
The same as the basic regular expression, the same as the beginning, end of line, the first and final anchor |
()
|
Indicates the meaning of grouping |
| |
Or to indicate a selection relationship
|
^$ |
Represents a blank line, equivalent to ^[[:space:]]*$ |
Common Search Commands: grep, Egrep
Command Common parameters:
-V |
Reverse match, showing rows that are not matched to |
-O |
Displays only the matched string, not the line where the string is located |
-I. |
Ignore character case |
-E |
Supports the use of an extended regular expression |
-a# |
Displays the specified number of rows following the matching line |
-b# |
Displays the previous specified number of rows in the matching row |
-c# |
Show matching rows before and after specifying the same number of rows
|
Instance:
1, display the root account information in the/etc/passwd, you can see the function of the ^ symbol
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/5D/FC/wKiom1UogAezf4O2AACZGGsrvlg624.jpg "title=" Capture 001. PNG "alt=" Wkiom1uogaezf4o2aaczggsrvlg624.jpg "/>
Regular Expressions of Linux