https:\/\/github.com\/google\/re2\/wiki\/Syntax<\/a><\/p>\n\n\n\nIt does mention that it doesn’t support backreferences.<\/p>\n\n\n\n
However, there is another golang package available that uses libpcre++, Perl regexes, and it supports backreferences.<\/strong><\/p>\n\n\n\nhttps:\/\/github.com\/glenn-brown\/golang-pkg-pcre\/tree\/master\/src\/pkg\/pcre<\/code><\/pre>\n\n\n\n <\/p>\n\n\n\n
<\/span>Program<\/strong><\/span><\/h2>\n\n\n\nSo let’s see examples of backreferences in golang using this pcre<\/strong> package.<\/p>\n\n\n\n<\/span>First Example<\/strong><\/span><\/h3>\n\n\n\nLet’s say we want to match the repetition of a digit. Valid inputs are<\/p>\n\n\n\n
1111\n888888888\n444<\/code><\/pre>\n\n\n\nRegex to match for the same would be<\/p>\n\n\n\n
(\\d)\\1+<\/code><\/pre>\n\n\n\nLet’s dissect this regex<\/p>\n\n\n\n
- (\\d)<\/strong> – Matches a single digit. The single-digit is enclosed in parentheses so it acts as a capturing group.<\/li><\/ul>\n\n\n\n
- \\1<\/strong> – Backreferences the first sub match by capturing group. So it will reference the first digit<\/li><\/ul>\n\n\n\n
- +<\/strong> – One or more occurrences of the previous digit<\/li><\/ul>\n\n\n\n
Program for the same<\/p>\n\n\n\n
package main\n\nimport (\n\t\"fmt\"\n\n\t\"github.com\/glenn-brown\/golang-pkg-pcre\/src\/pkg\/pcre\"\n)\n\nfunc main() {\n\tregex := pcre.MustCompile(`(\\d)\\1+`, 0)\n\n\tmatches := regex.MatcherString(\"1111\", 0).Matches()\n\tfmt.Println(\"For 1111 : \", matches)\n\n\tmatches = regex.MatcherString(\"88888888\", 0).Matches()\n\tfmt.Println(\"For 88888888 : \", matches)\n\n\tmatches = regex.MatcherString(\"444\", 0).Matches()\n\tfmt.Println(\"For 444 : \", matches)\n\n\tmatches = regex.MatcherString(\"123\", 0).Matches()\n\tfmt.Println(\"For 123 : \", matches)\n}<\/code><\/pre>\n\n\n\nOutput<\/strong><\/p>\n\n\n\nFor 1111 : true\nFor 88888888 : true\nFor 444 : true\nFor 123 : false<\/code><\/pre>\n\n\n\nAs expected it gives a match for repetition of digits<\/p>\n\n\n\n
1111\n888888888\n444<\/code><\/pre>\n\n\n\nAnd it does not match for below as it is not a repetition<\/p>\n\n\n\n
123<\/code><\/pre>\n\n\n\n<\/span>Second Example<\/strong><\/span><\/h3>\n\n\n\nLet’s say we want to match the repetition of a word separated by a colon. Valid inputs are<\/p>\n\n\n\n
John:John\nThe names are Simon:Simon<\/code><\/pre>\n\n\n\nRegex to match for the same would be<\/p>\n\n\n\n
(\\w+):\\1<\/code><\/pre>\n\n\n\nLet’s dissect this regex<\/p>\n\n\n\n
- (\\w+)<\/strong> – Matches a word having more than one character. It is enclosed in parentheses so it acts as a capturing group.<\/li><\/ul>\n\n\n\n
- \\1<\/strong> – Backreferences the first sub match by capturing group. So it will reference the matched word<\/li><\/ul>\n\n\n\n
Program for the same<\/p>\n\n\n\n
package main\n\nimport (\n\t\"fmt\"\n\n\t\"github.com\/glenn-brown\/golang-pkg-pcre\/src\/pkg\/pcre\"\n)\n\nfunc main() {\n\tregex := pcre.MustCompile(`(\\w+):\\1`, 0)\n\n\tmatches := regex.MatcherString(\"John:John\", 0).Matches()\n\tfmt.Println(\"For John:John: \", matches)\n\n\tmatches = regex.MatcherString(\"The names are Simon:Simon\", 0).Matches()\n\tfmt.Println(\"For The names are Simon:Simon: \", matches)\n\n\tmatches = regex.MatcherString(\"John:Simon\", 0).Matches()\n\tfmt.Println(\"For John:Simon: \", matches)\n\n}<\/code><\/pre>\n\n\n\nOutput<\/strong><\/p>\n\n\n\nFor John:John: true\nFor The names are Simon:Simon: true\nFor John:Simon: false<\/code><\/pre>\n\n\n\nAs expected it gives a match for a string that contains a substring having a repetition of a word<\/p>\n\n\n\n
John:John\nThe names are Simon:Simon<\/code><\/pre>\n\n\n\nAnd it does not match for below as it does not contain a repetition of a word<\/p>\n\n\n\n
John:Simon<\/code><\/pre>\n\n\n\n <\/p>\n\n\n\n