我有一個字符串,它可以是由下列ilustrated(extraspaces意):正則表達式與Python:的findall一個boundry內部
"words that don't matter START some words one some words two some words three END words that don't matter"
抓住START和END ['some words one', some words two', 'some words three']
之間的每個子串,我寫以下代碼:
result = re.search(r'(?<=START).*?(?=END)', string, flags=re.S).group()
result = re.findall(r'(\(?\w+(?:\s\w+)*\)?)', result)
是否有可能用一個單一的正則表達式實現這一點?
這就是我正在尋找的:一個正則表達式解決方案。這是相當新的模塊,對吧?我不知道這件事。我還需要了解IF x THEN | ELSE在正則表達式中的可能性。 –
@LeandroRibeiro:的確如此。看看https://regexone.com/和http://rexegg.com/(很高級,但很棒)。 – Jan
我改變了你的正則表達式[一點](https://regex101.com/r/oLFVRk/2/)。這樣它就可以抓取所有子字符串,而不管字數。我的例子有三個字的子字符串,但我需要它匹配每個字符串未知數量的字:(?:\ G(?!\ A)| START)\ s * \ K (?!\ bEND \ b) \ w +(?:\ s \ w +)* –