# 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))+$
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://thamizhiniyancs.gitbook.io/writeups/hackerrank/regex/backreferences.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
