[a-z]: lowercase letters
[A-Z]: Uppercase letters
[a-Z]: Small or uppercase letters
[0-9]: Numbers
[a-zA-Z0-9]: Matches a character that is a letter or number
. : Matches any character, except spaces
[0-f]: Hexadecimal number
abc | def: abc or def
a (bc | de) f: abcf or adef
\<: The first word is usually separated by spaces or special characters, and the continuous string is regarded as the word
\>: Word ending
[^expression]: All characters except lowercase letters, and so on.
The left side of such symbols must have the expression of the first point above
Expression*: 0 or n characters
Expression+: 1 or n characters
Expression?: 0 or 1 characters
Expression {n}: n characters
Expression {n:m}: n to m Characters
Expression {n,}: at least n characters
[Example] [a-z]* means matching 0 Or multiple lowercase letters
^ Expression: The head matches
Expression$: The tail matches
Extended version of grep, you can use regular expressions
egrep - option 'regular expression' file name
-n: Display line number
① Numeric positioning (input line number positioning)
【Example】sed -n '1p' /etc/passwd
②Regular expression positioning
[Example] sed -r 's/ (.)(.)/\2\1/ file1 means to replace the first and second parts of the match
*Greedy option: fill in g, which means to replace all the matching parts in one line Matching item replacement
*s command special instructions:
Use {Command 1: Command 2: Command 3} Multiple commands can be addedsCommand syntax: sed -r 'Replacement command s/regular expression/replacement content/greedy option g' File name3, awk text analysis toolComposed of commands, regular expressions (need to be surrounded by //), comparisons and relational operationsUse the -F parameter in option to define the interval symbolUse the order of $1, $2, $3, etc. to represent the different fields in each column separated by spacers in each row of files. The NF variable represents the number of fields in the current record.
awk - Option parameters 'Logical judgment {command variable 1, variable 2, variable 3}' File name
-F Define field separator , the default delimiter is consecutive spaces or tabs
-v. Define variables and assign values. You can also use the borrowed method to introduce
AWK variable
NR The number of current records (statistics after all files are connected)
FNR The number of current records (only statistics for the current file, not all)
FS field separator defaults to consecutive spaces or tabs, and multiple different symbols can be used for separation. Symbol -F[:/]
OFS The default separator for output characters is a space
[OFS example]
# awk -F: 'OFS="=====" {print $1,$2}' /etc/passwd
root===== x
NF The number of fields in the currently read row
ORS The output record separator defaults to newline
【ORS example】
# awk -F: 'ORS="=====" {print $1,$2}' /etc/ passwd
root x=====bin x=====
FILENAME Current file name
[Example 1] Using AWK variables
# awk '{print NR,FNR,$1}' file1 file2
1 1 aaaaa
2 2 bbbbb
3 3 ccccc
4 1 dddddd
5 2 eeeeee
6 3 ffffff
#[Example 2]How to quote shell variables
# a=root
# awk -v var=$a -F: '$1 == var {print $0}' /etc/passwd
Or split the entire command and pass it to expose the shell variables,
# awk -F: '$1 == "'$a'" {print $0}' /etc/passwd
# a=NF
# awk -F: '{print $'$a'}' /etc/passwd
= += -= / = *=: Assignment
&& || !: Logical and logical or logical non-
~ !~: Match regular or not match, Regular expressions need to be surrounded by /regular/
Please correct me if there are any errors. For more details, please refer to:
The above is the detailed content of regular expression. For more information, please follow other related articles on the PHP Chinese website!