This article mainly introduces some of the most basic usage methods in linux. Here are some experiences and test instances for the webmaster to learn linux regular expressions. Let's take a look at them.
Concepts:
1. Regular Expressions extended by basic regular expressions (consisting of common characters and metacharacters)
2. wildcard (composed of common characters and metacharacters)
3. metacharacters
Note: The meanings of metacharacters in regular expressions and the meanings of metacharacters in regular expressions are different.
Bash shell itself does not support regular expressions. It uses shell commands and tools, such as grep, sed, and awk.
However, bash can use some metacharacters in regular expressions to enable wildcard functions. At this time, these metacharacters are called wildcards.
In this case, the meaning of the wildcard metacharacters is different from that of the regular expression metacharacters.
Wildcard configuration refers to the process of extending a batch of specific file names that contain wildcards to computers, servers, and networks.
The significance of metacharacters in a basic regular expression is as follows:
For example, save the following file as the regular_express.txt file.
"Open Source" is a good mechanic to develop programs.
Apple is my favorite food.
Football game is not use feet only.
This dress doesn' t fit me.
However, this dress is about $3183 dollars.
GNU is free air not free beer.
Her hair is very beauty.
I can't finish the test.
Oh! The soup taste good.
Motorcycle is cheap than car.
This window is clear.
The symbol '*' is represented as start.
Oh! My god!
The gd software is a library for drafting programs.
You are the best is mean you are the no. 1.
The world <Happy> is the same with "gglad ".
I like dog.
Google is the best tools for search keyword.
Goooooogle yes!
Go! Go! Let's go.
# I am VBird
2. First, let's take a look at the content., Vim regular_express.txt
3. Search for a specific string
Use the q command to exit. If you accidentally modify the content, Use q! Force exit
Enter
grep -n ‘the’ regular_express.txt
You can see the rows with the (PS: Linux is case sensitive. I used apache to release the phpMyAdmin homepage because M and A are not in uppercase, it took a long time to find that the webpage could not be opened for this reason)
The query result is as follows:
4. Reverse SelectionIs to find the content without this string (windows does not have this function)
grep -vn ‘the’ regular_express.txt
Add a v and it will be OK. Maybe it is reverse.
5. Case InsensitiveSearch
grep -in ‘the’ --color=auto regular_express.txt
Add an I, ignore. As you can see, there are rows 9 and 14th more.
6. Fuzzy search using brackets
For example, to search for the taste and test words and find that they are in the t ■ st format, you can use the command
grep -n ‘t[ae]st’ regular_express.txt
If you only want to check for oo characters, run the following command:
grep -n ‘oo’ regular_express.txt
If you do not want to search for any g above, use [^] to exclude it.
grep -n ‘[^g]oo’ regular_express.txt
In addition, if you do not want oo to be preceded by lowercase letters, you can do this.
grep -n ‘[^a-z]oo’ regular_express.txt
7. You want the first line to end with a certain character.(^)
grep -n '^the' regular_express.txt
Do not start with an English letter
grep -n '^[^A-Za-z]' regular_express.txt
A little dizzy? The first ^ indicates that it must be met; the second ^ indicates that it is not, not. So we found the rows starting with a symbol or number. A-z represents all lower-case letters, and A-Z represents all upper-case letters.
Search for rows ending '.'
grep -n ‘.$’ regular_express.txt
Escape characters are used to remove the special meaning. Why did 5-9 rows not print out?
Let's use cat to see
cat -An regular_express.txt
They end with ^ M $. What is this? In fact, this is the difference between the windows and Linux broken line characters $.
8. Any one character. And repeated characters *
grep -n ‘g..d’ regular_express.txt
There must be 2 characters, so you don't have to worry about anything.
* There are 0 to infinite
grep -n 'ooo*' regular_express.txt
The first two oo objects must exist, and the third o objects must have 0 to infinite objects.
9. Limit the continuous RE character range {}
grep -n 'go{2,5}g' regular_express.txt
Goooooole is not selected. Why?
Because we have limited o to 2-5 times, and the above word o appears 6 times!