in reply to Regex search and replace appears to be broken

Perhaps seeeing exactly what is being matched and captured will help (/x regex modifier used to help my poor eyes):

>perl -wMstrict -le "my $s = qq{abandon/to\t(a road)\t}; $s =~ s{ ([^\t]*) \t ([^\t]*) \1 ([^\t]*) \t }{\t$2$1$3\t}xms; print qq{1 '$1' 2 '$2' 3 '$3'}; print qq{'$s'}; " 1 'o' 2 '(a r' 3 'ad)' 'abandon/t (a road) '

Update: This may help (note anchor at start of string): If the entire content of the first cell is repeated anywhere in the second cell, delete the entire content of the first cell:

>perl -wMstrict -le "for my $s (qq{abandon/to\t(a road)\t}, qq{abandon/to\txxx abandon/to yyy \t}, ) { print qq{'$s'}; (my $t = $s) =~ s{ \A ([^\t]+) (\t [^\t]* \1 [^\t]* \t) }{$2}xms; print qq{1 '$1' 2 '$2'} if defined($1) and defined($2); print qq{'$t' \n}; } " 'abandon/to (a road) ' 'abandon/to (a road) ' 'abandon/to xxx abandon/to yyy ' 1 'abandon/to' 2 ' xxx abandon/to yyy ' ' xxx abandon/to yyy '

And this, capturing less, may be slightly better:
    s{ \A ([^\t]+) (?= \t [^\t]* \1 [^\t]* \t) }{}xms;

Replies are listed 'Best First'.
Re^2: Regex search and replace appears to be broken
by elef (Friar) on Aug 17, 2010 at 14:06 UTC
    You're absolutely right about the anchoring, I completely overlooked that.
    On some lines, my regex was matching the last letter or the last couple of letters of the first cell instead of the whole cell. s/^([^\t]*)\t... instead of s/([^\t]*)\t... fixes the problem.