簡體   English   中英

Python正則表達式匹配多次

[英]Python regex to match multiple times

我正在嘗試將模式與可能具有多個模式實例的字符串進行匹配。 我需要單獨的每個實例。 re.findall() 應該這樣做,但我不知道我做錯了什么。

pattern = re.compile('/review: (http://url.com/(\d+)\s?)+/', re.IGNORECASE)
match = pattern.findall('this is the message. review: http://url.com/123 http://url.com/456')

我需要“ http://url.com/123 ”, http://url.com/456和兩個數123 456是不同的元素match列表。

我也試過'/review: ((http://url.com/(\\d+)\\s?)+)/'作為模式,但沒有運氣。

用這個。 您需要在捕獲組之外放置“審核”以獲得所需的結果。

pattern = re.compile(r'(?:review: )?(http://url.com/(\d+))\s?', re.IGNORECASE)

這給出了輸出

>>> match = pattern.findall('this is the message. review: http://url.com/123 http://url.com/456')
>>> match
[('http://url.com/123', '123'), ('http://url.com/456', '456')]

你在正則表達式中有額外的東西。 在python中,模式應該只是一個字符串。 例如,而不是這樣:

pattern = re.compile('/review: (http://url.com/(\d+)\s?)+/', re.IGNORECASE)

它應該是:

pattern = re.compile('review: (http://url.com/(\d+)\s?)+', re.IGNORECASE)

通常在python中你實際上使用這樣的“原始”字符串:

pattern = re.compile(r'review: (http://url.com/(\d+)\s?)+', re.IGNORECASE)

字符串前面的額外r可以避免你不得不做大量的反斜杠轉義等。

使用兩步法:首先獲取從“review:”到EOL的所有內容,然后對其進行標記。

msg = 'this is the message. review: http://url.com/123 http://url.com/456'

review_pattern = re.compile('.*review: (.*)$')
urls = review_pattern.findall(msg)[0]

url_pattern = re.compile("(http://url.com/(\d+))")
url_pattern.findall(urls)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM