Shell下的正则表达式 (鸟哥私房菜)

时间:2023-03-09 16:10:48
Shell下的正则表达式 (鸟哥私房菜)
 "Open Source" is a good mechanism to develop programs.$
apple is my favorite food.$
Football game is not use feet only.$
this dress doesn't fit me.$
However, this dress is about $ dollars.^M$
GNU is free air not free beer.^M$
Her hair is very beauty.^M$
I can't finish the test.^M$
Oh! The soup taste good.^M$
motorcycle is cheap than car.$
This window is clear.$
the symbol '*' is represented as start.$
Oh!^IMy god!$
The gd software is a library for drafting programs.^M$
You are the best is mean you are the no. .$
The world <Happy> is the same with "glad".$
I like dog.$
google is the best tools for search keyword.$
goooooogle yes!$
go! go! Let's go.$
# I am VBird$
$

一. 搜寻特定字符串

'the'

二. 利用中括号[]搜寻集合字符

't[ae]st'

'oo'

'[^g]oo'

'[^a-z]oo' 或者 '[^[:lower:]]oo'

'[0-9a-zA-Z]oo'

'[0-9]' 或者 '[[:digit:]]'

三. 行首^与行尾字符$

'^the'

'^[a-z]' 或者 '^[[:lower:]]'

'^[^[:alpha:]]'

'\.$'

'^$'

四. 任意一个字符.与重复字符*

'g..d'

'o*'

'oo*'

'ooo*'

'goo*g'

'g.*g'

'[0-9][0-9]*' 或 '[[:digit:]][[:digit:]]*'

五. 限定连续RE字符范围{}

'a\{2\}'

'go\{2,5\}g'

'go\{2,\}g'

六. 锚定词首词尾\b\b或\<\> (来自马哥)

'\bWORD\b' 或 '\<WORD\>'

七. 后向引用

\1: 第一个左括号以及与之对应的右括号所包含的所有内容

以下为扩展型正则表达式, 需配合egrep使用

Shell下的正则表达式 (鸟哥私房菜)