Linux Regular expressions

Source: Internet
Author: User
Tags alphabetic character file url posix

The composition of the regular expression:

    1. General characters: characters with no special meaning
    2. Special characters (meta characters): metacharacters, which have special meanings in regular expressions

Common meta-characters in regular expressions

    1. POSIX Bre with both meta characters in ere
      1. \ usually used to turn on or off special meanings of subsequent characters, such as \ (... \) with \{...\};
      2. . Matches any single character (except null);
      3. * Match any number or no single character before it, for example:. Represents any one character, then. * matches any length of any character;
      4. ^ matches the regular expression immediately thereafter, the Bre only has a special meaning at the beginning of the regular expression, and ere in any position has a special meaning;
      5. $ matches the preceding regular expression, either at the string or at the end. The BRE has a special meaning only at the end of the regular expression, and Ere has a special meaning in any position;
      6. [] matches any one of the characters in the square brackets, where the hyphen (-) is the range of consecutive characters, and the ^ symbol appears in the first position of the square brackets, indicating that the match is not any character in the list;
    2. Only characters in POSIX Bre
      1. \{n,m\}: An interval expression that matches the number of occurrences of a single character before it. \{n\} refers to reproducing n times; \{n,m\} refers to reproducing N to M times;
      2. \ (\): Reserve space to store up to 9 independent sub-patterns in a single pattern. such as \ (ab\). *\1: Refers to two occurrences of a matched AB combination, with any number of characters in the middle.
      3. \ n: Repeats the pattern of the nth sub-pattern in the \ (\) parentheses;
    3. POSIX ere there are characters
      1. {n,m} is the same as in the BRE
      2. + one or more extensions that match the preceding regular expression
      3. ? Match 0 or one extension of the preceding regular expression
      4. | Match | Regular expression before or after a symbol
      5. () match () a regular expression group enclosed
    4. Character
      The identity character set has the following types:

[::alnum]: Span style= "font-family: Arial" > numeric characters

[:d igit:]: Span style= "font-family: Arial" > numeric characters

[:p UNCT:]: Span style= "font-family: Arial" > punctuation character

[:alpha:]: Span style= "font-family: Arial" > alphabetic character

[:graph:]: Span style= "font-family: Arial" > non-whitespace character

 [:space:]: space character

[:blank:]: Span style= "font-family: Arial" > spaces and positioning characters

[:lower:]: Span style= "font-family: Arial" > lowercase alphabetic characters

[:upper:]: Span style= "font-family: Arial" > capital letter character

[:cntrl:]: Span style= "font-family: Arial" > control character

[:p rint:]: Span style= "font-family: Arial" > The characters that can be displayed

[:xdigit:]:

[:: Alnum]: Numeric characters

[:d igit:]: Numeric characters

[:p UNCT:]: Punctuation characters

Case One

The contents of the original file Url.txt are as follows:

Www.baidu.com

Http://www.baidu.com

Https://www.baidu.com

Http://wwwbaiducom

Baidu.com

Baidu

Requirement: Match begins with http or HTTPS, followed by: and contains a string of.
Bre match:

Grep ' ^https\{0,1\}:.*\. * ' Url.txt

Ere match:
Grep–e ' ^https?:.*\. * ' Url.txt

Case Two

Email match

The contents of the original file Email.txt are as follows:

[Email protected]

[Email protected]

[Email protected]

[Email protected]

@ @baidu. com

Requirement: A match begins with a letter or a number, or an underscore, followed by an @ followed by multiple alphanumeric or underlined, with one.

Grep ' ^[[:alpha:][:d igit:]_]*@[[:alpha:][:d igit:]]*\. * ' Email.txt

Note: This article refers to http://www.jb51.net/article/42989.htm

Linux Regular expressions

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.