Re: s/A/B/ and s/B/C/ but don't want A -> C



On 29 Dec 2007 00:23:55 GMT Abigail <abigail@xxxxxxxxxx> wrote:

A> With 5.10, you can do it with A and B patterns:

A> use 5.010;
A> our $REGMARK;
A> my %replacement = qw [X B Y C];
A> $_ = "AABDAAABBE";
A> s/(*:X)A(?{})|(*:Y)B/$replacement{$REGMARK}/g;
A> say;
A> __END__
A> BCDBCE

A> Note that the (?{}) is there to prevent a bug from triggering.

On Sat, 29 Dec 2007 14:45:17 +0100 "Dr.Ruud" <rvtol+news@xxxxxxxxxxxx> wrote:

R> If string-A can ever be a subpattern of string-B, then you need to add a
R> "longest first" approach.
....
R> (or use a different regex-engine :)

Abigail's 5.10 solution doesn't scale well, and Dr. Ruud's longest-first
solution (which others suggested but without the longest-first fix) is
closest to a generic solution. It won't help if the list contains
regular expressions, but for fixed strings it's the best I can see. I'd
just build the match alternation with something like this:

sprintf "(%s)", join('|', sort { length $a <=> length $b } keys %patterns);

Thanks for all the suggestions.

Ted
.