> 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="https://3452970062-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FhMewGT4PbQ5e1zQPC4Ko%2Fuploads%2FI1wj7X3kpYvpEk0OeIHA%2Fimage.png?alt=media&amp;token=8ca7455f-2d9c-416c-a07e-36fc01330abb" 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="https://3452970062-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FhMewGT4PbQ5e1zQPC4Ko%2Fuploads%2Fp513R0LWE8FCLw51fHEG%2Fimage.png?alt=media&amp;token=9b7ee46f-f592-40e5-adca-99e6b5dd3f4c" alt=""><figcaption></figcaption></figure>

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

***

## Forward References

<figure><img src="https://3452970062-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FhMewGT4PbQ5e1zQPC4Ko%2Fuploads%2FvgFTcLshupEnatEeFzqM%2Fimage.png?alt=media&amp;token=2015cfa9-9f1e-4d17-bd25-a398c5b1bb17" alt=""><figcaption></figcaption></figure>

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