国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > Python > 正文

python如何使用正則表達(dá)式的前向、后向搜索及前向搜索否定模式詳解

2020-01-04 16:29:20
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

前言

在許多的情況下,很多要匹配內(nèi)容是一起出現(xiàn),或者一起不出現(xiàn)的。比如《》,< >,這樣的括號(hào),不存在使用半個(gè)的情況。因此,在python/266919.html">正則表達(dá)式里也有一致性的判斷,要么兩個(gè)尖括號(hào)一起出現(xiàn),要么一個(gè)也不要出現(xiàn)。怎么樣來(lái)實(shí)現(xiàn)這種判斷呢?針對(duì)這種情況得引入新的正則表達(dá)式的語(yǔ)法:(?=pattern),這個(gè)語(yǔ)法它會(huì)向前搜索或者向后搜索相關(guān)內(nèi)容,如果不會(huì)出現(xiàn)就不能匹配。不過(guò),這個(gè)匹配不會(huì)消耗任何輸入的字符,它只是查看一下。

例子如下:

#python 3.6 #蔡軍生 #http://blog.csdn.net/caimouse/article/details/51749579 # import re  address = re.compile(  '''''  # A name is made up of letters, and may include "."  # for title abbreviations and middle initials.  ((?P<name>   ([/w.,]+/s+)*[/w.,]+   )   /s+  ) # name is no longer optional   # LOOKAHEAD  # Email addresses are wrapped in angle brackets, but only  # if both are present or neither is.  (?= (<.*>$)  # remainder wrapped in angle brackets   |   ([^<].*[^>]$) # remainder *not* wrapped in angle brackets   )   <? # optional opening angle bracket   # The address itself: username@domain.tld  (?P<email>   [/w/d.+-]+  # username   @   ([/w/d.]+/.)+ # domain name prefix   (com|org|edu) # limit the allowed top-level domains  )   >? # optional closing angle bracket  ''',  re.VERBOSE)  candidates = [  u'First Last <first.last@example.com>',  u'No Brackets first.last@example.com',  u'Open Bracket <first.last@example.com',  u'Close Bracket first.last@example.com>', ]  for candidate in candidates:  print('Candidate:', candidate)  match = address.search(candidate)  if match:   print(' Name :', match.groupdict()['name'])   print(' Email:', match.groupdict()['email'])  else:   print(' No match') 

結(jié)果輸出如下:

Candidate: First Last <first.last@example.com> Name : First Last Email: first.last@example.comCandidate: No Brackets first.last@example.com Name : No Brackets Email: first.last@example.comCandidate: Open Bracket <first.last@example.com No matchCandidate: Close Bracket first.last@example.com> No match

python里使用正則表達(dá)式的前向搜索否定模式

上面學(xué)習(xí)前向搜索或后向搜索模式(?=pattern),這個(gè)模式里看到有等于號(hào)=,它是表示一定相等,其實(shí)前向搜索模式里,還有不相等的判斷。比如你需要識(shí)別EMAIL地址:noreply@example.com,這個(gè)EMAIL地址大多數(shù)是不需要回復(fù)的,所以我們要把這個(gè)EMAIL地址識(shí)別出來(lái),并且丟掉它。怎么辦呢?這時(shí)你就需要使用前向搜索否定模式,它的語(yǔ)法是這樣:(?!pattern),這里的感嘆號(hào)就是表示非,不需要的意思。比如遇到這樣的字符串:noreply@example.com,它會(huì)判斷noreply@是否相同,如果相同,就丟掉這個(gè)模式識(shí)別,不再匹配。

例子如下:

#python 3.6 #蔡軍生 #http://blog.csdn.net/caimouse/article/details/51749579 # import re  address = re.compile(  '''''  ^   # An address: username@domain.tld   # Ignore noreply addresses  (?!noreply@.*$)   [/w/d.+-]+  # username  @  ([/w/d.]+/.)+ # domain name prefix  (com|org|edu) # limit the allowed top-level domains   $  ''',  re.VERBOSE)  candidates = [  u'first.last@example.com',  u'noreply@example.com', ]  for candidate in candidates:  print('Candidate:', candidate)  match = address.search(candidate)  if match:   print(' Match:', candidate[match.start():match.end()])  else:   print(' No match') 

結(jié)果輸出如下:

Candidate: first.last@example.com Match: first.last@example.comCandidate: noreply@example.com No match

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)VEVB武林網(wǎng)的支持。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到python教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 漳浦县| 四会市| 宜都市| 张家口市| 德昌县| 东兴市| 白银市| 南靖县| 邢台县| 福安市| 大足县| 翼城县| 杂多县| 罗山县| 曲松县| 太湖县| 通城县| 都江堰市| 陈巴尔虎旗| 临漳县| 昌宁县| 格尔木市| 罗山县| 当涂县| 兰西县| 新营市| 汝城县| 肃宁县| 墨竹工卡县| 林甸县| 日照市| 鄂温| 日土县| 和平县| 彰化市| 抚顺县| 池州市| 洛川县| 鲁甸县| 科技| 秦安县|