正規表示式練習 實驗樓會員課程

2021-09-17 23:44:03 字數 2282 閱讀 8111

本次實驗將介紹正規表示式的一些基本概念和用法。

正規表示式的概念

特殊符號

課程使用的實驗環境為 ubuntu linux 14.04 64 位。實驗中會用到程式:

xfce終端

在做文書處理或編寫程式時,用到查詢、替換等功能,使用正規表示式能夠簡單快捷的完成目標。簡單而言,正規表示式通過一些特殊符號的幫助,使使用者可以輕鬆快捷的完成查詢、刪除、替換等處理程式。例如 grep, expr, sed , awk. 或 vi 中經常會使用到正規表示式,為了充分發揮 shell 程式設計的威力,需要精通正規表示式。正規表示法基本上是一種『表示法』, 只要工具程式支援這種表示法,那麼該工具程式就可以用來作為正規表示法的字串處理之用。 也就是說,例如 vi, grep, awk ,sed 等等工具,因為她們有支援正規表示法, 所以,這些工具就可以使用正規表示法的特殊字元來進行字串的處理。

[

:alnum:

]代表英文大小寫字母及數字

[:alpha:

]代表英文大小寫字母

[:blank:

]代表空格和 tab 鍵

[:cntrl:

]鍵盤上的控制按鍵,如 cr,lf,tab,del

[:digit:

]代表數字

[:graph:

]代表空白字元以外的其他

[:lower:

]小寫字母

[:print:

]可以被列印出來的任何字元

[:punct:

]代表標點符號

[:upper:

]代表大寫字元

[:space:

]任何會產生空白的字元如空格,tab,cr 等

[:xdigit:

]代表 16 進製的數字型別

任意建立乙個文字並編輯。或者以原有的文字做實驗皆可。

$  touch regular_express.txt

$ vi regular_express.txt

//或者 sudo gedit regular_express.txt

將文字內容貼上進regular_express.txt檔案

"open source" is a good mechanism to develop programs.

football game is not use feet only.

this dress doesn't fit me.

however,

this dress is about $ 3183 dollars.

gnu is free air not free beer.

her hair is very beauty.

i can't finish the test.

oh! the soup taste good.

motorcycle is cheap than car.

this window is clear.

the symbol '*' is represented as start.

oh!my god!

the gd software is a library for drafting programs.

you are the best is mean you are the no.

1. 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

查詢小寫字母

$ grep -n '[[:lower:]]' regular_express.txt
操作截圖(藍色為匹配成功的字元)

正規表示式練習

取出其中的參考文獻,注意到每行只有乙個參考文獻,所以直接用 re.search regex,line import re with open test2 r as f lines f.readlines regex re.compile r a z reg open refer.txt w for ...

正規表示式練習

1 匹配一段文字中的每行的郵箱 y 123 qq.comaaa 163.combbb 126.comasdfasfs33333 adfcom import reret re.findall w qq 163 126 com y print ret 123 qq.com aaa 163.com bbb...

正規表示式練習

字元描述 匹配前面的子表示式零次或多次。例如,zo 能匹配 z 以及 zoo 等價於。匹配前面的子表示式一次或多次。例如,zo 能匹配 zo 以及 zoo 但不能匹配 z 等價於 匹配前面的子表示式零次或一次。例如,do es 可以匹配 do does 中的 does doxy 中的 do 1 va...