> For the complete documentation index, see [llms.txt](https://thamizhiniyancs.gitbook.io/writeups/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://thamizhiniyancs.gitbook.io/writeups/hackerrank/regex/backreferences.md).

# Backreferences

{% embed url="<https://www.hackerrank.com/domains/regex?filters%5Bsubdomains%5D%5B%5D=backreferences>" %}

## Matching Same Text Again & Again

<figure><img src="/files/BfPKKPZd6Jj9HA2RWU5y" alt=""><figcaption></figcaption></figure>

```regex
([a-z]\w\s\W\d\D[A-Z][a-zA-Z][aeiouAEIOU]\S)\1
```

***

## Backreferences to Failed Groups

You have a test string S.\
Your task is to write a regex which will match S, with following condition(s):

* S consists of 8 digits.
* S may have "-" separator such that string S gets divided in 4 parts, with each part having exactly two digits. (Eg. 12-34-56-78)

```regex
^\d{2}(-?)\d{2}\1\d{2}\1\d{2}$
```

***

## Branch Reset Groups

<figure><img src="/files/ipczL5qwAnyeGJUEOSQJ" alt=""><figcaption></figcaption></figure>

```regex
^\d{2}(-(?:--)?|\.|:)\d{2}\1\d{2}\1\d{2}$
```

***

## Forward References

<figure><img src="/files/1RP9ZYX4ZxL5vmv8u5gB" alt=""><figcaption></figcaption></figure>

```regex
^(\2tic|(tac))+$
```
