Regular Expressions of Linux

Source: Internet
Author: User

Basic Regular Expressions

  Character Matching: 

.: Matches any single character

[]: matches any single character within the specified range

[^]: matches any single character outside the specified range

  Character:  

Reference method: [[: Space:]]

[: Space:]: white space character

[:p UNCT:]: Punctuation

[: Lower:]: lowercase letters

[: Upper:]: Uppercase

[: Alpha:]: Uppercase and lowercase letters

[:d igit:]: Number

[: Alnum:]: Represents the English uppercase and lowercase characters and numbers, that is, 0-9,a-z,a-z

[: Graph:]: represents all other keys except the space bar (SPACEBAR and TAB key)

[:p Rinit:]: Represents any character that can be printed out

[: Xdigit:]: represents 16 binary numeric types, so includes 0-9,a-f,a-f numbers and characters

[: Blank:] represents the space Bar and TAB key

[: Cntrl:]: Represents the control keys above the keyboard, that is, including Cr,lf,tab,del, etc.

[Abc],[a-z],[a-z],[a-za-z],[0-9a-za-z] Description: You can also customize some character sets

 Number of matches:

"\" is used to escape

*: Matches any of its preceding characters any time

. *: Any character of any length

\?: Match its preceding character 1 or 0 times

\{m,n\}: Matches characters preceding it at least m times up to N times

\{0,\}: Indicates any number of times

  Location anchoring:   

^word: Anchor the beginning of the line, the content after this character must appear at the beginning of the line

word$: Anchor Line end, this character before the content must appear at the end of the row

^$: Indicates a blank line

\< or \b: Anchor word, any character following it must appear as the first word

\> or \b: anchors the ending, any character following it must appear as the tail of the word

\<>\: Indicates an exact match, indicating that the letter combination is in the beginning and ending of the word, and that is a word

  Group matching:

   "\" is used as an escape

\(\)

For example: \ (ab\) *: AB as a whole, AB can appear any time

"()" not only can the matching content be grouped, but also the role of a back reference

\1: Refers to all the contents of the first opening parenthesis and its corresponding closing parenthesis

\2: Refers to all the contents of the second opening parenthesis and its corresponding closing parenthesis

\3: Refers to the third opening parenthesis and its corresponding closing parenthesis for all of the content included

Extending regular Expressions

 The following command can refer to an extended regular expression

Grep-e

Egrep

Sed-r

Awk

 Matching rules:

Word? : Matches word once or 0 times

Word+: Match word at least once

Word1| Word2: To find several strings in a way or another

For example, "Glad|good" looks for two strings "glad" or "good"

    (word): To find the contents of parentheses as a whole, in fact, the difference between the basic regular expression is that there is no transfer character

    (word) +: finds the contents of parentheses as a whole match more than once

    

Regular Expressions of Linux

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.