Reputation: 64895
In an extended regular expression, with backrefernces, is it valid to have a backreference before the associated group?
For example, does the pattern \1(a)
make sense and what does it match?
Upvotes: 2
Views: 186
Reputation: 27723
\1(a)
is valid, however it may not return anything. There are statements that sometimes are good to be used for implementing a particular trick, however maybe not be used for their original purposes.
(?:\1(a)|(a))
This RegEx might be a rough example:
\??
Upvotes: 0
Reputation: 48711
Regex \1(a)
alone doesn't produce a match in regex flavors that support forward referencing. Why? because referred capturing group isn't yet processed. But they mean something when used in a quantified cluster e.g. (...)+
. A practical usage of using forward references is an attempt for matching nested brackets.
if you are writing a regex engine, should you reject such patterns at compilation time? Should they be accepted but never match anything?
There is no absolute answer for this. JavaScript doesn't support forward references but it doesn't complain about it either. It matches a zero-length position instead. Boost engine throws an error and PCRE deals with it in another way.
Should the backref be treated as an empty string hence this pattern would match
a
?
It's the case with JS. In fact there is no standard defined for such behaviors. It's all engines peculiarities that someone sometime decided to implement into their own flavor.
Upvotes: 3