簡體   English   中英

正則表達式,如何匹配所有出現的事件

[英]Regex, how to match everything up to nth occurrence

我正在嘗試從網頁中獲取所有信息,直到第二次出現單詞matchdate

(.*?matchdate){2}是我正在嘗試的方法,但這並不是在做這個技巧。 該頁面具有14個以上的“ matchdate”匹配項,我只想讓所有內容都達到第二個,然后就別無其他。

https://regex101.com/r/Cjyo0f/1 <---我保存的正則表達式。

我在這里想念什么?

謝謝。

您可以通過以下幾種方法執行此操作:

如果可以,請刪除g標志

沒有全局標志,正則表達式將僅捕獲其遇到的第一個實例。

https://regex101.com/r/Cjyo0f/2

在正則表達式的前面添加^

尖號將迫使正則表達式從字符串的開頭開始匹配,排除所有其他可能性。

https://regex101.com/r/Cjyo0f/3

如果Python可用,請使用.split().join()

如果有常規的python,我建議:

string = "I like to matchdate, I want to each matchdate for breakfest"
print "matchdate".join(string.split("matchdate")[:2])

你差點就吃了! (.*?matchdate){2}實際上是正確的。 它只需要一個re.DOTALL標志,以便點與換行符以及其他字符匹配。

這是一個工作測試:

>>> import re

>>> s = '''First line
Second line
Third with matchdate and more
Fourth line
Fifth with matchdate and other
stuff you're
not interested in
like another matchdate
or a matchdate redux.
'''

>>> print(re.search('(.*?matchdate){2}', s, re.DOTALL).group())
First line
Second line
Third with matchdate and more
Fourth line
Fifth with matchdate

暫無
暫無

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

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