導航:首頁 > 編程語言 > python正則表達式

python正則表達式

發布時間:2022-01-13 05:18:38

A. python正則表達式

group和groups是兩個不同的函數。

一般,m.group(N) 返回第N組括弧匹配的字元。
而m.group() == m.group(0) == 所有匹配的字元,與括弧無關,這個是API規定的。

m.groups() 返回所有括弧匹配的字元,以tuple格式。
m.groups() == (m.group(0), m.group(1), ...)

對你給的例子:

m = re.match("([abc])+", "abc")
你的+號在括弧外面。括弧最多匹配到一個字元,要麼是a, 要麼是c,這個python引擎匹配的是末尾的c。
而m.group() == m.group(0) 這個返回的是整個匹配的字元串"abc".

關於捕獲型括弧在正則表達式里的用法,參見相關文檔。
參見http://..com/link?url=CltRBzI_-_jFl88a

B. python中的正則表達式中的 "|"

Python中re.findall()函數是要求正則表達式在捕獲第0組數據時,要在正則表達式上加小括弧才能捕獲.

也就是說如果你要獲取整個正則表達式匹配的數據(你這里是電子郵箱地址),需要在正則表達式外面加小括弧,

然後取第0捕獲組的數據(你這里是[x[0] for x in zhengze]),

因為findall函數把每一個匹配的多個捕獲組(就是你正則表達式中的小括弧中)的數據放到一個元組里,所以要用for循環把第0捕獲組的數據取出來.

具體程序改進如下

>>>zhengze=re.findall("([A-Za-z0-9]+@(163|qq|gmail).com)",txt)

>>>[x[0]forxinzhengze]

結果就是你要的郵箱列表了.

C. python 正則表達式 (.*)

groups()返回所有捕獲組構成的tuple。你的正則表達式中有唯一一個捕獲組(.*?),而?在此處表示非貪婪匹配,即在整個正則表達式成立的前提下匹配盡可能少的字元,此處最少的情況是什麼也不匹配,整個正則表達式匹配Python中的Py,而捕獲組自然為空字元串。

D. python正則表達式re.findall(r"\b\w+\b", s)中的r是什麼意思

在Python的string前面加上『r』, 是為了告訴編譯器這個string是個raw string,不要轉意backslash '' 。 例如, 在raw string中,是兩個字元,和n, 而不會轉意為換行符。由於正則表達式和 會有沖突,因此,當一個字元串使用了正則表達式後,最好在前面加上'r'。

例:r" 」

作用:聲明後面的字元串是普通字元串

特殊字元串中含有:轉義字元 什麼什麼的

用途:一般用在 正則表達式、文件絕對地址

1,正則表達式:

這樣就不用專門的去處理引號之中的特殊字元了

E. python正則表達式基礎

下面列出: 1.測試正則表達式是否匹配字元串的全部或部分regex=ur"" #正則表達式
if re.search(regex, subject): do_something()else: do_anotherthing() 2.測試正則表達式是否匹配整個字元串 regex=ur"/Z" #正則表達式末尾以/Z結束
if re.match(regex, subject): do_something()else: do_anotherthing() 3.創建一個匹配對象,然後通過該對象獲得匹配細節(Create an object with details about how the regex matches (part of) a string) regex=ur"" #正則表達式
match = re.search(regex, subject)if match: # match start: match.start() # match end (exclusive): atch.end() # matched text: match.group() do_something()else: do_anotherthing() 4.獲取正則表達式所匹配的子串(Get the part of a string matched by the regex) regex=ur"" #正則表達式
match = re.search(regex, subject)if match: result = match.group()else: result ="" 5. 獲取捕獲組所匹配的子串(Get the part of a string matched by a capturing group) regex=ur"" #正則表達式
match = re.search(regex, subject)if match: result = match.group(1)else: result ="" 6. 獲取有名組所匹配的子串(Get the part of a string matched by a named group) regex=ur"" #正則表達式
match = re.search(regex, subject)if match:result = match.group"groupname")else:result = "" 7. 將字元串中所有匹配的子串放入數組中(Get an array of all regex matches in a string) result = re.findall(regex, subject) 8.遍歷所有匹配的子串(Iterate over all matches in a string) for match in re.finditer(r"<(.*?)/s*.*?//1>", subject) # match start: match.start() # match end (exclusive): atch.end() # matched text: match.group() 9.通過正則表達式字元串創建一個正則表達式對象(Create an object to use the same regex for many operations) reobj = re.compile(regex) 10.用法1的正則表達式對象版本(use regex object for if/else branch whether (part of) a string can be matched) reobj = re.compile(regex)if reobj.search(subject): do_something()else: do_anotherthing() 11.用法2的正則表達式對象版本(use regex object for if/else branch whether a string can be matched entirely) reobj = re.compile(r"/Z") #正則表達式末尾以/Z 結束
if reobj.match(subject): do_something()else: do_anotherthing() 12.創建一個正則表達式對象,然後通過該對象獲得匹配細節(Create an object with details about how the regex object matches (part of) a string) reobj = re.compile(regex) match = reobj.search(subject)if match: # match start: match.start() # match end (exclusive): atch.end() # matched text: match.group() do_something()else: do_anotherthing() 13.用正則表達式對象獲取匹配子串(Use regex object to get the part of a string matched by the regex) reobj = re.compile(regex) match = reobj.search(subject)if match: result = match.group()else: result ="" 14.用正則表達式對象獲取捕獲組所匹配的子串(Use regex object to get the part of a string matched by a capturing group) reobj = re.compile(regex) match = reobj.search(subject)if match: result = match.group(1)else: result ="" 15.用正則表達式對象獲取有名組所匹配的子串(Use regex object to get the part of a string matched by a named group) reobj = re.compile(regex) match = reobj.search(subject)if match: result = match.group("groupname")else: result ="" 16.用正則表達式對象獲取所有匹配子串並放入數組(Use regex object to get an array of all regex matches in a string) reobj = re.compile(regex) result = reobj.findall(subject) 17.通過正則表達式對象遍歷所有匹配子串(Use regex object to iterate over all matches in a string) reobj = re.compile(regex)for match in reobj.finditer(subject): # match start: match.start() # match end (exclusive): match.end() # matched text: match.group()字元串替換 1.替換所有匹配的子串 #用newstring替換subject中所有與正則表達式regex匹配的子串
result = re.sub(regex, newstring, subject) 2.替換所有匹配的子串(使用正則表達式對象) reobj = re.compile(regex) result = reobj.sub(newstring, subject) 字元串拆分 1.字元串拆分 result = re.split(regex, subject) 2.字元串拆分(使用正則表示式對象) reobj = re.compile(regex) result = reobj.split(subject)

F. python 正則表達式.* 是什麼意思 詳細解釋

分開來說:
1、. 匹配任意除換行符「\n」外的字元;
2、*表示匹配前一個字元0次或無限次;

3、+或*後跟?表示非貪婪匹配,即盡可能少的匹配,如*?重復任意次,但盡可能少重復;
4、 .*? 表示匹配任意數量的重復,但是在能使整個匹配成功的前提下使用最少的重復。
如:a.*?b匹配最短的,以a開始,以b結束的字元串。如果把它應用於aabab的話,它會匹配aab和ab。

G. 想請教python 正則表達式

這你就需要了解正則表達式的貪婪匹配和非貪婪匹配

在此例中

#.*是貪婪匹配模式,所謂貪婪匹配就是在整個表達式匹配成功的前提下,盡可能多的匹配,
#也就是所謂的「貪婪」,通俗點講,就是看到想要的,有多少就撿多少,除非再也沒有想要的了。


#.*?是非貪婪模式,所謂非貪婪模式就是在整個表達式匹配成功的前提下,盡可能少的匹配,
#也就是所謂的「非貪婪」,通俗點講,就是找到一個想要的撿起來就行了,
#至於還有沒有沒撿的就不管了

#舉個例子
#字元串"abcdccd"
#那麼"a.*"匹配的是"abcdccd",貪婪模式,從a開始的我都要了
#"a.*?"匹配的是"a",非貪婪模式,滿足條件的情況下,我只要a就行了
#"a.*?d"匹配的是"abcd",非貪婪模式,我只要從a開始最快到d的字元串就行了
#"a.*d"匹配的是"abcdccd",貪婪模式,我只要從a開始,到最遠d的就可以了

不知道這樣講解時否明白

H. python 正則表達式

正則語法:

top|123

效果:

I. python 判斷正則表達式

看了你的提問,你的要求是:

輸入格式:

輸入包含兩行:

  1. 待匹配字元串

  2. 正則表達式

輸出格式:

若正則表達式能夠匹配第一行字元串則輸出True,否則,輸出False

以下是我依據你的功能需求,個人簡單寫的一些代碼,供你參考:

importre

flg=True
#定義主要工作代碼函數
defjobCode(txtstr,regex):
result=re.search(regex,txtstr)
#如果匹配第一行字元串flg為True,否則flg為False
ifresult.group()==txtstr:
#print(result.group())
returnflg==True#返回flg並終止循環
else:
#print(result.group())
returnflg==False#返回flg並終止循環

#程序主入口
if__name__=='__main__':
txtstr=str(input("請輸入待匹配的字元串:"))
regex=input("請輸入正則表達式:")
print(jobCode(txtstr,regex))#調用定義函數jobCode()

代碼應該還能更簡潔,具體你自己去完善。

純手工,如果對你有幫助望採納!

閱讀全文

與python正則表達式相關的資料

熱點內容
pdf製作技巧 瀏覽:608
免費解壓啤酒視頻 瀏覽:38
貴州雲存儲伺服器 瀏覽:555
高中生做程序員的規劃 瀏覽:811
領克app怎麼綁定車輛別人的車 瀏覽:637
外語教學pdf 瀏覽:38
程序員釋義 瀏覽:249
數控g71編程時應注意什麼 瀏覽:411
捷聯慣導演算法心得 瀏覽:144
c4d命令的理解 瀏覽:568
pdf文檔水印 瀏覽:917
高斯模糊演算法java 瀏覽:354
小學樂高機器人編程作品 瀏覽:522
小猿搜題app怎麼使用 瀏覽:420
內孔左螺紋編程 瀏覽:893
怎麼查找程序員信息 瀏覽:538
adb日誌導出到本地的命令 瀏覽:717
手機微信壓縮包 瀏覽:263
坐高鐵應下什麼app 瀏覽:529
命令行查找文件夾 瀏覽:389