日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

python正则表达式匹配模式屠夫之桥_Python 编程快速上手 第 7章 模式匹配与正则表达式...

發(fā)布時(shí)間:2024/1/23 python 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python正则表达式匹配模式屠夫之桥_Python 编程快速上手 第 7章 模式匹配与正则表达式... 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1 >>> consonantRegex = re.compile(r'[^aeiouAEIOU]')2 >>> consonantRegex.findall('RoboCop eats baby food. BABY FOOD.')3 ['R', 'b', 'c', 'p', ' ', 't', 's', ' ', 'b', 'b', 'y', ' ', 'f', 'd', '.', '4 ','B','B','Y',' ','F','D','.']

7.1 不用正則表達(dá)式來(lái)查找文本模式

7.2 用正則表達(dá)式查找文本模式

正則表達(dá)式,簡(jiǎn)稱為 regex,是文本模式的描述方法

7.2.1 創(chuàng)建正則表達(dá)式對(duì)象

7.2.2 匹配 Regex 對(duì)象

1 >>> phoneNumRegex = re.compile(r'\d\d\d-\d\d\d-\d\d\d\d')2 >>> mo = phoneNumRegex.search('My number is 415-555-4242.')3 >>> print('Phone number found:' +mo.group())4

5

6 Phone number found: 415-555-4242

7.2.3 正則表達(dá)式匹配復(fù)習(xí)

1.用 import re 導(dǎo)入正則表達(dá)式模塊。

2.用 re.compile()函數(shù)創(chuàng)建一個(gè) Regex 對(duì)象(記得使用原始字符串)。

3.向 Regex 對(duì)象的 search()方法傳入想查找的字符串。它返回一個(gè) Match 對(duì)象。

4.調(diào)用 Match 對(duì)象的 group()方法,返回實(shí)際匹配文本的字符串。

7.3 用正則表達(dá)式匹配更多模式

7.3.1 利用括號(hào)分組

>>> phoneNumRegex = re.compile(r'(\d\d\d)-(\d\d\d-\d\d\d\d)')>>> mo = phoneNumRegex.search('My number is 415-555-4242.')>>> mo.group(1)'415'

>>> mo.group(2)'555-4242'

>>> mo.group(0)'415-555-4242'

>>>mo.group()'415-555-4242'#如果想要一次就獲取所有的分組,請(qǐng)使用 groups()方法,注意函數(shù)名的復(fù)數(shù)形式>>>mo.groups()

('415', '555-4242')>>> areaCode, mainNumber =mo.groups()>>>print(areaCode)415

>>>print(mainNumber)555-4242

7.3.2 用管道匹配多個(gè)分組

1 >>> heroRegex = re.compile (r'Batman|Tina Fey')2 >>> mo1 = heroRegex.search('Batman and Tina Fey.')3 >>>mo1.group()4 'Batman'

5 >>> mo2 = heroRegex.search('Tina Fey and Batman.')6 >>>mo2.group()7 'Tina Fey'

8 #Batman 和 Tina Fey 都出現(xiàn)在被查找的字符串中,第一次出現(xiàn)的匹配文本,9 將作為 Match 對(duì)象返回。

1 >>> batRegex = re.compile(r'Bat(man|mobile|copter|bat)')2 >>> mo = batRegex.search('Batmobile lost a wheel')3 >>>mo.group()4 'Batmobile'

5 >>> mo.group(1)6 'mobile'

7.3.3 用問(wèn)號(hào)實(shí)現(xiàn)可選匹配

1 >>> phoneRegex = re.compile(r'(\d\d\d-)?\d\d\d-\d\d\d\d')2 >>> mo1 = phoneRegex.search('My number is 415-555-4242')3 >>>mo1.group()4 '415-555-4242'

5 >>> mo2 = phoneRegex.search('My number is 555-4242')6 >>>mo2.group()7 '555-4242'

7.3.4 用星號(hào)匹配零次或多次

1 >>> batRegex = re.compile(r'Bat(wo)*man')2 >>> mo1 = batRegex.search('The Adventures of Batman')3 >>>mo1.group()4 'Batman'

5 >>> mo2 = batRegex.search('The Adventures of Batwoman')6 >>>mo2.group()7 'Batwoman'

8 >>> mo3 = batRegex.search('The Adventures of Batwowowowoman')9 >>>mo3.group()10 'Batwowowowoman'

7.3.5 用加號(hào)匹配一次或多次

7.3.6 用花括號(hào)匹配特定次數(shù)

1 >>> haRegex = re.compile(r'(Ha){3}')2 >>> mo1 = haRegex.search('HaHaHa')3 >>>mo1.group()4 'HaHaHa'

5 >>> mo2 = haRegex.search('Ha')6 >>> mo2 ==None7 True

7.4 貪心和非貪心匹配

1 >>> greedyHaRegex = re.compile(r'(Ha){3,5}')2 >>> mo1 = greedyHaRegex.search('HaHaHaHaHa')3 >>>mo1.group()4 'HaHaHaHaHa'

5 >>> nongreedyHaRegex = re.compile(r'(Ha){3,5}?')6 >>> mo2 = nongreedyHaRegex.search('HaHaHaHaHa')7 >>>mo2.group()8 'HaHaHa'

7.5 findall()方法

1 >>> phoneNumRegex = re.compile(r'\d\d\d-\d\d\d-\d\d\d\d') # has no groups2 >>> phoneNumRegex.findall('Cell: 415-555-9999 Work: 212-555-0000')3 ['415-555-9999', '212-555-0000']

如果在正則表達(dá)式中有分組,那么 findall 將返回元組的列表

1 >>> phoneNumRegex = re.compile(r'(\d\d\d)-(\d\d\d)-(\d\d\d\d)') # has groups2 >>> phoneNumRegex.findall('Cell: 415-555-9999 Work: 212-555-0000')3 [('415', '555', '1122'), ('212', '555', '0000')]

7.7 建立自己的字符分類(lèi)

字符分類(lèi)[aeiouAEIOU]將匹配所有元音字 符,不論大小寫(xiě)

1 >>> vowelRegex = re.compile(r'[aeiouAEIOU]')2 >>> vowelRegex.findall('RoboCop eats baby food. BABY FOOD.')3 ['o', 'o', 'o', 'e', 'a', 'a', 'o', 'o', 'A', 'O', 'O']

通過(guò)在字符分類(lèi)的左方括號(hào)后加上一個(gè)插入字符(^),就可以得到“非字符類(lèi)”。 非字符類(lèi)將匹配不在這個(gè)字符類(lèi)中的所有字符

1 >>> consonantRegex = re.compile(r'[^aeiouAEIOU]')2 >>> consonantRegex.findall('RoboCop eats baby food. BABY FOOD.')3 ['R', 'b', 'c', 'p', ' ', 't', 's', ' ', 'b', 'b', 'y', ' ', 'f', 'd', '.', '4 ','B','B','Y',' ','F','D','.']

7.8 插入字符和美元字符

正則表達(dá)式 r'^Hello'匹配以'Hello'開(kāi)始的字符串。

1 >>> beginsWithHello = re.compile(r'^Hello')2 >>> beginsWithHello.search('Hello world!')3 <_sre.sre_match object span="(0," match="Hello">

4 >>> beginsWithHello.search('He said hello.') ==None5 True

正則表達(dá)式 r'\d$'匹配以數(shù)字 0 到 9 結(jié)束的字符串

1 >>> endsWithNumber = re.compile(r'\d$')2 >>> endsWithNumber.search('Your number is 42')3 <_sre.sre_match object span="(16," match="2">

4 >>> endsWithNumber.search('Your number is forty two.') ==None5 True

正則表達(dá)式 r'^\d+$'匹配從開(kāi)始到結(jié)束都是數(shù)字的字符串

1 >>> wholeStringIsNum = re.compile(r'^\d+$')2 >>> wholeStringIsNum.search('1234567890')3 <_sre.sre_match object span="(0," match="1234567890">

4 >>> wholeStringIsNum.search('12345xyz67890') ==None5 True6 >>> wholeStringIsNum.search('12 34567890') ==None7 True

總結(jié)

以上是生活随笔為你收集整理的python正则表达式匹配模式屠夫之桥_Python 编程快速上手 第 7章 模式匹配与正则表达式...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。