Regular Expressions Backreferences and Non-Capturing Groups


Since Groups are "numbered" some engines also support matching what a group has previously matched again.

Assuming you wanted to match something where two equals strings of length three are divided by a $ you'd use:


This would match any of the following strings:

"a b$a b"
"af $af "
"   $   "

If you want a group to not be numbered by the engine, You may declare it non-capturing. A non-capturing group looks like this:


They are particularly useful to repeat a certain pattern any number of times, since a group can also be used as an "atom". Consider:

(\d{4}(?:-\d{2}){2} \d{2}:\d{2}.\d{3}) (.*)[\r\n]+\1 \2

This will match two logging entries in the adjacent lines that have the same timestamp and the same entry.