Basic formal notation character rounding (characters)
After a few simple examples above, we can combine the basic formal notation special characters as follows:
RE character |
Meaning and example |
^word |
Meaning: the string to be searched (word) at the beginning of the line! Example: Searching for the line starting at the beginning of lines, parallel travel number
grep-n ' ^# ' regular_express.txt
|
word$ |
Meaning: the string to be searched (word) at the end of the line! Example: End of line is! The ranks of the line printed out, parallel travel number
grep-n '!$ ' regular_express.txt
|
. |
Meaning: "Must have an arbitrary byte" character! Example: The search string can be (Eve) (Eae) (EEE) (e e), but not only (EE)! That is, E and E in the middle "must" only have one byte, and the blank byte is also a byte!
grep-n ' e.e ' regular_express.txt
|
\ |
Meaning: Skipping characters, removing special meanings of special symbols! Example: Search for the line containing the single quote '!
grep-n \ ' Regular_express.txt
|
* |
Meaning: Repeat 0 to infinitely multiple of the previous RE character Example: Find a String containing (es) (Ess) (ESSS) and so on, note that because * can be 0, ES is also compatible with the search string. In addition, because * is a repetition of the "previous re character" of the symbol, so, before the * must be connected to a RE-character Fu Yi! For example, any byte is ". *"!
grep-n ' ess* ' regular_express.txt
|
[List] |
Meaning: The byte collection of the RE character, which lists the bytes you want to retrieve! Example: Search for a line containing (GL) or (GD), it is necessary to pay special attention to [] in the "to represent a byte to be searched", such as "A[afl]y" for the search of the string can be Aay, Afy, Aly that [AFL] for a or F or l mean!
grep-n ' g[ld] ' regular_express.txt
|
[N1-N2] |
Meaning: The byte collection of the RE character, which lists the range of bytes you want to retrieve! Example: Search for the line that contains any number! Pay special attention to the minus sign in the byte set []-it's special, he represents all the contiguous bytes between two bytes! But this continuity is related to ASCII encoding, so your coding needs to be configured correctly (in bash, you need to determine if LANG and LANGUAGE variables are correct!) For example, all uppercase bytes are [a-z]
grep-n ' [A-z] ' Regular_express.txt
|
[^list] |
Meaning: The byte set of the RE character, which lists no strings or ranges! Example: The search string can be (Oog) (Ood) but not (oot), that ^ within [], the meaning of the representation is "reverse selection" meaning. For example, I don't want to capitalize bytes, then [^a-z]. However, it is important to note that if you search by Grep-n [^a-z] Regular_express.txt and find all the lines in the file are listed, why? Because this [^a-z] is a "non-uppercase byte" meaning, because each row has a non-uppercase byte, for example, the first line of "Open Source" has p,e,n,o .... And so on.
grep-n ' oo[^t] ' regular_express.txt
|
\{n,m\} |
Meaning: "Previous RE character" for successive N to M Meaning: If \{n\} is the previous RE character of a continuous n Meaning: If \{n,\} is a continuous n more than the previous RE character! Example: between G and G there are 2 to 3 o strings that exist, i.e. (GOOG) (Gooog)
grep-n ' go\{2,3\}g ' regular_express.txt
|
Tips: One thing to report to you is this: " formal notation is totally different from million bytes!" "It's important!" Because "Universal bytes (wildcard) represents a function of the bash operation Interface", the formal notation is a way of string processing! This is a very clear distinction. So, to learn this chapter, please forget the meaning of bash in the previous chapter.
To be honest, brother Bird had just come into contact with the formal notation, and was always thinking about combining the two, and the result was ... Wrong cognition a lot ~ so it is recommended that you learn this chapter first forget the million bytes to learn it! |
For example, LS, which does not support the formal notation, if we use "Ls-l *" to represent a file of any filename, and "ls-l A *" represents a file with any filename starting with a, but in the formal notation we need to find a file containing the beginning of a, it must be: ( Need to be paired with tools that support the formal notation)
ls | grep-n ' ^a.* '
Summary of the basic syntax for regular expressions