Regular Expression learning Summary
What is a regular expression?
Regular Expression, Also knownRegular Representation,General notation(English: Regular Expression, often abbreviated as RegEx, Regexp or Re in code), a concept of computer science. Regular Expressions use a single string to describe and match a series of strings that conform to a certain syntax rule. In many text editors, regular expressions are usually used to retrieve and replace texts that match a certain pattern.
I. metacharacters of Regular Expressions
(1) character matching:
. --> Indicates matching any single character
[] --> Match any single character in the specified range
[^]: Reverse
(2) times matching metacharacters: the number of times that a character can appear before the first brake.
* --> Any length. The first character can appear at any time.
\? --> Once or 0, the first character can be none
\ {M \} --> m times, indicating that the previous character must appear m times
\ {M, N \} --> at least m times, at most N times Note: m at least 0 cannot be omitted, n no upper limit can be omitted
. * Reference of any character of any length with \ 1
(3) Positioning
^ First line anchor $ last line anchor ^ $ blank line
A string consisting of consecutive characters that do not contain special subcharacters:
\ <Beginning of a word \> can be replaced by \ B
(4) group :\(\)
The brackets are numbered: \ n references the content matching the nth brackets, not the pattern itself.
Ii. Common grep options
-V: Reverse Selection
-O: indicates that the string to be matched by the mode is not a whole line.
-I: ignore-Case, Case Insensitive
-E: regular extension expressions are supported.
-A: the first few rows matching the content
-B: displays the last few lines matching the content.
-C: displays the rows before and after matching the content.
3. Use of various options and Regular Expressions
Grep [Option]... 'pattren' file -- color = auto
1. grep-V "#"/etc/fstab shows that rows without "#" are reversed with-v.
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/3C/14/wKioL1PAxHSB1B43AADLzaRw13k451.jpg "Title =" 1.jpg" alt = "wkiol1paxhsb1b43aadlzarw13k451.jpg"/>
2. grep-o "\ <user1 \>"/etc/passwd user1 is anched By the beginning and end of the word-O indicates that the entire line is not displayed
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/3C/14/wKioL1PAxIXxfNlOAABz7WAwaBk424.jpg "Title =" 2.jpg" alt = "wkiol1paxixxfnloaabz7wawabkw.jpg"/>
3. grep-A2 "^ Zhengzhou \>"/etc/passwd Zhengzhou is anchored at the beginning of the line-A2 to display the following two rows
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/3C/15/wKiom1PAxN6iUWK7AADWnx8vVvw904.jpg "Title =" i1ebru_a ~ Piv2017063853366u7m9.jpg "alt =" wkiom1paxn6iuwk7aadwnx8vvvw904.jpg "/>
4. grep-B2 "^ Zhengzhou \>"/etc/passwd-B2 indicates that the last two rows are displayed.
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/3C/15/wKioL1PAxg2iaT1gAADlbXfT8Po997.jpg "Title =" ow.ln'gncqsbfrx5ljkfw.w.' B .jpg "alt =" wkiol1paxg2iat1gaadlbxft8po997.jpg "/>
5. grep-C2 "^ Zhengzhou \>"/etc/passwd-B2 indicates that the upper and lower rows are displayed.
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/3C/15/wKiom1PAxp-i9OzGAAEsShm_mCY913.jpg "Title =" 'W @ bt2jx (~ Kfew.c6mm@xhe6.jpg "alt =" wKiom1PAxp-i9OzGAAEsShm_mCY913.jpg "/>
6. grep "Tuser *\(. \) \ 3 "/etc/passwd R * indicates that R can appear any times \(. \) indicates ". "group \ 3 indicates that the group has been referenced three times. 650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/3C/15/wKiom1PAxtrT-6RtAAHRVHzU2RA068.jpg "Title =" zin9o85s4 % sgsx {['~ Tda1g0jpg "alt =" wKiom1PAxtrT-6RtAAHRVHzU2RA068.jpg "/>
7. grep "\ (user \). * \ 1"/etc/passwd. * indicates any character Length
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/3C/15/wKioL1PAxv2Dxsn4AAHLKFbm-7Y537.jpg "Title =" kkg3%k0hdf%0s1s1z1l4ewg.jpg "alt =" wKioL1PAxv2Dxsn4AAHLKFbm-7Y537.jpg "/>
8. grep "B \ {2, 4 \}"/etc/fstab B \ {2, 4 \} indicates that B appears at least 2 times and at most 4 times
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/3C/15/wKiom1PAxzyyPAjCAABtKDfdQDM995.jpg "Title =" '}' kl5d _ p ~ Pho7'okpoliceooic.jpg "alt =" wkiom1paxzyypajcaabtkdfdqdm995.jpg "/>
Iv. grep extension egrep (grep-E)
-GrepNot Supported ?And +These two types Modifier,However EgrepYou can.
? :Match the first character0Or1Times;
+:Match the character at least1Times
|: Represents or links,AB | BCBIndicatesABOrBCB
():Combine part of the content into a unit group. For exampleA(B | BC)B,()The advantage is that it can be used by group+? *.
Grep "Tuser | ^ Z"/etc/passwdSearch for includeTuserOr useZLine starting
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/3C/15/wKiom1PAx0nhSmhRAAHPM9IN1YE295.jpg "Title =" q56 ~ Dhv@2shq(o[ygvq289r.jpg "alt =" wkiom1pax0nhsmhraahpm9in1ye295.jpg "/>
I personally think that grep-esubstitution can be used to use egrep. It can not only be used to extend the function of egrep, but also reduce the probability of errors.
If any error occurs in this article, please correct me. Thank you!
This article from the "rookie road forward" blog, please be sure to keep this source http://8381039.blog.51cto.com/8371039/1437341