1

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?

Kirill
  • 1,530
  • 5
  • 24
  • 48
bab
  • 2,119
  • 3
  • 28
  • 49
  • 7
    Please check [this answer](http://stackoverflow.com/a/23968993/3832970). In short: it is not possible to use backreferences in Go regex. – Wiktor Stribiżew Jul 22 '15 at 21:36

1 Answers1

2

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