Basic regular expression character sink, regular expression character sink

Source: Internet
Author: User
Tags uppercase character egrep

Basic regular expression character sink, regular expression character sink

^ Word ---- meaning: the string to be searched (word) at the beginning of the line
Example: Search for the row whose first line is # and list the row number
Grep-n' ^ # 'test.txt
Word $ ---- meaning: the string to be searched (word) at the end of the row
Example: end the line! Print the row and list the row number.
Grep-n '! $ 'Test.txt
. ------- Meaning: a character that represents [must have any character]
Example: The search string can be (eve) (family) (eee) (e), but it cannot only be (ee), that is, e and e must have only one character in the middle, spaces are also characters
Grep-n 'E. E' test.txt
\ ------- Meaning: Skip a character to remove the special meaning of a special symbol
Example: Search for the line containing single quotes
Grep-n \ 'test.txt
* ------- Meaning: repeat the first RE character from zero to infinite
Example: Find the strings containing (es) (ess) (esss). Note that because * Can be 0, es is also consistent with the string to be searched. In addition, because * is the symbol that repeats the previous RE character, a RE character must be followed before. For example, any character is [. *]
Grep-n 'ess * 'test.txt
[List] ------ meaning: RE character in character set, which lists the characters to be truncated
Example: Search for the line containing (gl) or (gd). Note that [] only represents a character to be searched, for example, a [afl] y indicates that the searched string can be aay, afy, aly, that is, [afl] indicates the meaning of a, f, or l.
Grep-n 'G [ld] 'test.txt
[N1-n2] --- meaning: RE character in character set, which lists the range of characters to intercept
Example: Search for the row containing any number! Note that the minus sign in the character set [] is of special significance. It represents all consecutive characters between two characters! But this continuity is related to ASCII encoding, so your encoding needs to be set correctly, for example, all uppercase letters are [A-Z]
Grep-n '[0-9]' test.txt
[^ List] ------ meaning: RE character in character set, which lists strings that do not match
Example: The searched string can be (oog) (ood) but not (oot). The ^ in [] indicates the meaning of reverse selection. For example, if I do not use uppercase characters, it is [^ A-Z]. However, it is important to note that if you search for grep-n [^ A-Z] test.txt, you find that all rows in the file are listed. Why? Because this [^ A-Z] is not an uppercase character, because each line has a non-uppercase character.
Grep-n 'oo [^ t] 'test.txt
\ {N, m \} ---- meaning: The first RE character from n to m in a row
If it is \ {n \}, It is n consecutive first RE characters
If it is \ {n, \}, it is more than n consecutive first RE characters

Example: There are two to three o strings between g and g, also (goooog)
Grep-n 'go \ {2, 3 \} G' test.txt

Extended regular expressions have several special symbols:

+ ---- Meaning: repeat one or more previous RE characters
Example: Search for strings (god) (good) (goood)... and so on. The o + represents more than one o.
Egrep-n'go + d' test.txt
? ---- Meaning: zero or one of the first RE characters
Example: search for the string (gd) (god. Which o? Represents an empty or one o
Egrep-n' go? D 'test.txt
| ---- Meaning: returns several strings in the or (or) method.
Example: Search for the gd or good strings
Egrep-n 'gd | good 'test.txt
Egrep-n 'gd | good | dog 'test.txt
() --- Meaning: Find the group string
Example: search for the two strings (gglad) or (good). Because g and d are duplicated, I can list la and oo in, separated by |
Egrep-n 'G (la | oo) D' test.txt
() + -- Meaning: Identification of multiple duplicate groups
Example: Call AxyzxyzxyzxyzC with echo, and then use the following method to search
Egrep 'a (xyz) + C' test.txt
The above example indicates that I want to find A string starting with A and ending with C, and there is more than one "xyz" in the middle.

Note :! It is not a special character in a regular expression.

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.