It uses a single character to describe and match a series of strings that conform to the rules of a statement. It is composed of ordinary characters and special characters. Regular expressions are widely used in script programming and text editor.
Regular expressions are abbreviated to regex, regexp, RE.
In most languages, regular expressions are included in two forward slashes "/"
Regular expressions have a feature of greedy matching: try to match the longest
- Basic regular expression
\Escape character. For example: "!" will be logical no! As a normal character, you can also break the lineExample 1: [root@localhost ~]# ls -l /home \ > /tmp //Example 2: [root@localhost ~]# grep "b.*in" a.txt 12 bin 34 23 sb.*in 23 44 binary 49 23 binlog 23 [root@localhost ~]# grep "b\.\*in" a.txt 23 sb.*in 23
^Matches the starting position of the string to For the first time
The position at the end of the $match string to Ending
^$indicates a blank line, not a spaceExample: [root@localhost ~]# cat a.txt aa aa bb aa a b a [root@localhost ~]# grep "^aa$" a.txt aa
. match any string *Match face subexpression (character) 0 times or more Example: [root@localhost ~]# grep "a 0*" a.txt aa 0 aa 00 aa bb 0000 aa 00 a b a 000
Example: [root@localhost ~]# grep "a 0\" a.txt aa 00 aa bb 0000 aa 00 a b a 000 \ Face subexpression before matching at least n second [root@localhost ~]# grep "a 0\" a.txt aa 00 aa bb 0000 aa 00 a b a 000 \ Least face subexpressions before matching n Second largest m second \< Fixed word head \> Fixed ending //Example: [root@localhost ~]# grep "\<a\>" a.txt a b a 000Escape character Significance \a Bell (BEL) \b Backspace (BS), move current position to previous column \n Line feed (LF) moves the current position to the beginning of the next line \r Enter (CR) moves the current position to the beginning of the line \t Horizontal tabulation (HT) to next TAB \v Vertical tabulation (VT) \ Represents a backslash character "\"
-
Extended regular expression
+Face subexpression 1 or more times before matching
? match face subexpression 0 or 1 times before
() string in parentheses as a whole
|Match characters in or
- grep matching, querying (filtering)
- sed edit (add, delete, modify)
- awk text formatting (string extraction)
- In Linux, regular processes files in behavioral units
- alias grep='grep --color=auto'
- Note character set, LANG=C
wildcard
Regular expressions are fundamentally different from the wildcards we use on the command line.
Wildcards usually deal with filenames
ls -l .txt
: any string of any length, belonging to wildcard.
?: a single arbitrary string, belonging to wildcard.
Parameters of grep command:
-v exclude matching content (reverse)
-E support extended regular expression = egrep
[root@localhost ~]# grep -Ev "^$|#" /etc/httpd/conf/httpd.conf ServerRoot "/etc/httpd" Listen 80 Include conf.modules.d/*.conf
-i ignore case
[root@localhost ~]# cat a.txt aAAA [root@localhost ~]# grep -i "a" a.txt aAAA [root@localhost ~]#
-o output only matching content
[root@localhost ~]# grep -o "root" /etc/passwd root root
– color=auto matching content display color (use less alias to view aliases)
-n display line number at the beginning of line
[root@localhost ~]# grep -n "root" /etc/passwd 1:root:x:0:0:root:/root:/bin/bash 10:operator:x:11:0:operator:/root:/sbin/nologin 43:admin:x:1000:1000:root:/home/admin:/bin/bash
-q matching content is not displayed (silent output, usually used when writing scripts but not output to the screen)
-w match filter conditions as words
[root@localhost ~]# grep -w "bin" /etc/passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync admin:x:1000:1000:root:/home/admin:/bin/bash
[root@localhost ~]# grep "bin" /etc/passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin
-c output only matching row count
[root@localhost ~]# grep -c "root" /etc/passwd 3