ruby - 如何在 go 中使用 lookbehind 正则表达式?

标签 ruby regex go

我正在尝试将此 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/

相关文章:

ruby-on-rails - Rails 4 中的 null_session 和 reset_session 有什么区别?

arrays - ruby ,传统知识库。 getopenfile 的输出

python - 为 mongoengine 0.9.0 StringField 设置正则表达式模式

javascript - 如何从Wasm调用外部js函数?

go - Go 闭包捕获规则与 defer 有何不同?

ruby - 包含数组的散列到 ruby​​ 中的散列数组

ruby - 对哈希值进行排序 Ruby

r - 拆分数据表中不同长度的字符串

java - 如何替换以特定字符开头和结尾的所有子字符串?

arrays - Append for array of maps raplaces all previous array items on the 最新的一个