Learning Experience of Regular Expressions

Source: Internet
Author: User
1. metacharacters supported by basic Regular Expressions
Metacharacters Description Example
. Match any character. A., x. y, X...,. AB.
[] Matches any character in the specified range. [ABC], [0-9], [A-Z], [A-Z], [[: Alpha:], [[digit:], [[: alnum:], [[space:], [[: Upper:], [[: lower:], [[: punct:]
[^] Matches any character other than the specified range, that is, the inverse. [^ 0-9], [^ [: Space:], [^ [: punct:]
* Match any times before the character. AB *,. *, * AB, XY * z, [[: Space:] * AB, XY [^ [: punct:] *
? Match the first character 0 or 1 time. Use the Escape Character '\? ' AB ?, [[: Space:]? AB, XY [^ [: punct:]?
{} Match the first character to a specified interval. Escape characters must be used, I .e '\{\}' AB \ {3 \}, AB \ {1, \}, AB \ {0, 3 \}, AB \ {1, 5 \}
() Group character. The Escape Character '\ (\)' must be used '\(\)' AB \ (XYZ \) *, \ (ABC \) Like \ (XYZ \)
\ Num Group reference. Strictly speaking, it is not a metacharacter, but a specific escape. It makes sense to use it with a group character. He \ (love \) s his \ 1R
^ First line anchor ^ #. *, ^ Root
$ Anchor at the end of a line China $, ^ $
< The anchor at the beginning of the word. The Escape Character '\ <' must be used' \ <[[: Digit:], \ <Root
> The ending anchor. The Escape Character '\>' must be used' \ <Root \>, \ <[[: alnum:] * \>


2. Extended metacharacters added to the regular expression

Metacharacters Description Example
+ Match the first character at least once. AB +, ^ # [[: Space:] + [^ [: Space:] +
| Select one more character in the group. The selection is (Yes | no), user name is (root | hadoop | RedHat | centos)

Note: In the extended regular expression mode, except for the first separator '<' and last separator '>', escape is required, no escape is required for other metacharacters!

3. Common options for grep and egrep commands

-V: returns the reversed display of rows with unmatched expressions.

-O only displays the matched text content, not the whole line.

-I matching is case-insensitive.

-E uses the extended regular expression mode.

-A num: displays the num row under the matched row at the same time.

-B num: displays the num row above the matched row at the same time.

-C num: both the top and bottom num rows of the matched rows are displayed.

-- Color = auto displays the matched text content in the appropriate font color.

-Q silent mode: This mode only cares about matching execution results without outputting any content. It is suitable for use in shell scripts.

-E expr has multiple Regular Expression Conditions, and must be combined with multiple expression conditions before each expression. -E can be omitted if there is only one expression condition by default.

4. ing between references in Regular Expressions

The regular expression uses '\ 1',' \ 2', '\ 3 '... Reference the content in the brackets corresponding to the preceding rule: the order in which left parentheses appear from left to right. '\ 1' references content starting from the left and starting from 1st left brackets to the corresponding content in the right brackets.' \ 2' references content starting from the left, the 2nd left brackets start with the corresponding content in the right brackets; The '\ 3' reference content starts from the left, and the 3rd left brackets start with the corresponding content in the right brackets; and so on. Therefore, from the perspective of the brackets, the content in the outer brackets is referenced first, and then the content in the inner brackets is referenced. Example:

Info. Text:

1 xyxyabababxyab

2 xyabxyab

3 xyabxyabxyab

4 xyxyxy

5 xyxyabababxyabxyxyababab

6 xyxyabababxyabxyabxyababxyabxyabxyab

[[Email protected] Test] $ egrep -- color = auto "(xy) {2} (AB) {3}) (\ 2 \ 3) * \ 1 \ 4 {2} "info. text

6 xyxyabababxyabxyabxyababxyabxyabxyab

5. The string pattern matching operator '= ~ supported in ~ '
  • You do not need to quote the regular expression on the right of the pattern matching operator.

  • The supported regular expressions are extended regular expression patterns. That is, no escape is required for all metacharacters.

  • The START separator '<' and end separator '>' are not supported '.

Linestr = 19934567

If [["$ linestr" = ~ ([1-9] [0-9] + | 1 [0-9] [0-9])]; then
Echo "$ linestr"
Fi


This article is from the blog of "bee Huai Yu Shan Ren", please be sure to keep this http://deshanrenjian.blog.51cto.com/9324633/1550435

Learning Experience of 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.