The experience of learning regular expressions under Linux system

I have always been confused about regular expressions. Today I carefully read the book "Bird Brother's Linux Private Kitchen" and searched for relevant information on the Internet. Now I have a certain understanding of regular expressions.

First of all, regular expressions refer to the use of specific strings to filter strings that meet certain requirements. We often use the command grep under the Linux system. This command is used to select the line where the strings that meet certain requirements are located. In fact, This is very simple. For a simple example, we want to find out whether the file newfile exists in a certain directory. We can use the pipeline command, ls | grep newfile, to find out whether the file we want exists. Then suppose we want to find out whether there is a file beginning with newfile in a certain directory, we can use "ls | grep '^newfile'" to achieve it, the function of ^ is the mark at the beginning of the line, which refers to the newfile at the beginning of the line row out. Since the characters at the beginning of the line can be specified, of course the characters at the end of the line can also be specified. What is used to mark the end of the line? It is $. For example, if we want to find the line ending with newfile, we can give the command like this, ls | grep 'newfile$', so it is very clear.

So what else is there to learn about regular expressions? Such as *, the * here is different from the wildcard character used in the bash environment representing 0-innumerable characters, and the * here refers to a string of 0-innumerable characters that repeat the previous character. For example, we want to display in expand.txt Find in the file whether there is a string starting with g, ending with g, and there are 0 or several o in the middle. How should we give commands? At this time, we can use characters like *, grep -n 'go*g' expand.txt, so that we can find the string that meets the requirements.

Regarding regular expressions, we also need to use such as [a-zA-Z], which represents one of the English characters; and ^[^a-zA-Z], this ^ is different inside and outside [] Yes, it means the beginning of the line on the outside, and the reverse selection on the inside, so it means the line whose beginning of the line is not an English character; and . refers to a character;

Of course, there are still many things to learn about regular expressions. . . . . .

Guess you like

Origin blog.csdn.net/daida2008/article/details/46765931