第八课 正则表达式

末鹿安然 提交于 2020-02-09 17:32:48

正则表达式

示例1(新建名为grep.txt文件)

ooxx12121212ooxx
ooxx 12121212
oox 12121212
1212 ooxx 1212
oo3xx
oo4xx
ooWxx
oomxx
$ooxx
oo1234xx
ooxyzxx

键入如下命令

grep    "\(^[0-9]\|[^0-9][0-9]\)[0-9]\{2\}\([0-9][^0-9]\|[0-9]$\)"  grep.txt

grep -E  "^[0-9]{4}[^0-9]|[^0-9][0-9]{4}[^0-9]|[^0-9][0-9]{4}$|^[0-9]{4}$" grep.txt

示例2(新建test文件)

aaabbcaaa
aa bbc aaa
bb bbc bbb
asgodssgoodsssagodssgood
asgodssgoodsssagoodssgod
sdlkjflskdjf3slkdjfdksl
slkdjf2lskdjfkldsjl

键入如下指令

cat test 
   51  grep "a" test
   52  grep  "a\{3\}"  test
   53  grep  "\<aaa"  test
   54  grep  "\<aaa\>"  test
   55  grep "b" test
   56  grep  "b\{2,3\}" test 
   57  clear
   58  cat test
   59  grep "god" test
   60  grep "godgood" test
   61  grep "god*good" test
   62  grep "god.*good" test
   63  grep "god.*good.*god.*good" test
   64  grep "god.*good+" test
   65  grep "\(god.*good\)+" test
   66  grep "\(god\).*good.*\1" test
   67  grep "\(god\).*\(good\).*\1.*\2" test
   68  vi test 
   69  grep "\(god\).*\(good\).*\1.*\2" test
   70  grep "\(god\).*\(good\).*\2.*\1" test

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!