Beginner Regular Expressions

Source: Internet
Author: User

What is a regular expression:

The regular expression is the filtering of the finished data, rejecting the data that is not satisfied with the regular expression definition, leaving the data that matches the regular expression.

Metacharacters (metacharacters): A meta-character is a character used to illustrate the meaning of a character expression, which is the meaning of a character's expression.

The metacharacters of a regular expression is divided into basic regular expression metacharacters and extended expression meta-characters.


Basic expression meta-characters:

Character Match Meta character:

.: matches any single character. Equivalent to a shell file name wildcard character? No.

Example: R.. Any 2 characters between T root Rabt R and T can be


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

Example: 1[23]3

123 133 [] inner faces 2 and 3 can all match


[^]: Take counter

Example: 1[23]3

143 153 Not [] 2 and 3 of the inner faces can be


Number of match meta characters:

*: Any length, the characters in front of it can appear any time. Wildcard with Shell filename * denotes any character of any length

Example: A*b

Aaab b * Number A can occur any number of times including 0 times


\?: 0 or 1 times, the characters in front of it are optional

Example: A\?b

AB b \ is escaped compliant, \? Front a can occur at most once, or it may not


\+: 1 or more times

Example: A\+b

AB AaB A must appear 1 times here, up to No limit


\{m\}: M times, it before the character to appear m times

Example: 1\{2\}3

113 1 to appear 2 times


\{m,n\}: At least m times, up to N times

Example: 1\{2,5\}3

113,1113,11113 Here 1 appears at least 2 times, up to 5 times

\{m,\}: At least m times

Example: 1\ (2,\) 3

113,111113 here 1 min 2 times, no Limit

\{0,n\}: Up to n times

Example: 1\ (0,3\) 3

3,1113, here 1 min 0 times, but up to 3 times


Location anchoring:

^: Anchor at the beginning of the line;

Example: ^a

AVBFF big A must be at the beginning of the current


$: End of line anchoring:

Example: a$

ABCSA large A must appear at the end of the row

^$: Blank Line

^ whitespace character is the beginning of the line, and the $ white space character is the end of lines, that is blank

Word: A string consisting of consecutive characters that do not contain special characters

\<: The first word, appearing on the left side of the word,

Example: \<root

ROOTTT root must appear on the left side of the word, can match to ROOTTT, cannot match rrroot


\>: Ending, appearing on the right side of the word,

Example:root\>

Rrroot root must appear on the right side of the word to match rrroot and not match roottt


\b: Write with \<,\> in front of the word to match the first word, write after the word matches the ending

Example: \broot

ROOTTT root must appear on the left side of the word, can match roottt, and cannot match rrroot


Example: root\b

Rrroot root must appear on the right side of the word to match rrroot and not match Roott


\<char\>: precise anchoring of words

Example:\<root\>

Root root appears in front of the word also appears behind the word, that is, accurate targeting position


Group:

\ (\): Grouping

Example: \ (12\) 1

121 12 must appear at the same time


\ n: Reference
Example: \ (12\) 3\1

12312 \1 means to print \ (12\) match mode content in \1 position

Note: The parentheses of the grouping can be nested using such as \ (12\ (23\) \), where the reference position starts from the first opening parenthesis to \1, the second left parenthesis is \2, and so on.


To extend an expression:

Basically the same as the basic expression, with different points without \ escaping.


More than the basic expression has the | meta-character:

|: OR

Example: cat| Cat

Cat, or Cat. Easy to appear t| C-like errors


Usage of regular Expressions:

grep [option]: ' PATTERN ' FILE ...

Option options:

-V: Reverse Selection

-O: Displays only the matched string, not the line where the string is located

-i:ignore-case, ignoring character case

-E: Supports the use of extended regular expressions

-A #: Displays the following lines that match to the line

-B #: Displays the first few lines matching to the line

-C #: Displays the front and back lines matching to the line


Example: Grep-o "^root"/etc/passwd


Here are examples of regular expressions commonly used, others need to know more about the manuals or materials. If there is any improper, please the great God guidance.



This article is from the "Linux" blog, so be sure to keep this source http://4269478.blog.51cto.com/4259478/1650428

Beginner 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.