我正在尝试将此 ruby 正则表达式转换为 go。
GROUP_CALL = /^(?<i1>[ \t]*)group\(?[ \t]*(?<grps>#{SYMBOLS})[ \t]*\)?[ \t]+do[ \t]*?\n(?<blk>.*?)\n^\k<i1>end[ \t]*$/m
我把它转换成
groupCall := regexp.MustCompile("^(?P<i1>[ \\t]*)group\\(?[ \\t]*(?P<grps>(?::\\w+|:\"[^\"#]+\"|:'[^']+')([ \\t]*,[ \\t]*(?::\\w+|:\"[^\"#]+\"|:'[^']+'))*)[ \\t]*\\)?[ \\t]+do[ \\t]*?\\n(?P<blk>.*?)\\n^\\k<i1>end[ \\t]*$/s")
但是运行的时候出现这个错误
error parsing regexp: invalid escape sequence: \k
go docs 中没有提到\k
, go 中没有等价物吗?
最佳答案
lookbehinds 不受支持,也不支持像 @stribizhev 提到的反向引用。
正则表达式 2 (RE2) 语法引用:
https://github.com/google/re2/wiki/Syntax
The syntax of the regular expressions accepted is the same general syntax used by Perl, Python, and other languages. More precisely, it is the syntax accepted by RE2 and described at //code.google.com/p/re2/wiki/Syntax, except for \C. --GoLang Docs
(ref: https://golang.org/pkg/regexp/)
关于ruby - 如何在 go 中使用 lookbehind 正则表达式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31574320/