Re: s/A/B/ and s/B/C/ but don't want A -> C
- From: Ted Zlatanov <tzz@xxxxxxxxxxxx>
- Date: Mon, 31 Dec 2007 08:09:18 -0600
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
.
- References:
- FAQ 6.4 I put a regular expression into $/ but it didn't work. What's wrong?
- From: PerlFAQ Server
- Re: FAQ 6.4 I put a regular expression into $/ but it didn't work. What's wrong?
- From: Ben Morrow
- s/A/B/ and s/B/C/ but don't want A -> C (was: FAQ 6.4 I put a regular expression into $/ but it didn't work. What's wrong?)
- From: Ted Zlatanov
- Re: s/A/B/ and s/B/C/ but don't want A -> C
- From: Ted Zlatanov
- Re: s/A/B/ and s/B/C/ but don't want A -> C
- From: Abigail
- FAQ 6.4 I put a regular expression into $/ but it didn't work. What's wrong?
- Prev by Date: FAQ 6.17 Why don't word-boundary searches with "\b" work for me?
- Next by Date: Re: FAQ 6.11 Can I use Perl regular expressions to match balanced text?
- Previous by thread: Re: s/A/B/ and s/B/C/ but don't want A -> C
- Next by thread: Re: s/A/B/ and s/B/C/ but don't want A -> C (was: FAQ 6.4 I put a regular expression into $/ but it didn't work. What's wrong?)
- Index(es):