Home>Article>Web Front-end> Detailed explanation of the use of grep command for regular expressions
This time I will bring you a detailed explanation of how to use the grep command ofregular expressions, and a detailed explanation of how to use the grep command of regular expressions.What are the precautions?. The following is a practical case. Get up and take a look.
Regular expression is a type of expression used to match text. It is often used to express search conditions in the grep command. In fact, it is equivalent to setting some advanced requirements when you search on Baidu. For example, if you want to find photos of a person named James on Baidu, you will find a lot of James from the Cavaliers. At this time, you need advanced requirements: Search except James besides LeBron. This is where regular expressions are used.
Learning regular expressions is simply an inhuman job, because it does not work according to normal people's thinking at all, so you must be mentally prepared.
I saw a lot of usage of regular expressions when I first started learning. Later I discovered that regular expressions are different in different environments. This is reflected in whether certain words requireEscape character\ and metaCharacter set, so here we will integrate the regular expression related content for grep in bash.
Text Basic usage of search tool grep
Function: Findstringin each line of the input file.
Basic usage:
grep [-acinv] [–color=auto] [-A n] [-B n] 'Search string' file name
Parameter description:
-a: Process the binary document as text
-c: Display the number of matches
-i : Ignore the case difference
-n: Display the line number at the beginning of the line
-A: After means, display the data of n lines after the matching string
-B :Before means, display the data of n lines before matching string
-v: Display no matching lines -A: After means, display n lines after the matching part -B: Before means, display the matching part n lines before
–color: Highlight matching keywords in a specific color
Text search tool grep regular expressionmetacharactersset
^
The beginning of the anchor line is like: '^grep' matches all lines starting with grep.
$
End of anchor line For example: 'grep$' matches all lines ending with grep.
.
Matches a non-newline character. For example: 'gr.p' matches gr followed by any character, then p.
*
Matches zero or more previous characters. For example: 'grep' matches all lines with one or more spaces followed by grep. . used together represents any character.
[]
Matches characters within a specified range, such as '[Gg]rep' matches Grep and grep.
[^]
Matches a character that is not within the specified range, such as: '[^A-FH-Z]rep' matches a letter that does not include A-R and T-Z, followed by rep line.
(..)
mark matching characters, such as '(love)', love is marked as 1.
\<
The beginning of the anchor word, such as: '\
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to php Chinese website Other related articles!
Recommended reading:
The principles and syntax of parsing JS regular expressions
Detailed explanation of the use of regular non-capturing groups and capturing groups
The above is the detailed content of Detailed explanation of the use of grep command for regular expressions. For more information, please follow other related articles on the PHP Chinese website!