Regex Back References

Back References or sometimes called groups could be created using () and later be referred by \{digit} depending on the programming languages. Usually, the whole matched pattern will be grouped as group 0.

Vim

In Vim’s regexp, we could refer to the group by using the syntax \[0-9]. The maximum groups in a pattern is 10. There is no mechanism, at least from what I know, to support more than 10 regexp groups.

C++

Back references could be accessed via regex_iterator from regex object. From there, we could iterate over the matched groups from the regex.

In order to not create a back reference, we could use the syntax (?:).

Rust

In order to get the back references from Regex object, we need to obtain a Capture object from Regex method captures. From Capture, we use the method get to get the particular group as a Match object.

Note: Remember to unwrap or map them to desirable type.

#cpp #regex #vim #rust