Page 1 of 1

What is back refernces

Posted: Wed Jul 08, 2009 9:25 am
by Naeem
Hello to all
I want know what is back reference meaning in regular expressions.
Thanks.

Re: What is back refernces

Posted: Wed Jul 08, 2009 9:34 am
by superdezign
A backreference is when you re-use part of your regex. So, let's say you wanted to match a full HTML tag, opening and closing:

Code: Select all

<tag>content</tag>
You'd want to find any tag, and then match it with it's closing tag. Opening tag is the '<' character, the tag name, and the '>' character; closing tag is the '</' characters, tag name, and the '>' character. So, this regex would match a basic tag:

Code: Select all

<([A-Za-z]+)>.*?</\\1>
In PHP, we need two slashes before the '1' in order to create the escaping slash character, but normally you'd only use 1 slash.

Basically, the slash then the number matches the nth matched data. Each match is in parentheses, so \1 matches the first match in parentheses, the '([A-Za-z]+)'.

Note that in HTML parsing, this regex can't handle nesting very well.