There are two types of regular expressions:
Basic REGEXP: Basics
Extended REGEXP: Extended
Basic Regular Expressions:
. : Any single character
[]: A single character in the specified range
[^]: Specify a single character outside the range
Number of matches:
*: The characters in front of it any time
\?:0 or 1 times
\{m,n\}: At least m times, up to N times
. *: Any character of any length
Anchoring:
^: Anchor at the beginning of the line
$: End of line anchoring
\<: Anchor Word first
\>: Anchor ending
\ (\): Grouping
\1,\2,\3,...
grep: A command that filters text using patterns defined by a basic regular expression
-I: Case insensitive
-V: Reverse match
-O: Show only the matching string
--color: Matching to the highlighted display
-E: Using extended regular expressions
-A #: When a row is matched to a pattern specified by grep, the line is displayed, followed by
-B #: Ditto, not only the matching line shows, the front also shows
-C #: Ditto, not only the matching line shows, the upper and lower # lines are also displayed
To extend the regular expression:
Character Matching:
. : Matches a single character
[]: A single character in the specified range
[^]: Specify a single character outside the range
Number of matches:
*; its preceding character any time
? : Its former character 0 or 1 times
+: Match the characters in front of it at least once
{M,n}
Location anchoring:
^
$
\<
\>
Group:
(): Group
\1,\2,\3,...
Or
| : OR
C|car:c or Cat
GREP-E = Egrep
Match white space characters at least once
# grep-e ' ^[[:space:]]+ '/etc/passwd
Find the number between 1-255 in the/boot/grub/grub.conf file;
# egrep ' \< ([1-9] | [1-9] [0-9] | 1 [0-9] [0-9] | 2 [0-4] [0-9] | [0-5]) \> '/boot/grub/grub.conf
IPv4:
Five categories:
A:1-127
b:128-191
c:192-223
D
E
Match IP:
# \< ([1-9]|[ 1-9][0-9]|1[0-9]{2}|2[01][0-9]|22[0-3]) \> (\.\< ([0-9]|[ 1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4] \>) {2}\.\< ([1-9]|[ 1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4]) \>
Grep,egrep
Fgrep: Regular expressions are not supported
Linux--grep and regular Expressions (II.)