A regular expression is a notation notation used to recognize text patterns. The main program for Linux to process regular expressions is grep. grep searches for rows that match the regular expression and transports the results to standard output.
1. grep matching mode
grep accepts options and parameters as follows (where Regex represents a regular expression)
grep [Options] regex [Files]
The options are the following table:
Options |
Meaning |
Function description |
-I. |
Ignore case |
Ignore case |
-V |
Invert match |
Does not match the matching |
-L |
File-with-match |
The output matches the file name |
-L |
File-without-match |
Output mismatched file names |
-C |
Count |
Number of output matches (number of rows) |
-N |
Number |
The output matches the row with the file name and the number of rows in the file name |
-H |
No-filename |
Suppress the output of a file name |
Examples Show
Suppose there are three files Del1, Del2, del3 three files with the contents as follows
Example
2. Special characters
Symbol |
Meaning |
Example |
^ |
Start tag |
Examples of "^ABC" satisfies ABC, ABCD |
^ |
Non (within []) |
Examples of "[^ABC]" gratification: DDD, MPD |
$ |
End tag |
Examples of "abc$" satisfies ABC, MMABC |
. |
Any character |
Examples of "A.C" satisfies ABC, FAPCC |
\< |
Match Word start |
Examples of "\<ABC" satisfies ABC, ABCD |
\> |
Match Word End |
Examples of "abc\>" satisfies ABC, PMRABC |
| |
Or |
"Aaa| BBB "satisfies the example AAA, BBBPP |
3. Scope
Symbol |
Meaning |
Example |
? |
Match the previous character 0 or 1 times |
"ABC?" Examples of satisfaction AB, MABCD |
* |
Match previous character ≥ 0 times |
Examples of "abc*" gratification abbb, ABCDK |
+ |
Match previous character ≥ 1 times |
Examples of "abc+" satisfies ABCD, ABCCCDD |
{} |
{m}, {M,n}, {m,}, {, n} match the previous character M times, M to n times, ≥m times, ≤n times, respectively |
Examples of "abc\{3,5\}" gratification ABCCCC, ABCCCCCC |
[] |
[] If not within the scope, select one; |
"M[abc]p" satisfies the example acpd;m[1-9]p satisfies the example m8pp |
() |
Place all candidate elements within (), separated by | |
"A (1|2|3) BC" Satisfied Example A1BC, MBA3BCD |
Note: {} needs to be transferred in the Zheng expression, and {} () is not required.
Note An example of the scope of the {}:
4. Standard character class
Character class |
Interpretation |
[: Alnum:] |
Letters and numbers, equivalent to [a-za-z0-9] |
[: Word:] |
[: Alnum:] Plus underline _ |
[: ALPA:] |
Letters, equivalent to [a-za-z] |
[:d Igit:] |
Number, with [0-9] equivalent |
[: Xdigit:] |
hexadecimal characters, equivalent to [0-9a-fa-f] |
[: Blank:] |
Spaces and Tabs |
[: Graph:] |
Visible characters, by extension 33~126 |
[: Lower:] |
lowercase letters |
[: Upper:] |
Capital |
[:p rint:] |
printable characters |
[: Space:] |
White space character, equivalent to [\t\r\n\v\f] |
[:p UNCT:] |
Punctuation |
[: Cntrl:] |
ASCII control code, including character 0~31 and 127 |
Example
Linux Regular Expressions