本文目錄一覽:
python 正則表達式,怎樣匹配以某個字元串開頭,以某個字元串結尾的情況?
匹配以某個字元串開頭,以某個字元串結尾的情況的正則表達式:^abc.*?qwe$
Python正則表達式的幾種匹配用法:
1.測試正則表達式是否匹配字元串的全部或部分
regex=ur”” #正則表達式
if re.search(regex, subject):
do_something()
else:
do_anotherthing()
2.測試正則表達式是否匹配整個字元串
regex=ur”/Z” #正則表達式末尾以/Z結束
if re.match(regex, subject):
do_something()
else:
do_anotherthing()
3.創建一個匹配對象,然後通過該對象獲得匹配細節(Create an object with details about how the regex matches (part of) a string)
regex=ur”” #正則表達式
match = re.search(regex, subject)
if match:
# match start: match.start()
# match end (exclusive): atch.end()
# matched text: match.group()
do_something()
else:
do_anotherthing()
4.獲取正則表達式所匹配的子串(Get the part of a string matched by the regex)
regex=ur”” #正則表達式
match = re.search(regex, subject)
if match:
result = match.group()
else:
result = “”
5. 獲取捕獲組所匹配的子串(Get the part of a string matched by a capturing group)
regex=ur”” #正則表達式
match = re.search(regex, subject)
if match:
result = match.group(1)
else:
result = “”
6. 獲取有名組所匹配的子串(Get the part of a string matched by a named group)
regex=ur”” #正則表達式
match = re.search(regex, subject)
if match:
result = match.group”groupname”)
else:
result = “”
7. 將字元串中所有匹配的子串放入數組中(Get an array of all regex matches in a string)
result = re.findall(regex, subject)
8.遍歷所有匹配的子串(Iterate over all matches in a string)
for match in re.finditer(r”(.*?)/s*.*?//1″, subject)
# match start: match.start()
# match end (exclusive): atch.end()
# matched text: match.group()
9.通過正則表達式字元串創建一個正則表達式對象(Create an object to use the same regex for many operations)
reobj = re.compile(regex)
10.用法1的正則表達式對象版本(use regex object for if/else branch whether (part of) a string can be matched)
reobj = re.compile(regex)
if reobj.search(subject):
do_something()
else:
do_anotherthing()
11.用法2的正則表達式對象版本(use regex object for if/else branch whether a string can be matched entirely)
reobj = re.compile(r”/Z”) #正則表達式末尾以/Z 結束
if reobj.match(subject):
do_something()
else:
do_anotherthing()
12.創建一個正則表達式對象,然後通過該對象獲得匹配細節(Create an object with details about how the regex object matches (part of) a string)
reobj = re.compile(regex)
match = reobj.search(subject)
if match:
# match start: match.start()
# match end (exclusive): atch.end()
# matched text: match.group()
do_something()
else:
do_anotherthing()
13.用正則表達式對象獲取匹配子串(Use regex object to get the part of a string matched by the regex)
reobj = re.compile(regex)
match = reobj.search(subject)
if match:
result = match.group()
else:
result = “”
14.用正則表達式對象獲取捕獲組所匹配的子串(Use regex object to get the part of a string matched by a capturing group)
reobj = re.compile(regex)
match = reobj.search(subject)
if match:
result = match.group(1)
else:
result = “”
15.用正則表達式對象獲取有名組所匹配的子串(Use regex object to get the part of a string matched by a named group)
reobj = re.compile(regex)
match = reobj.search(subject)
if match:
result = match.group(“groupname”)
else:
result = “”
16.用正則表達式對象獲取所有匹配子串並放入數組(Use regex object to get an array of all regex matches in a string)
reobj = re.compile(regex)
result = reobj.findall(subject)
17.通過正則表達式對象遍歷所有匹配子串(Use regex object to iterate over all matches in a string)
reobj = re.compile(regex)
for match in reobj.finditer(subject):
# match start: match.start()
# match end (exclusive): match.end()
# matched text: match.group()
python 正則如何從末尾匹配
c=re.match(‘.*?([0-9]+)$’, xyz)
print c.group(1)
把不需要的匹配去掉就可以了
python的正則表達式
1,正則表達式的一些內容
正則表達式主要是用來匹配文本中需要查找的內容,例如在一片文章中找出電話號碼,就中國的來說11位純數字(不說座機),則使用”\d{11}” 意味匹配數字11次,就能準確的查找出文本中的電話號碼. 還有就是在編寫網路爬蟲的時候需要提取很多超鏈接再次進行爬取,使用正則表達式就很方便.直接匹配http開頭就行,當然也可以使用beautifulsoup的select方法.
看下面的程序看看正則表達提取文本中的郵箱:
\w 匹配字母,數字,下劃線
+ 匹配1次或者多次
re是正則表達式的工具包,工具包出錯的話在anaconda的命令行輸入”pip install re”安裝,其他的工具包也是如此.
re.compile()中的r示意\不是轉義字元,也就是保持後面字元串原樣,findall返回一個列表.下面還有一個版本的程序略有不同.
compile的另一個參數re.IGONORECASE(忽略大小寫),還可以是re.DORALL,多行模式,具體功能也是模糊不清,不過在使用通配符 . 匹配的時候加上re.DOTALL參數能夠匹配換行.如果希望忽略大小寫和多行模式都開啟可以使用re.compile(r’….’,re.IGNORECASE|re.DOTALL) .
表達式使用( ),對匹配到的內容分為3組 也就是(\w+)出現字母,數字,下劃線一次或多次,這個分組就是下面使用match對象的grou()方法的時候的參數.不給參數和參數0都是得到整個匹配到的內容, 參數1得到第一個括弧匹配到的內容,以此類推參數2和3,如果沒有括弧分組的話使用參數會出現錯誤.
search( )查找和正則式匹配的內容,只匹一次後面的那個找不到.返回一個match對象
\w 匹配字母,數字,下劃線
\W 匹配字母,數字.下劃線之外的所有字元
\d 匹配數字
\D 匹配非數字
\s 匹配空格,製表符,換行符
\S匹配除空格製表符,換行符之外的其他字元
[ …. ]定義自己的匹配,如[aeiouAEIOU ]匹配所有的母音字母,注意不是匹配單詞.
{最少次數,最多次數},例如{3,9} 匹配3-9次,{ ,10}匹配0-10次. 默認為匹配最多次數(貪心匹配),非貪心模式在後面加上問號
? 可選 0次或者1次吧
+匹配1次或多次
*匹配0次或者多次
^ 判斷開頭 ^\d 如果待匹配串是數字開頭則返回第一個數字
$判斷結尾 \d$ 如果待匹配串是數字結尾則返回最後一個數字
. 通配符,匹配除換行之外的所有字元
\d{11} 匹配數字11次
. * 匹配所有字元除 換行
[a-zA-Z0-9._%+-] 小寫和大寫字母、數字、句點、下劃線、百分號、加號或短橫
[a-zA-Z]{2,4} 匹配字母 2 – 4次
原創文章,作者:WSLY,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/135831.html