First, what is a regular expression
Simply put, the regular expression is the method of processing the string, it is the behavior unit of the string processing behavior, the regular expression through some special symbols of the auxiliary, you can easily reach the user to find, delete, replace a particular string of handlers.
A regular expression is basically a notation, and as long as the tool program supports this notation, the tool can be used as a string for regular expressions. Example: vi,grep,awk,sed
The string expression of regular expressions is divided into basic regular expressions and extended expressions according to different rigor, and extended expressions can do string processing of groups in addition to a simple set of string processing.
Second, special characters
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/7C/7B/wKioL1bRWznzbrYkAABjlDdBYdk325.png "title=" Regular expression special characters. png "alt=" Wkiol1brwznzbrykaabjlddbydk325.png "/> Third, the meta-character of the regular expression
1. Character matching of regular expressions
. match any single character
[] Any single character within the specified range such as [AB] is a or b
Common examples: [0-9],[[:d igit:]]
[A-z],[[:lower:]]
[A-z],[[:upper:]]
And so on. Other special characters
[^] any single character outside the specified range
2, number of times matching (the number of times to match the preceding characters)
* any time
For example: X*y contains: Y,xy,xxy,xxxy and so on greedy mode: to match characters as long as possible
. * matches any character of any length
\? 0 or 1 times
For example: X\?y contains: xy,y
\{m\} Match m Times
\{m,n\} match only M times, up to N times
\{m,\} at least m times
\{,n\} Up to N times
3. Position anchoring (for the occurrence of character occurrences)
^ anchor the beginning of the line
$ Anchor Line End
^$ Blank line
\< anchor Word first \> anchor ending
For example,:\<abcd\> can only match ABCD
4. Grouping
\ (\) for example: \ (ab\) *xy matches any of the AB strings, such as: Abxy XY ababxy, etc.
5. References
\1 refers to the first opening parenthesis in front and the matching contents of its corresponding closing parenthesis
\2 references the preceding second opening parenthesis and its corresponding closing parenthesis matches the content
\3 ...
For example: \ (ab\) xy\1 for Abxyab
Four, regular expression tool grep
grep Common Options
-V: Reverse, showing the line that cannot be matched by the pattern;
-O: Displays only the string that is matched to the pattern, not the entire row
-I: Case insensitive
-E: Supports extended regular expressions
-A: Not only shows the rows that are matched, but also shows the following n rows (-a followed by numbers)
-B: Shows not only the rows that are matched, but also the previous n rows
-C: Not only shows the rows that are matched, but also shows the n rows of the up and down questions
V. Examples of Practice
1. Display the line that starts with the case R in the/etc/passwd file
grep--color=auto "^[RR]"/etc/passwd or grep--color=auto-i "^[r]"/etc/passwd
2. Remove the default shell for non-bash users
grep--color=auto-v "bash$"/etc/passwd | Cut-d:-f1
3. Take out the default shell for bash, and the user ID is the largest user
grep--color=auto "bash$"/etc/passwd | Sort-n-T:-k3|tail-1|cut-d:-F1
4, display/etc/rc.d/rc.local file, start with #, followed by at least one white space character, and then at least one non-blank word lines
grep--color=auto "^#[[:space:]]\{1,\}[^[:space:]]\{1,\}"/etc/rc.d/rc.local
Linux Regular Expressions--basic article