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 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法