成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

Python正則表達式如何匹配子串

開發 后端
Python正則表達式在進行子串匹配的時候需要注意很多的問題,首先我們來看看如何獲取Python正則表達式中匹配的子串。

Python正則表達式有很多的時候需要我們進行子串的匹配,不單在進行替換的時候需要進行,在很多的地方都會用到這些代碼。下面我們就來詳細的學習如何用Python正則表達式獲取自己想得到的匹配子串。

獲取Python正則表達式所匹配的子串(Get the part of a string matched by the regex)

 

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

獲取捕獲組所匹配的子串(Get the part of a string matched by a capturing group)

 

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

獲取有名組所匹配的子串(Get the part of a string matched by a named group)

 

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

將字符串中所有匹配的子串放入數組中(Get an array of all regex matches in a string)

 

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

遍歷所有匹配的子串(Iterate over all matches in a string)

 

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

通過Python正則表達式 字符串創建一個正則表達式對象(Create an object to use the same regex for many operations)

 

  1. rereobj = re.compile(regex) 

用法1的Python正則表達式對象版本(use regex object for if/else branch whether (part of) a string can be matched)

 

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

用法2的Python正則表達式對象版本(use regex object for if/else branch whether a string can be matched entirely)

 

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

以上就是對Python正則表達式在獲取匹配子串應用的介紹。

【編輯推薦】

  1. Python字符串替換如何才能進行字符的拆分
  2. Python文本亂碼發生時的解決方案
  3. Python編程語言總體性能優點評測
  4. Python編程語言具有相當高的適應能力
  5. Python編程語言維和受到眾人的追捧
責任編輯:張浩 來源: CSDN
相關推薦

2009-09-16 16:22:04

正則表達式匹配

2009-09-16 17:02:15

正則表達式匹配字符串

2009-09-16 18:08:14

正則表達式匹配單詞

2010-07-21 10:43:25

Perl正則表達式匹配

2024-09-14 09:18:14

Python正則表達式

2021-01-27 11:34:19

Python正則表達式字符串

2010-03-10 18:57:53

Python正則表達式

2010-03-25 18:25:36

Python正則表達式

2018-09-27 15:25:08

正則表達式前端

2020-09-04 09:16:04

Python正則表達式虛擬機

2010-03-04 15:20:20

Ubuntu Patt

2009-09-16 13:24:30

PHP正則表達式匹配

2021-12-03 08:50:25

LeetCode正則表達式算法

2009-09-16 16:48:03

正則表達式匹配數字

2021-05-25 09:18:04

正則表達式Linux字符串

2009-08-14 17:44:46

C#中使用正則表達式匹

2010-03-01 15:51:59

Python則表達式

2010-03-11 08:55:45

python正則表達式

2019-12-10 10:40:57

Python正則表達式編程語言

2024-12-25 15:09:38

Python字符串函數
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 成人av一区二区三区 | 久草视频在线播放 | 在线欧美一区 | 一区二区三区四区在线播放 | 中文字幕日韩专区 | 中文字幕丁香5月 | 一级黄色毛片免费 | 国产精品久久久久久久久久久久久久 | 日韩成人av在线 | 日本精品免费在线观看 | 亚洲精品永久免费 | 亚洲国产日韩欧美 | www.日本在线 | 成年人免费看 | 亚洲一区久久 | 精品国产一区二区三区性色av | 成人精品一区 | 国产精品国产三级国产aⅴ中文 | 男人亚洲天堂 | 国产综合久久久久久鬼色 | 欧美中文字幕在线观看 | 天天碰夜夜操 | 亚洲精品乱码久久久久久按摩 | 国产一区二区精 | 91精品国产综合久久久久久 | 不卡一区二区三区四区 | 欧美精品一区二区在线观看 | 欧美在线观看一区 | 精品国产一区二区久久 | 国产精品一区三区 | 久久精品欧美一区二区三区麻豆 | 午夜影晥| 一级看片免费视频囗交动图 | 成人在线中文字幕 | 精品国产免费一区二区三区演员表 | 99久久免费精品 | 国产精品毛片无码 | 99福利视频 | 女同久久另类99精品国产 | 久久久九九 | 日韩毛片在线视频 |