我在匹配和替换某些不包含在http://
当前Regex:
http://.*?\s+
这与模式匹配http://www.egg1.com http://www.egg2.com
我需要一个regex来匹配http://
示例:
"This is a sample. http://www.egg1.com and http://egg2.com. This regex will only match
this egg1 and egg2 and not the others contained inside http:// "
Match: egg1 egg2
Replaced: replaced1 replaced2
最终输出:
"This is a sample. http://www.egg1.com and http://egg2.com. This regex will only
match this replaced1 and replaced2 and not the others contained inside http:// "
问题: 需要匹配某些模式(例如:egg1 egg2),除非它们是http:///的一部分。如果egg1和egg2存在于http:///中,则不要匹配它们
我能想到的一个解决方案是为HTTP url和您的模式形成一个组合模式,然后相应地过滤匹配项:
印刷品:
更新:要将此习惯用法与
re.sub()
一起使用,只需提供一个筛选函数:印刷品:
您需要在模式前面加上一个否定的lookbehind断言:
在这个正则表达式中,每当正则表达式引擎找到匹配的模式
egg[0-9]
时,它都会回过头来验证前面的模式是否不匹配。负lookbehind断言以(?<!
开头,以)
结尾。这些分隔符之间的所有内容不应位于以下模式之前,也不会包含在结果中。如何在您的案例中使用它:
这不会捕获
http://...
:相关问题 更多 >
编程相关推荐