douchen9855 2018-01-05 21:01
浏览 52
已采纳

regexp Go lang中的错误

I'm trying to use this regular expressions in Go lang:

~((?<=^[^\s])|(?<=\s[^\s]))\s(?=[^\s](\s|$))~

{(c|d|e|i|l|n|o|p|r|t|z)\1+}

{( |a|b|d|[f-h]|i|[j-k]|m|q|s|[u-y])\1+}

{(c|d|e|i|l|n|o|p|r|t|z)\1+}

{( |a|b|[f-h]|i|[j-k]|m|q|s|[u-y])\1+}

{(si|sa|za|ja|to)\1+}

and in everyone of those i get this error:

panic: regexp: Compile(~((?<=^[^\s])|(?<=\s[^\s]))\s(?=[^\s](\s|$))~): error parsing regexp: invalid or unsupported Perl syntax: (?<

Maybe there is a trick know for someone? :/ or is it impossible to use them?

  • 写回答

1条回答 默认 最新

  • doujujian0052 2018-01-05 21:17
    关注

    If no lookbehinds supported, use a workaround like this:

    ((^|\s)\S)(\s)(?=\S(\s|$))

    where:

    Capture group 1 contains what's to be written back in place of the
    lookbehind.

    Capture group 3 is the actual whitespace in question.
    It's in a capture group just to separate it from the entire match.

    Summary: Group 1 + Group 3 = entire match.

    Expanded:

     #  (                             # (1 start)
     #       (?<= ^ [^\s] )
     #    |  (?<= \s [^\s] )
     #  )                             # (1 end)
    
     (                             # (1 start)
          ( ^ | \s )                    # (2)
          \S 
     )                             # (1 end)
     ( \s )                        # (3)
     (?=
          \S 
          ( \s | $ )                    # (4)
     )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏