You are here: PSPad forum > Bug report / Hlášení chyb > back reference not working correctly

back reference not working correctly

#1 back reference not working correctly

Posted by: Jack_Brody | Date: 01/22/2010 13:52 | IP: IP Logged

Let's have this text:

Quote:
alpha bravo
bravo alpha

and use this regex search string:

Quote:
(alpha).*(bravo)|\2.*\1

It will find only the first line...

Tried in 4.5.4 build 2356 and latest developer build 4.5.5 (2381). The same results.

Options: Reply | Quote | Up ^


#2 Re: back reference not working correctly

Posted by: myf | Date: 01/26/2010 20:34 | IP: IP Logged

IMO not a bug.

Backreferences in a statement after 'or' operator are nonsense, because they cannot get something that was not matched by the positive statement before, so they receive 'nothing'.

Look at

/(A).*(B)|\2.*\1/

that is
"A"
followed by anything (except newlines, but including "B")
followed by "B"
OR
'nothing' (because whole left side failed)
followed by anything (except newlines, but including even "A B")
followed by 'nothing' (because whole left side failed)

As I understand it, there is a difference in what that 'nothing' technically is in certain implementations. Javascript for example seems to consider that 'nothing' as an empty string, so above regexp will match really anything
'x'.match(/(A)|(\1)/)[2] === '' // true

[url]http://regexpal.com/?flags=&regex=(A).*(B)|\2.*\1&input=x%20y%3B%20y%20x%3B%20x%20y%3B%20y%20x%20A%20B%20A%20B[/url]

Options: Reply | Quote | Up ^






Editor PSPad - freeware editor, © 2001 - 2014 Jan Fiala
Hosted by Webhosting TOJEONO.CZ, design by WebDesign PAY & SOFT, code Petr Dvořák