bab
bab

Reputation: 2169

How to use lookbehind regexp in go?

I'm trying to convert this ruby regex to go.

GROUP_CALL = /^(?<i1>[ \t]*)group\(?[ \t]*(?<grps>#{SYMBOLS})[ \t]*\)?[ \t]+do[ \t]*?\n(?<blk>.*?)\n^\k<i1>end[ \t]*$/m

I converted it to

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")

but when run I get this error

error parsing regexp: invalid escape sequence: \k

There's no mention of \k in the go docs, is there no equivalent in go?

Upvotes: 1

Views: 1856

Answers (1)

marsgoonbars
marsgoonbars

Reputation: 76

lookbehinds aren't supported neither are backreferences like @stribizhev mentioned.

Regular Expression 2 (RE2) Syntax Reference:
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/)

Upvotes: 2

Related Questions