Regular expressions (matches under the grep command)

Source: Internet
Author: User


Basic Regular expression: REGular expression (REGEXP)


One, meta-character:

    1. . : matches any single character;

    2. []: matches any single character within the specified range;

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

PS : Common character sets

[:d igit:]--Digital Collection

[: lower:]--Small Letter Set

[: upper:]--Caps Collection

[: space:]--Space

[: alpha:]--All letters set

[: alnum:]--All letters and numbers set



Second, the number of matches:

    1. *: Match the characters in front of it any time;

    2. . *: Matches any character of any length;

    3. \? : matches the characters in front of it at least once;

    4. \{m,n\}: Matches its preceding character at least m times, up to N times.

PS : The regular expression defaults to working in greedy mode, which matches the maximum length string for an expression.


Three, the position anchor nails:

    1. ^: Anchor at the beginning of the line, the content after this character must appear at the top;

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

    3. ^$: Blank line (the line at which the anchor is just beginning to end);

    4. \< or \b: Anchor word, any character behind it must appear as the first word (non-English word);

    5. \> or \b: anchor ending, any character in front of which must appear as the tail of a word (non-English word).

      For example,:\<root\> matches the word root.


Four, group:

    1. \ (\): Matches the grouped content, matching the contents of the parentheses as a whole.

      For example: \ (ab\) * means matching "ab" any time.

    2. A back reference (someone also referred to as a forward reference): Used with grouping.

      \1--refers to the first opening parenthesis and all content contained in the corresponding closing parenthesis (whether or not nested in parentheses)

      \2--refers to the second opening parenthesis and all the contents of the corresponding closing parenthesis (IBID.)

      \3 —— ... (An analogy) ...

      For example, the following three lines of content:

      He like his liker.

      She Love her lover.

      He like her lover.

      Expression: L.. E can match each line;

      Expression: \ (L.. e\). *\1 can only match the first two lines.




Extended Regular expression: (differs from basic regular expression)


    1. ? : matches the number of characters in front of it at most once (note: there is no \ in front of it);

    2. +: Matches the number of characters before it at least once (note: The basic regular expression does not have this metacharacters);

    3. {M,n}: Number of times match, usage with basic regular expression (no need to use \ in front of curly braces);

    4. (): Grouping, the real grouping (no need to use \), also support the back reference;

    5. | Or For example: A|b matches A or b,c|cat matches C or cat (matches the entire left or right).



Over.


This article is from the "stupid Bird Learn to fly" blog, please be sure to keep this source http://yshun.blog.51cto.com/9570549/1718837

Regular expressions (matches under the grep command)

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.