正則表達式備忘錄-Regular Expressions Cheatsheet中文版


正則表達式備忘錄
Regular Expressions Cheatsheet中文版
原文:https://www.maketecheasier.com/cheatsheet/regex/

 

測試文件a.txt

0x1:

If you work with text, 

you’ll appreciate

 how useful regular expressions are. 

0x00001:

Regular expressions

 are everywhere in Linux

 for searching through text

 right down to the character. 

0x0000001:

This Regular Expressions cheatsheet 

 will be useful

 for people 

 who simply need a little refresher 

 from time

 to time.

 

字符

描述

例子

. (點)

任何單個字符,除了換行(\n)

c.t 匹配 "cat", "cut" 或 "cot."。

'任意字符加im':

[root@test: /tmp]# egrep '.im' a.txt

 who simply need a little refresher 

 from time

 to time.

* (星號)

重復前一個表達式0或多次

12*3 匹配 "13", "123", "1223", "12223"。 

與 . 合用代表任何字符。

m.*easier 匹配 "maketecheasier"。

'x加任意個0加1:'

[root@test: /tmp]# egrep 'x0*1:' a.txt

0x1:

0x00001:

0x0000001:

'任何包含f加任意字符加l'

[root@test: /tmp]# egrep 'f.*l' a.txt

 how useful regular expressions are. 

 will be useful

 for peopl

+ (加號)

重復前一個表達式1或多次

12+3 匹配 "123","1223","12223"

'x加至少一個0加1:' (比較上一個用*的)

[root@test: /tmp]# egrep 'x0+1:' a.txt

0x00001:

0x0000001:

? (問號)

前一個字符可有可無

ma?ke 匹配 "make", "mke"

'有n或無n加ee'

[root@test: /tmp]# egrep 'n?ee' a.txt

This Regular Expressions cheatshee

 who simply need a little refresher 

^ (尖號)

匹配字符串的開頭

^he 匹配以he開頭的 "hello", "hell", "help", "he is a boy"

'以空格開頭的行'

[root@test: /tmp]# egrep '^ ' a.txt

 how useful regular expressions are. 

 are everywhere in Linux

 for searching through text

 right down to the character. 

 will be useful

 for people 

 who simply need a little refresher 

 from time

 to time.

$ (美刀)

匹配字符串的結尾

ed$ 匹配以ed結尾的 "acted", bed", "greed"

'字母e結尾的行'

[root@test: /tmp]# egrep 'e$' a.txt

you’ll appreciate

 from time

(...) (小括號)

匹配字符組合

(ak) 匹配 "make", "take"

'包含 it 的'

[root@test: /tmp]# egrep '(it)' a.txt

If you work with text, 

 who simply need a little refresher

{n} (大括號,n是大於0的整數)

重復前一個字符n次,n>0

12{3}5 匹配 "12225"

'x加4個0加1'

[root@test: /tmp]# egrep 'x0{4}1' a.txt

0x00001:

[...] (中括號)

匹配里面的任意一個字符

[abc] 匹配字符串"abc"中的"a","b" 或 "c"

'所有包含v或b的'

[root@test: /tmp]# egrep '[vb]' a.txt

 are everywhere in Linux

 will be useful

[^...]

匹配任意字符,除了里面定義的

a[^b]c 匹配 "aec", "acc", "adc", 但不匹配 "abc"

'f前面不能是空格或e'

[root@test: /tmp]# egrep '[^ e]f' a.txt

If you work with text,

| (管道符)

匹配管道符分隔的任一字符串

col(o|ou)r 匹配 "color", "colour"

 

- (連字符)

指定某個范圍內的字符一般是[a-z],[A-Z],[1-9],[a-zA-Z1-9]

a[a-z]c 匹配 "abc", "acc", "adc"

\ (反斜線)

轉義符,將特殊符合轉義為符號本身

a\*c 匹配 "a*c".

\n, \r, \t

代表 換行,回車,制表符

 

\b...\b

匹配整個單詞

\bTech\b 匹配 the word "Tech" in "Make Tech Easier".

找到單詞time

[root@test: /tmp]# egrep '\btime\b' a.txt

 from time

 to time.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM