Introduction to Regular expressions

Source: Internet
Author: User
Tags expression engine

Regular expression definition: A collection of characters or special string patterns.

Function: Searches for text based on the pattern and displays lines of text that conform to the pattern.

Pattern (pattern): Text word wildcards regular the expression's meta-character combination to match the condition


#正则表达式就是里面有一些元字符, these characters do not represent the meaning of itself, but the meaning of a wildcard.


Note: Regular expressions work in greedy mode by default


Grep:

#文本搜索过滤工具, support for basic regular expressions

Synopsis:grep [OPTIONS] PATTERN [FILE ...]

Options:

--color=auto: Color The matching text to highlight;

-i:igmorecase, ignoring the case of characters;

-O: Displays only the string that matches to itself;

-v:--invert-match: Reverse display;

-N: Output line number

-C: Count the occurrences of the string that is matched to

-W: Exact match in characters

-E:--ENTENDED-REGEXP: Supports extended regular expression meta-characters;

-q:--quiet,--silent; silent mode, do not output any information;

-A #:after, after # line

-B #:before, front # line

-c #:context, front and back # lines




Basic regular Expression meta-characters:

Character Matching:

. : matches any single character;

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

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

[:d Igit:]

[: Alpha:]

[: Upper:]

[: Lower:]

[: Space:]

[: Alnum:]

[:p UNCT:]




Number of matches: used to limit the number of occurrences of the preceding character, after the character to specify the number of occurrences;

*: Match its preceding characters any time; 0.1, multiple times;

. *: Matches any character of any length

?: matches the preceding character 0 or 1 times, that is, the preceding character is optional;

+: Matches the preceding character one or more times, that is, the preceding character appears at least once;

{m}: matches the preceding character m times;

{M,n}: matches the characters preceding it at least m times, up to n times;




Location anchoring:

^: Anchor at the beginning of the line, for the leftmost mode;

$: End of line anchoring: for the right-most side of the pattern;

^pattern$: Using PATTERN to match the whole line;

^$: blank line;

^[[:space:]]*$: Blank lines or lines that contain white space characters

< b: The first anchor of the word, used for the leftmost side of the word;

> B: The ending anchor for the rightmost side of the word;

<pattern>: Exact match of complete words;


Grouping and referencing

(): Bind one or more characters together and treat as a whole

(XY) *ab


Note:

The contents of the pattern in the grouping brackets are automatically recorded in the internal variables by the regular expression engine, which are:

1: The pattern from the left side, the first opening parenthesis and the matching closing parenthesis, matches the character of the pattern;

2: The pattern from the left side, the second opening parenthesis and the matching closing parenthesis, matches the character of the pattern;

3: ...

...

* Back reference: Refers to the character to which the pattern in the preceding grouping brackets matches


Extending the meta-character of a regular expression

Character Matching:

.:

[]:

[^]:


Number of matches:

*: Any time

? : 0 or 1 times,

+: Its first character at least once;

{m}: the characters before the period m times;

{M,n}: At least m times, up to n times;


Location anchoring:

^: Anchor at the beginning of the line

$: End of line anchoring

<,b: The first anchor of the word

>,b: Ending anchoring

Grouping and referencing:

(): grouping; the characters that match the pattern in parentheses are recorded in the internal variables of the regular expression engine;

Back To reference: ...

Or:

A|b:

C|cat:c or Cat

(c| C) At:cat or cat


Introduction to 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.