導航:首頁 > 編程語言 > python正則表達式多行匹配

python正則表達式多行匹配

發布時間:2022-07-19 18:42:11

1. 正則表達式如何滿足多行和跨行匹配

正則:

且加上多行的參數是:

python

完整代碼:

(網路知道的編輯器中竟然不能輸入代碼,鄙視👎之)

"""

Function;

正則表達式如何滿足多行和跨行匹配?_網路知道

https://..com/question/989230535019059459.html

Author: Crifan Li

Update: 20200208

"""

import re

yourMultipleLineStr = """

first line dog

second line cat

third line no animal

"""

foundMatched = re.search("dog.+cat", yourMultipleLineStr, re.S)

print("foundMatched=%s" % foundMatched)

if foundMatched:

matchedStr = foundMatched.group(0)

print("matchedStr=%s" % matchedStr)

# foundMatched=<re.Match object; span=(12, 31), match='dog second line cat'>

# matchedStr=dog

# second line cat



具體語法詳見:

re --- 正則表達式操作 — Python 3.8.1 文檔

  • re.S

  • re.DOTALL

  • 讓'.'特殊字元匹配任何字元,包括換行符;如果沒有這個標記,'.'就匹配除了換行符的其他任意字元。對應內聯標記(?s)。

-》就可以讓上面的 點. 可以匹配到 換行符 -》 就可以去跨行去匹配了


在線測試效果截圖:

RegExr: Learn, Build, & Test RegEx


更多內容,詳見我的教程:

應用廣泛的超強搜索:正則表達式

2. python3 正則表達式如何匹配多段內容,舉例如下:(中間需要通配掉許多字元)

\ 應該轉義吧?試試這樣寫:

pattern = re.compile(r'href=(.*?) target="_blank" title=(.*?)>.*?timestyle4222">(.*?)\xa0')



pattern = re.compile('href=(.*?) target="_blank" title=(.*?)>.*?timestyle4222">(.*?)\\xa0')

3. 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 = ""

4. python 正則表達式 匹配多行聊天記錄的問題。

如果你所指得[任意文字]的意思是1個或1個以上文字得話
你可以試試下面得語句
p = re.compile(u'.*想知道.+\n.+也想知道.+')

否則你只需要將裡面得"+"改為"*"就可以匹配0得情況
也就是下面得語句
p = re.compile(u'.*想知道.*\n.*也想知道.*')

測試代碼如下:
>>>a = u"""我想知道。
..... 我也想知道。"""
>>>p.search(a)
>>><_sre.SRE_Match object at 0x1014d5100>

看了你的問題補充,如果你想要查找出字元串中最後匹配的子字元串的話,我目前沒想到比較好的辦法。
如果一定要用正則的話你可以考慮採用遍歷的方式,也就是匹配所有不含abc的情況。
比如說^a, ^b, ^c, ab^c, a^bc....等等等等。不過這樣以來正則表達式會顯得過於復雜,而且擴展行幾乎為0。
如果可以不用正則,那python本身的庫就能夠實現(可能你嫌處理語句過多或考慮到時間問題而不想用),而且邏輯也較為簡單(我覺得是這樣)。python的string類中自帶有find()和rfind()方法再加上split()方法,合理使用的話應該能找出所有的匹配子字元串。

5. 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()字元串替換 1.替換所有匹配的子串 #用newstring替換subject中所有與正則表達式regex匹配的子串
result = re.sub(regex, newstring, subject) 2.替換所有匹配的子串(使用正則表達式對象) reobj = re.compile(regex) result = reobj.sub(newstring, subject) 字元串拆分 1.字元串拆分 result = re.split(regex, subject) 2.字元串拆分(使用正則表示式對象) reobj = re.compile(regex) result = reobj.split(subject)

6. python re正則表達式多匹配頭單匹配尾如何最小匹配

importre

string='''<imgsrc="https://img3.doubanio.com/f/shire//pics/blank.gif"data-origin="https://img1.doubanio.com/view/photo/albumcover/public/p2519116699.jpg"alt=""/>'''

regex=re.findall(r'data-origin="([^"]+.jpg)"',string)
print(regex[0])

7. python正則表達式匹配滿足條件的兩行

所以,你這是在多行文件中找出任意2行符合條件的,還是2行就算一組數據,裡面有多組數據?

r=re.compile(r'[S]+|([AGCT]{12})([AGCT]{12})[s]+163[s]+[w]+[s]+([d]+)(?:[s]+[S]+){3}[s]+([d]+)(?:[s]+[S]+){2}[
]+'
r'[S]+|21[s]+993[s]+[w]+[s]+3(?:[s]+[S]+){3}[s]+4(?:[s]+[S]+){2} ')

。。。

8. python正則表達式提取多個匹配內容

替換掉不就行咯,像這樣:
re.sub(r'<[A-Z]+>',' ',『<SPAN><P>eng li aas<SS>ddde<AP>iiiiideeeeef<P>
』)
或者:
>>> ' '.join(re.split(r'<[A-Z]+>','<SPAN><P>eng li aas<SS>ddde<AP>iiiiideeeeef<P>'))
' eng li aas ddde iiiiideeeeef '
>>>

閱讀全文

與python正則表達式多行匹配相關的資料

熱點內容
伺服器無響應是什麼原因呀 瀏覽:978
wd文檔里的app怎麼製作 瀏覽:509
電腦里的文件夾沒有了一般能恢復嗎 瀏覽:410
哪裡有配加密鑰匙的 瀏覽:208
伺服器開不了機怎麼把數據弄出來 瀏覽:958
gif動態圖片怎麼壓縮 瀏覽:519
黑猴子棒球壓縮文件解壓密碼 瀏覽:631
如何讓app適應不同的手機屏幕大小 瀏覽:8
蘋果手機如何給安卓手機分享軟體 瀏覽:759
蘋果電腦怎麼運行騰訊雲伺服器 瀏覽:59
明日之後沙石堡命令助手 瀏覽:261
蛋糕店用什麼樣的app 瀏覽:877
長安銀行信用卡app怎麼取現 瀏覽:635
dos命令cmd命令的 瀏覽:226
阿里雲存檔視頻文件的伺服器 瀏覽:194
ftp修改文件許可權命令 瀏覽:491
周易八卦梅花演算法 瀏覽:676
java組織機構 瀏覽:953
h5大轉盤游戲源碼 瀏覽:592
學校伺服器地址查詢 瀏覽:109