Shell programming --- Regular Expressions
Detailed explanation of Regular Expressions
Common Character: only literal meaning
Metacharacters: meaning beyond the literal meaning
Basic Regular Expression metacharacters and their meanings
* #0 or multiple normal characters before *
. # Match any character
^ # Match the beginning of a row
$ # Matching the end of a row
$ # Run the previous command
! $ # The last parameter of the previous command, such as/etc/passwd, can be cat! $
[] # Matching Character Set combination
# Escape Character
\ {N \} # match the previous CHARACTER n times
\ {N ,\}# match the previous character at least n times
\ {N, m \} # match the previous character to appear n-m times
Example
* El * o # matches with helll8o, hello, and hellllo.
... 73 .. # can match abc73 .,! @ #73? , 12373c
^... X86 * # can match characters starting with abcX86fang
$ Make $ # indicates ending with make
^ $ # Indicates empty rows
^. $ # Indicates that a row has only one character.
[A-z] # represents all lowercase letters of a-z
[0-9] # All numbers 0-9
[B-q] # B-q letters
[^ B-q] # returns the inverse, except all the characters in B-q.
[A-Za-z] [A-Za-z] * # match any English word
\ * # Convert the metacharacters after \ To literal meanings
\ <The \> # exact match: matches only the words of the character. Use \ symbol shielding <> symbol grep '\ <the \>'
The matching value of \ {n \} # JO \ {3 \} B is JOOOB.
The matching value of \ {n ,\}# JO \ {3, \} B is JOOOB joooooob JOOOOOOOB.
The matching value of \ {n, m \} # JO \ {3, 6 \} B is JOOOB jooooooob JOOOOOOB.
[A-z] \ {5 \} # hello house whowh exact match five lower-case letters
Echo [abc] * # Any multi-character file starting with a/B/c
Echo [^ abc] * # Any multi-character file starting with a/B/c
Touch {a, B, c}-{1, 2, 3} # create a file by means of exhaustive action. Each file is created by matching. a total of 9 files are created.
! $ # Last parameter in the previous command
; # Indicates that the command has ended
========================================================== ==========================================
Extended Regular Expression metacharacters and their meanings
? # Match 0 or 1 common character before it
+ # Match one or more common characters before it
() # Indicates that a character set is combined or used in expr.
| # Indicates or, matching a set of optional characters
? JO? B # match JO1B JO @ B JO! B. Only one character is matched. If it is a JOB or JOOOB, it does not match
+ S + EU # matches the first character at least once and can be matched multiple times
* S * U # matches the S123U ssuu s! @ # U SU
() Symbol and | symbol re (a | e | o) d # match read reed reod
Example
1. List all files ending with. awk
Ll *. awk
2. List Files starting with 0, followed by a character and suffixed with. pem
Ll 0 ?. Pem
3. List objects whose names start with a letter and end with. awk in the-h range
Ll [a-h] *. awk
4. List objects starting with a letter in the-h range and not ending with. awk after a period
Ll [a-h] *. [^ awk] *
5. List the files that meet the requirements of starting with a letter and ending with. awk within the-h range, or list the files that start with 0, followed by a character and suffixed with. pem
Ll {[a-h] *. awk, 0 ?. Pem}