全球取代负面前瞻?

时间:2012-05-16 08:17:20

标签: python regex

我是Python-regex新手,所以请耐心等待......

我有一个很长的多行字符串,我必须用其他东西替换[[...]]字符串的目录部分,但只有它们不以'file://'开头,所以例如

    s= 'chk1 [[file:///excursions/digital/MindMaps/|mm]],
    local file [[file:///inFolder/tmptest/a/fan.txt|a/fan.txt]]
    and nonlocal [[/excursions/books/calsync.txt|/excursions/books]]'

我使用了这种变体(目前替换字符串只是检查我发现的内容):

re.sub('\[\[(?!file:)(^])*',"found:< \\1 >",s)

但是我得到了一个“sre_constants.error:无与伦比的小组”。你知道我做错了吗?

谢谢!

的Alessandro

2 个答案:

答案 0 :(得分:1)

您错过了第二组中的[]运算符:

'\[\[(?!file:)(^])*

应该是

'\[\[(?!file:)([^\]]*)'

当你有很多转义时使用原始字符串也更好:

re.sub(r'\[\[(?!file:)([^\]]*)', r"found:<\1>", s)

答案 1 :(得分:0)

由于转义

,您的方括号无法比拟
相关问题