Linux--Shell--Analysis of Regular Expressions--Multiple Cases


Regular expression


Overview

  • Usually used in judgment sentences to check whether a string meets a certain format
  • A regular expression is a common character and metacharacters composition
  • Common characters include uppercase and lowercase letters, numbers, punctuation marks and some other symbols
  • Metacharacters refer to special characters with special meaning in regular expressions. They can be used to specify the appearance mode of its leading character (that is, the character before the metacharacter) in the target object

Basic regular expression common metacharacters

  • Supported tools: grep, egrep, sed, awk
Common metacharacters Explanation
\ Escape characters, used to cancel the meaning of special symbols, for example: \!, \n, \$, etc.
^ The starting position of the matching string, for example: ^a, ^the, ^#, ^[az]
$ End of the string matching position, for example: Word Katex the parse error: After the Expected Group '^' position AT 2:, ^ matches the null line
. Match any one character except \n, for example: go.d, g...d
* Match the preceding sub-expression 0 or more times, for example: goo*d, go.*d
[list] Match a character in the list, for example: go [ola]d, [abc], [az], [a-z0-9], [0-9] match any digit
[^list] Match any non-list character-for example: [^0-9], [^A-20-9], [^az], match any non-lowercase letter
{n} Example: go{2}d,'[0-9]{2}' matches two digits' matches two digits
{n,} Match the preceding sub-expression no less than n times, for example: go{2,)d,'[0-9]{2,}, match two or more digits
{n,m} Match the preceding sub-expression n to m times, for example: go{2,3}d,'[0-9]{2,3} matches two to three digits
note Egrep, awk use {n}, {n,}, {n,m} when matching, without adding "\" before "{}"

Extended regular expression metacharacters

  • Supported tools: egerp, awk
Metacharacter Explanation
+ Match the previous sub-expression more than once, for example: go+d, will match at least one o, such as god, good, goood, etc.
? Match the previous sub-expression 0 or 1 times, for example: go?d, will match gd or god
() The string in the brackets as a whole, for example: g(oo) +d, will match. Overall more than 1 time, such as good, gooood, etc.
| Match the string of words in an or manner, for example: g(oo|la)d, will match good or glad

Regular expression matches E-mail address

用户名@: ^ ([a-ZA-Z0-9_\-\.\+]+)@
子域名: ([a-zA-Z0-9_\-\.]+)
·顶级域名(字符串长度一般在2到5) : \. ( [a-zA-Z] {
    
    2,5})$

egrep  '^ ([a-zA-Z0-9\-\.\+]+) @ ( [a-ZA-Zo-9_\-\.]+) \. ([a-zA-Z] {2,5})$'  email.txt
awk '/^ ([a-zA-Z0-9_\-\.\+]+) @ ( [a-zA-Z0-9_\-\.]+)\. ( [a-zA-Z] {2,5})$/{print $o}'  email.txt

Example

mark

grep "goo*d" xcf.txt 

mark

grep "go.*d" xcf.txt

mark

mark

cat xcf1.txt

grep "[^abc]" xcf1.txt

mark

mark

cat xcf2.txt 

grep "go\{2\}" xcf2.txt

mark

egrep "go+d" xcf2.txt 

egrep "g(oo)+d" xcf2.txt 

egrep "go?d" xcf2.txt 

mark

Guess you like

Origin blog.csdn.net/weixin_51486343/article/details/111760036