簡體   English   中英

Python正則表達式:查找不包含子字符串的子字符串

[英]Python regex: find a substring that doesn't contain a substring

這是一個例子:

a = "one two three four five six one three four seven two"
m = re.search("one.*four", a)

我想要的是找到從“一”到“四”的子串,其中不包含子串“兩”。 答案應該是:m.group(0)=“一三四”,m.start()= 28,m.end()= 41

有沒有辦法用一條搜索線做到這一點?

您可以使用此模式:

one(?:(?!two).)*four

在匹配任何其他字符之前,我們檢查我們沒有開始匹配“兩個”。

工作示例: http//regex101.com/r/yY2gG8

隨着Satoru添加的更硬的字符串,這適用:

>>> import re
>>> a = "one two three four five six one three four seven two"
>>> re.findall("one(?!.*two.*four).*four", a)
['one three four']

但是 - 有一天 - 你真的會后悔寫一些棘手的正則表達式。 如果這是我需要解決的問題,我會這樣做:

for m in re.finditer("one.*?four", a):
    if "two" not in m.group():
        break

這很棘手,我在那里使用最小的匹配( .*? )。 Regexps可能是一個真正的痛苦:-(

編輯:哈哈! 但是,如果你讓字符串變得更難,那么頂部的混亂正則表示再次失敗:

a = "one two three four five six one three four seven two four"

最后:這是一個正確的解決方案:

>>> a = 'one two three four five six one three four seven two four'
>>> m = re.search("one([^t]|t(?!wo))*four", a)
>>> m.group()
'one three four'
>>> m.span()
(28, 42)

我知道你說你希望m.end()為41,但這是不正確的。

你可以使用負前瞻斷言(?!...)

re.findall("one(?!.*two).*four", a)

另一個襯里有一個非常簡單的圖案

import re
line = "one two three four five six one three four seven two"

print [X for X in [a.split()[1:-1] for a in 
                     re.findall('one.*?four', line, re.DOTALL)] if 'two' not in X]

給我

>>> 
[['three']]

暫無
暫無

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

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