A brief analysis of regular expressions _ regular expressions

Source: Internet
Author: User

The so-called regular expression, is to use a class of metacharacters (do not represent the meaning of the wildcard or other meaning), the group

The number of other characters, can match the conditions of the characters.
Regular expressions have basic regular expressions and extended regular expressions in two broad categories,
Here are some common characters for basic regular expressions and extended regular expressions.

Basic Regular Expressions:

^ Qualifying content for anchoring the beginning of a line
$ qualifying Content at Anchor Line end
^$ represents a blank line
. Match any single character
* Match the characters in front of them any time
. * Match any character of any length
[] matches any character in the specified range
[^] matches any character outside the specified range
\? Match the word immediately before it 0 or 1 times
\{m,n\} matches its preceding characters at least m times, at most n times
\{0,n\} matches the previous character 0-n times
\{m,\} matches its preceding character at least m times
\{m\} exact Match m times
\< Anchor Word First
\> anchor suffix
\ (\) grouping

Common options for grep

--color=auto automatically adds color to matching characters
-V reverse selection, showing only rows that do not conform to the pattern
-O displays only strings that are matched by patterns, not entire rows
-I case-insensitive character case
-a n displays the rows that match to, and the following n lines are displayed
B-N shows the front n rows
-C n shows the following n rows
-e using extended regular Expressions grep-e = Egrep

Extended Regular Expressions (here to list differences)

  ?             there were 0 or 1 regular expressions for \?
   {m,n}         matches the front character at least m times, up to N times
   ()              grouping, usage and grep similar to
   a|b            two Select a

1, anchoring the line at the beginning of the/etc/passwd file as root
     grep  "^root"/etc/passwd
2, anchoring/etc/ passwd the line with SH at the end of the line
     grep "sh$"/etc/passwd
3, find a blank line
     grep "^$" /ETC/PASSWD
4, matches the line with any single character followed by a
     grep "A."/etc/passwd
5, matching a line followed by a character with any length in a
      grep "A *"/etc/passwd
4, matches any character followed by a with any length, followed by the rows of B
     grep "A.*b"/etc/ passwd
5, matching a line followed by a number after any one, followed by any of the letters
     grep "a[0-9][a-za-z]"/etc/passwd
6, match a followed by any number or letter of the line
     grep "a[0-9a-za-z]"/etc/passwd
7, matching 0 or 1 a followed by a B line
      grep "A\?b"/etc/passwd
8, matching at least one a, up to 3 a followed by a B line
     grep "A\{1,3\}b" /ETC/PASSWD
9, anchor the word admin line
     grep "\<admin\>"/etc/passwd
10, match the ad from one less occurrence, Up to 3 times ad rows
     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.