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

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)
     )
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题