自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Python正則表達(dá)式十種相關(guān)的匹配方法

開發(fā) 后端
Python正則表達(dá)式需要我們不斷的學(xué)習(xí),在學(xué)習(xí)中我們會遇到相關(guān)匹配的問題。下面我們就看看在實(shí)際操作中十種經(jīng)常遇到的匹配方法。

Python正則表達(dá)式需要各種各樣的匹配,但是我們不能盲目的進(jìn)行相匹配,下面就向大家介紹經(jīng)常遇到的十種Python正則表達(dá)式匹配方式,希望大家有所收獲。

1.測試Python正則表達(dá)式是否 匹配字符串的全部或部分

  1. regex=ur"..." #正則表達(dá)式  
  2. if re.search(regex, subject):  
  3. do_something()  
  4. else:  
  5. do_anotherthing() 

2.測試Python正則表達(dá)式是否匹配整個字符串

  1. regex=ur"...\Z" #正則表達(dá)式末尾以\Z結(jié)束  
  2. if re.match(regex, subject):  
  3. do_something()  
  4. else:  
  5. do_anotherthing() 

3. 創(chuàng)建一個匹配對象,然后通過該對象獲得匹配細(xì)節(jié)

  1. regex=ur"..." #正則表達(dá)式  
  2. match = re.search(regex, subject)  
  3. if match:  
  4. # match start: match.start()  
  5. # match end (exclusive): match.end()  
  6. # matched text: match.group()  
  7. do_something()  
  8. else:  
  9. do_anotherthing() 

4.獲取Python正則表達(dá)式所匹配的子串

 

  1. regex=ur"..." #正則表達(dá)式  
  2. match = re.search(regex, subject)  
  3. if match:  
  4. result = match.group()  
  5. else:  
  6. result = "" 

5. 獲取捕獲組所匹配的子串

 

  1. regex=ur"..." #正則表達(dá)式  
  2. match = re.search(regex, subject)  
  3. if match:  
  4. result = match.group(1)  
  5. else:  
  6. result = "" 

6. 獲取有名組所匹配的子串

 

  1. regex=ur"..." #正則表達(dá)式  
  2. match = re.search(regex, subject)  
  3. if match:  
  4. result = match.group("groupname")  
  5. else:  
  6. result = "" 

7. 將字符串中所有匹配的子串放入數(shù)組中

 

  1. reresult = re.findall(regex, subject) 

8.遍歷所有匹配的子串

  1. (Iterate over all matches in a string)  
  2. for match in re.finditer(r"<(.*?)\s*.*?/\1>", subject)  
  3. # match start: match.start()  
  4. # match end (exclusive): match.end()  
  5. # matched text: match.group() 

 

9.通過Python正則表達(dá)式 字符串創(chuàng)建一個正則表達(dá)式對象

  1. (Create an object to use the same regex for many 
    operations)  
  2. rereobj = re.compile(regex) 

 

10.用法1的Python正則表達(dá)式對象版本

  1. rereobj = re.compile(regex)  
  2. if reobj.search(subject):  
  3. do_something()  
  4. else:  
  5. do_anotherthing() 

以上就是對Python正則表達(dá)式相關(guān)問題匹配的解決方案。

【編輯推薦】

  1. 關(guān)于Python腳本語言進(jìn)行學(xué)習(xí)介紹
  2. Python腳本在游戲中尋找自己的知音
  3. Python腳本在VIM環(huán)節(jié)中的系統(tǒng)介紹
  4. 利用Python腳本自動生成相應(yīng)文件的解決方案
  5. Python腳本解決在游戲開發(fā)中的困難
責(zé)任編輯:張浩 來源: IT168
相關(guān)推薦

2010-03-10 18:57:53

Python正則表達(dá)式

2024-09-14 09:18:14

Python正則表達(dá)式

2009-09-16 16:22:04

正則表達(dá)式匹配

2010-03-15 16:21:28

Python正則表達(dá)式

2009-11-30 17:22:24

PHP正則表達(dá)式多行匹

2009-09-16 18:08:14

正則表達(dá)式匹配單詞

2009-09-16 13:24:30

PHP正則表達(dá)式匹配

2018-09-27 15:25:08

正則表達(dá)式前端

2021-01-27 11:34:19

Python正則表達(dá)式字符串

2009-08-14 17:44:46

C#中使用正則表達(dá)式匹

2010-03-18 12:40:47

python正則表達(dá)式

2010-03-25 18:25:36

Python正則表達(dá)式

2021-12-03 08:50:25

LeetCode正則表達(dá)式算法

2009-09-16 16:48:03

正則表達(dá)式匹配數(shù)字

2020-09-04 09:16:04

Python正則表達(dá)式虛擬機(jī)

2010-03-04 15:20:20

Ubuntu Patt

2009-09-16 13:53:17

PHP正則表達(dá)式匹配

2010-03-01 15:51:59

Python則表達(dá)式

2010-03-11 08:55:45

python正則表達(dá)式

2019-12-10 10:40:57

Python正則表達(dá)式編程語言
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號