这个问题已经有了答案:
How to convert regexp from lookahead
1答
Using positive-lookahead (?=regex) with re2
2答
How to simulate negative lookbehind in Go
2答
Negative Look Ahead Go regular expressions
3答
Go regex, Negative Look Ahead alternative
1答
我有一个regex来检测css中的绝对路径,使用javascript可以,但在golang中不行:
这是我的正则表达式:
url\((?!['"]?(?:data|http|https):)['"]?([^'"\)]*)['"]?\)
在Golang中,运行时捕获错误:
error parsing regexp: invalid or unsupported Perl syntax: `(?!`
有人知道如何修正这个错误吗?
这是演示:
Demo in golang
这是其他语言的演示作品,而不是Golang:
https://regex101.com/r/WkbUuT/4
最佳答案:
(?=url\((?:['"]?(?:https?|data):[^'"\)]+['"]?|['"]?([^'")]+)['"]?)\)
部分地
url\(匹配url((?:非捕获组['"]?可选报价(?:https?|data):匹配http、https或数据。[^'"\)]+匹配除'、"或)['"]?可选报价|['"]?可选报价([^'")]+)捕获组1,匹配除'、"或)以外的任何字符的1+倍['"]?可选报价)关闭组\)Regex demo
路径在组1中。
请注意,对开头和结尾引号都使用
['"]?意味着它也可以在只有开头或结尾时匹配,因为它是可选的。如果您只希望在每个开头的引号被匹配的结束引号结束时进行一致的匹配,则可以列出所有变体。