Regular expressions and grep
Regular expressions are divided into two categories: basic regular expressions and extended regular expressions.
Basic regular expression metacharacters:
Character matching:
.: matches any character.
[]: matches any character in the specified range in [].
[^]: any character outside the specified range; you can also know that in [] parentheses, ^ indicates inversion.
Number of times match:
*: the character before it appears any number of times.
\?: the preceding character appears 0 or 1 times
\ {mdirection n}: the first character appears at least m times and up to n times. If there is no upper limit, n can not be written, and if it occurs at least 0 times, m is 0. M cannot be omitted.
. *: any character of any length appears before it.
Character Mou Ding:
^: the head of the line is Mou Ding.
&: Mou Ding at the end of the line.
\
< 或 \b:牟定单词在行首 \>Or\ b: the word Mouding is at the end of the line
\ (\): used for grouping. Backward reference:\ 1,\ 2,\ 3. Indicates the contents in parentheses of the nth pair of references
Grep: commands that use patterns defined by basic regular expressions to filter text are supported.
-I: ignore the case of matching letters
-v: reverse matching, displayed as matching content
-o: only the matching content is displayed
-- color: color display
-E=eprep: use extended regularities for matching.
-A digits,-B digits,-C digits:-A (after) shows not only the matching lines, but also the next N rows that match. -B (before) indicates that the first N lines of the match are displayed at the same time, and-C indicates that the first N lines and the last N lines are displayed at the same time.
More options are viewed in man.
To be clear, basic regular expressions work in greedy mode.
Extended regular expression
Character matching, position determined: same as basic regular
.
[]
[^]
Number of times match:
*: the same as the basic regular, the characters preceding it appear any number of times.
? Same as the basic regular, the preceding character appears 0 or 1 times
+: an extra + matches its first character at least once. Equivalent to\ {1,\}? + = *
{mdirection n}: you don't need to use\ to change your mind in the extension rule.
Grouping and
(): grouping
1, 2, 3. Backward reference
Or
|: | the front and rear are each as a whole. For example, abc | dec: indicates abc or dec. You can use egrep-- color for verification. To indicate c or d but want (), ab (c | d) ec