A regular expression is a logical formula for a string operation, which is a "rule string" that is used to express a filter logic for a string by using a predefined set of specific characters (metacharacters) and combinations of those specific characters. The meta-character does not represent the meaning of the character itself, but is used to express control or the function of the wildcard;
Object: String
Composition: Meta character + normal character
Function: Filter text characters, find matching characters and draw results for other commands to use
Action object: Text character (content inside the text file)
Given a regular expression and another string, we can achieve the following purposes:
1. Whether the given string conforms to the filtering logic of the regular expression (called "Match");
2. You can get the specific part we want from the string using a regular expression.
Regular expressions are characterized by:
1. Flexibility, logic and functionality are very strong;
2. Complex control of strings can be achieved quickly and in a very simple way.
grep is the underlying regular expression that the command uses to find the specified string within the file.
Full search of regular expressions and printing of rows is a powerful text-search tool that can use regular expressions to search for text and print matching lines.
GREP[-ACINV] [--color=auto] ' search string ' filename '
Options and Parameters:
- o : Shows only what is matched to the pattern
- v : Displays rows that cannot be matched to a pattern
- e : Using an extended regular expression
- I. : Ignores case differences, so case is considered the same
--color=auto : You can add a color display to the keywords you find!
- b : The first two lines are included
-A : Comes with the next two lines
- C : With the upper and lower lines displayed together
Linux egrep is an extended regular expression that is used to find the specified string within a file.
Egrep The execution effect is similar to "GREP-E", and the syntax and parameters used refer to the GREP directive, which differs from grep in the way the string is interpreted.
Character matching
. : Matches any single character
[] : Specify any single character in the collection
[^] : Matches any single character outside the specified collection
+ : Matches the preceding character appears at least 1 times
Match number limit
* : Matches any previous, 0,1, or multiple occurrences of the preceding character or not
? : Matches the preceding character 0 or 1 times
{m} : Matches the preceding character m times
{m,n} : Matches the preceding character at least m times, at most n times, {m,} at least m, at most without limit, {0,n} represents at least 0 times, up to N times
.* : Matches any character of any length
Character anchoring
^ : Anchor at beginning of line, appear leftmost
$ : Anchor at end of line, appears rightmost
\< : The first anchor of the word, on the left side of the found word pattern; \<char
\> : The ending anchor, on the right side of the found word pattern;char\>
\<pattern\> : Match words
Regular expressions and grep, egrep usage