偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

Python正則表達式十種相關的匹配方法

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

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

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

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

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

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

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

  1. regex=ur"..." #正則表達式  
  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正則表達式所匹配的子串

 

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

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

 

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

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

 

  1. regex=ur"..." #正則表達式  
  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正則表達式 字符串創(chuàng)建一個正則表達式對象

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

 

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

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

以上就是對Python正則表達式相關問題匹配的解決方案。

【編輯推薦】

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

2010-03-10 18:57:53

Python正則表達式

2024-09-14 09:18:14

Python正則表達式

2009-09-16 16:22:04

正則表達式匹配

2010-03-15 16:21:28

Python正則表達式

2009-11-30 17:22:24

PHP正則表達式多行匹

2009-09-16 18:08:14

正則表達式匹配單詞

2009-09-16 13:24:30

PHP正則表達式匹配

2018-09-27 15:25:08

正則表達式前端

2021-01-27 11:34:19

Python正則表達式字符串

2009-08-14 17:44:46

C#中使用正則表達式匹

2010-03-18 12:40:47

python正則表達式

2010-03-25 18:25:36

Python正則表達式

2020-09-04 09:16:04

Python正則表達式虛擬機

2021-12-03 08:50:25

LeetCode正則表達式算法

2009-09-16 16:48:03

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

2010-03-04 15:20:20

Ubuntu Patt

2009-09-16 13:53:17

PHP正則表達式匹配

2020-11-04 09:23:57

Python

2010-03-11 08:55:45

python正則表達式

2019-12-10 10:40:57

Python正則表達式編程語言
點贊
收藏

51CTO技術棧公眾號