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 Stata 面板数据模型选择
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用