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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python正则re

發布時間:2024/10/12 python 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python正则re 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
import re
line = "Catsaresmarterthandogs"
matchObj = re.match( r'(.*)are(\w{2})(.*)', line, re.M|re.I)
if matchObj:
print ("matchObj.group() : ", matchObj.group())#group返回匹配的整個字符串,groups返回匹配各分組的元祖
print ("matchObj.group(1) : ", matchObj.group(1))#span(1)返回匹配第一個分組的起始索引
print ("matchObj.group(2) : ", matchObj.group(2))
print ("matchObj.group(2) : ", matchObj.group(3))
else:
print ("No match!!")

matchObj.group() : Catsaresmarterthandogs
matchObj.group(1) : Cats
matchObj.group(2) : sm
matchObj.group(2) : arterthandogs

search用法一樣 ————————————————————————————————————————————————————————————————————————————
re.match只匹配字符串的開始,如果字符串開始不符合正則表達式,則匹配失敗,函數返回None;
而re.search匹配整個字符串,直到找到一個匹配。match 和 search 是匹配一次 findall 匹配所有
num = re.sub(r'#.*$', "", str) re.sub(替換規則,替換成xx,操作的字符串);用于替換字符串中的匹配項

—————————————————————————————————————————————————————————————— # 將匹配的數字乘以 2
import re
# 將匹配的數字乘以 2
def double(matched):
value = int(matched.group('value'))
return str(value * 2)
s = 'A23G4HFD567'
print(re.sub('(?P<value>\d+)', double, s))#A46G8HFD1134
——————————————————————————————————————————————————————————————————
re.finditer(r"\d+","12a32bc43jf3") 返回匹配結果為一個迭代器
re.split('\W+', 'w3cschool, w3cschool, w3cschool.')分割匹配結果為一個列表
————————————————————————————————————
[^...] [^abc] 匹配除了a,b,c之外的字符[^0-9] 匹配除了數字外的字符

轉載于:https://www.cnblogs.com/qinyios/p/10016706.html

總結

以上是生活随笔為你收集整理的python正则re的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。