An overview of the

Golang’s Regex package, RegEXP, uses the RE2 engine and does not support backreferences. You can check out the same situation here

Github.com/google/re2/…

It does mention that it does not support backreferences.

However, there is another Golang package that uses libpcrec ++, Perl Regexes, which supports backreferences.

https://github.com/glenn-brown/golang-pkg-pcre/tree/master/src/pkg/pcre
Copy the code

The program

So let’s look at an example of backreferencing using this PCRE package in Golang.

The first example

Suppose we want to match a repetition of a number. The valid input is

1111
888888888
444
Copy the code

Matching the same Regex will be

(\d)\1+
Copy the code

Let’s examine this code

  • (\d)- Matches a single number. The single number is enclosed in parentheses, so it acts as a capture group.

  • \1- Pushes back the first child match by capturing the group. So it’s going to reference the first number

  • +- One or more occurrences of the preceding number

Same program

package main

import (
	"fmt"

	"github.com/glenn-brown/golang-pkg-pcre/src/pkg/pcre"
)

func main(a) {
	regex := pcre.MustCompile(`(\d)\1+`.0)

	matches := regex.MatcherString("1111".0).Matches()
	fmt.Println("For 1111 : ", matches)

	matches = regex.MatcherString("88888888".0).Matches()
	fmt.Println("For 88888888 : ", matches)

	matches = regex.MatcherString("444".0).Matches()
	fmt.Println("For 444 : ", matches)

	matches = regex.MatcherString("123".0).Matches()
	fmt.Println("For 123 : ", matches)
}
Copy the code

The output

For 1111 :  true
For 88888888 :  true
For 444 :  true
For 123 :  false
Copy the code

As expected, it gives a match for repeated numbers.

1111
888888888
444
Copy the code

The following number does not match because it is not a repeated number

123
Copy the code

Second example

Suppose we want to match the repetition of a word separated by a colon. The valid input is

John:John
The names are Simon:Simon
Copy the code

The Regex that matches the same content is

(\w+):\1
Copy the code

Let’s take a look at this recombination

  • (\w+)- Matches a word with more than one character. It is enclosed in parentheses, so it is used as a capture group.

  • \1- Pushes back the first child match by capturing the group. So it will reference the matching word

Same procedure

package main

import (
	"fmt"

	"github.com/glenn-brown/golang-pkg-pcre/src/pkg/pcre"
)

func main(a) {
	regex := pcre.MustCompile(`(\w+):\1`.0)

	matches := regex.MatcherString("John:John".0).Matches()
	fmt.Println("For John:John: ", matches)

	matches = regex.MatcherString("The names are Simon:Simon".0).Matches()
	fmt.Println("For The names are Simon:Simon: ", matches)

	matches = regex.MatcherString("John:Simon".0).Matches()
	fmt.Println("For John:Simon: ", matches)

}
Copy the code

The output

For John:John:  true
For The names are Simon:Simon:  true
For John:Simon:  false
Copy the code

As expected, it gives a match for a substring containing a repeating word.

John:John
The names are Simon:Simon
Copy the code

Since the following string does not contain duplicate words, it does not match.

John:Simon
Copy the code

Replaces the matching string

The PCRE package also provides the ability to replace matching strings. Here is a similar example.

package main

import (
	"fmt"

	"github.com/glenn-brown/golang-pkg-pcre/src/pkg/pcre"
)

func main(a) {
	regex := pcre.MustCompile(`(\d)\1+`.0)

	input := "The number is 91-88888888"

	result := regex.ReplaceAll([]byte(input), []byte("redacted"), 0)
	fmt.Println("result: ".string(result))
}
Copy the code

The output

result:  The number is 91-redacted
Copy the code

In the example above, we have an entry with a backreference that matches a repeated number. We then edit the repeated number using the ReplaceAll method of the PCRE package.

result := regex.ReplaceAll([]byte(input), []byte("redacted"), 0)
Copy the code

As expected from the output, the repeated numbers are edited correctly.

result:  The number is 91-redacted
Copy the code

Hope you enjoyed this tutorial. Please share your feedback in the comments

Also, check out our Golang advanced tutorial series, Golang Regex: Backreferencing. Golang Advanced tutorial

The postGolang Regex: Backreferencesappeared first onWelcome To Golang By Example.