dsd119120 2019-09-21 08:00
浏览 563

Golang正则表达式lookarround不支持对正则表达式进行错误分析:Perl语法无效或不受支持:`(?!`[重复]

I have a regex to detect absolute path in CSS, with javascript it work, but not in GOLANG: this is my regex:

url\((?!['"]?(?:data|http|https):)['"]?([^'"\)]*)['"]?\)

In golang, when run it catch error:

error parsing regexp: invalid or unsupported Perl syntax: `(?!`

Does anyone known how to fix this error?

This is demo:

Demo in golang

This is demo work with other language, not golang:

https://regex101.com/r/WkbUuT/4

</div>
  • 写回答

1条回答 默认 最新

  • dongmi1864 2019-09-21 08:57
    关注

    Go does not support a positive lookahead (?=.

    To match the path if it does not start with data or http or https, one option is using an alternation first matching what you don't want and capture in group 1 what you want to keep.

    url\((?:['"]?(?:https?|data):[^'"\)]+['"]?|['"]?([^'")]+)['"]?)\)
    

    In parts

    • url\( Match url(
    • (?: Non capturing group
      • ['"]? Optional quote
      • (?:https?|data): Match http, https or data.
      • [^'"\)]+ Match 1+ times any char except ', " or )
      • ['"]? Optional quote
      • | Or
      • ['"]? Optional quote
      • ([^'")]+) Capture group 1, matching 1+ times any char except ', " or )
      • ['"]? Optional quote
    • ) Close group
    • \)

    Regex demo

    The path is in group 1.

    Note that using ['"]? for both the opening and closing quotes means that it could also match when only the opening or closing is present as it is optional.

    If you want only consistent matching where each opening quote is closed by a matching closing quote you might list all variations.

    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记