Regular Expression Analysis

Source: Internet
Author: User

The so-called regular expression is to use a type of metacharacters (not to represent its own meaning, but to represent the unified or other meanings ),

If other characters are encoded, they can match the matching characters.
Regular Expressions include basic regular expressions and extended regular expressions,
The following describes some common characters of basic regular expressions and extended regular expressions,

Basic regular expression:

^ Anchor specifies the content that meets the conditions at the beginning of the line
$ Anchor specifies the content at the end of the line that meets the conditions
^ $ Indicates blank rows
. Match any single character
* Match any character before it
. * Match any character of any length
[] Match any character in the specified range
[^] Match any character out of the specified range
\? Match the word that is next to it 0 times or 1 time
\ {M, n \} matches the character above it at least m times, at most n times
\ {0, n \} matches the first character 0-n times
\ {M, \} matches the character above it at least m times
\ {M \} exact match m times
\ <Start of anchor
\> Pin the end of a word
\ (\) Group

Common grep options

-- Color = auto automatically adds color to matching characters
-V reverse selection, only show rows that do not conform to the mode
-O only displays strings that are matched by the pattern, rather than the entire row.
-I is case insensitive.
-When A n is used to display the matched rows, the n rows following it are displayed.
-B n: The first n rows are displayed.
-C n: display the n rows following
-E: use the extended regular expression grep-E = egrep

Extended Regular Expressions (here we will list the differences)

? The regular expression 0 or 1 occurrence is \?
{M, n} matches the front and side characters at least m times and at most n times
() Grouping, similar to grep
A | B (either)

1. Pin the line whose first row is root in the/etc/passwd file.
Grep "^ root"/etc/passwd
2. Pin the lines at the end of the/etc/passwd file to sh.
Grep "sh $"/etc/passwd
3. Search for blank lines
Grep "^ $"/etc/passwd
4. Match the line after a with any single character
Grep "a."/etc/passwd
5. Match the line with any length characters after
Grep "a *"/etc/passwd
4. Match a with any character of any length, and then keep up with B.
Grep "a. * B"/etc/passwd
5. Match a with any number followed by any letter.
Grep "a [0-9] [a-zA-Z]"/etc/passwd
6. Match the row after a with any number or letter
Grep "a [0-9a-zA-Z]"/etc/passwd
7. Match 0 or 1 a followed by B.
Grep "\? B "/etc/passwd
8. Match at least one a, and at most three a rows followed by one B.
Grep "a \ {1, 3 \} B"/etc/passwd
9. Pin the line of the word admin.
Grep "\ <admin \>"/etc/passwd
10. Match the rows that appear once less than once and appear 3 times at most.
Grep "\ (AB \) \ {1, 3 \}"/etc/passwd

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.