[shell] Regular expressions: common wildcards, metacharacters and escape characters

1. Definition

A regular expression is a formula that can use a certain pattern to match a type of string. It is a string composed of a string of characters and metacharacters. The so-called metacharacters are characters used to explain the content of character expressions, convert and describe various operation information.

 

2. Common wildcards

1. Match a

1.1 " . " Dot notation

The dot notation is used to match any character except newline .

[root@localhost ~]# grep 'r..t' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

 

1.2. " ?" Symbol: represents any

When used as a wildcard, it represents any character.
If you want to list files that start with the letter A, have a letter in the middle, and end with .doc, you need to use "?".

[root@localhost ~]# ls-l A?.doc

 

1.3. " $ " symbol: match tail

" $ " is used to match the tail, for example " abc$ " represents the line ending with abc.

" ^$ ": means the line is empty, because there is nothing between ^ and $

# 以r开头,中间有一串任意字符,以h结尾的行
[root@localhost ~]# grep '^r.*h$' /etc/passwd
root:x:0:0:root:/root:/bin/bash

 

1.4. " [] "symbol

Used to match any character appearing in square brackets

Any one of the options A, B, C, and D, expressed in regular expressions is [ABCD]
To match any uppercase letter, you need to use the "-" sign to limit the range, written as [AZ], to match all letters Then write [A-Za-z].

Match mobile phone number: "^1[38][0-9]{9}"

 

1.5. " ^ "symbol and " ! "symbol

  1. When it appears in "[]", it means negation. [^A] (or [!A]) means not A.
  1. Used to match at the beginning. For example, "^root" matches lines beginning with the letter root.
[root@localhost ~]# grep '^root' 
/etc/passwdroot:x:0:0:root:/root:/bin/bash

 

2. Match multiple

2.1. " * " symbol

Represents 0 or more characters. For example, *.doc refers to all files ending in .doc.

The " " sign is often used together with the "." symbol. For example, ". " represents any length of characters that do not contain newlines.

[root@localhost ~]# grep 'r*t' /etc/passwd
root:x:0:0:root:/root:/bin/bash
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
。。。。

 

2.2. "{n,m}" notation

" * " cannot precisely control the number of repetitions of the match, but using the "{n,m}" symbol can control the number of repetitions of characters more flexibly.

 

2.3. "{}" symbol

Matches all comma-separated characters enclosed in parentheses.

For example, the following lists all files beginning with the letters A, B, C and ending with .doc:

#第一种方法:用“{
    
    }[root@localhost ~]# ls -l {
    
    A,B,C}.doc
#第二种方法:用“[][root@localhost ~]# ls -l [A-C].doc
#以上两种方法都能满足题意,但是如果要列出以字母AB或者CD开头、以.doc结尾的文件,就只能用“{
    
    }”了。
[root@localhost ~]# ls -l {
    
    [A-Z]*.doc,[0-9]??.txt}

 

3. Metacharacters and escape characters

insert image description here

 

insert image description here

 
 

Reference:
https://www.cnblogs.com/chengmo/archive/2010/10/17/1853344.html

Guess you like

Origin blog.csdn.net/hiliang521/article/details/131532569