Re: s/A/B/ and s/B/C/ but don't want A -> C
- From: "Dr.Ruud" <rvtol+news@xxxxxxxxxxxx>
- Date: Sat, 29 Dec 2007 14:45:17 +0100
Abigail schreef:
my %replacement = (A => B, B => C);
s/(A|B)/$replacement{$1}/g;
If string-A can ever be a subpattern of string-B, then you need to add a
"longest first" approach.
$ echo "AABABA" | perl -wpe '
BEGIN{ %repl = (A => B, AB => C); }
s/(A|AB)/$repl{$1}/g;
'
BBBBBB
$ echo "AABABA" | perl -wpe '
BEGIN{ %repl = (A => B, AB => C); }
s/(AB|A)/$repl{$1}/g; # longest first
'
BCCB
(or use a different regex-engine :)
--
Affijn, Ruud
"Gewoon is een tijger."
.
- 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 (was: FAQ 6.4 I put a regular expression into $/ but it didn't work. What's wrong?)
- From: Abigail
- FAQ 6.4 I put a regular expression into $/ but it didn't work. What's wrong?
- Prev by Date: Re: [newbie]How to call stored procedure using DBIx?
- Next by Date: FAQ 5.15 Why do I sometimes get an "Argument list too long" when I use <*>?
- Previous 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?)
- Next by thread: Regular Expression for XML Parsing
- Index(es):