in reply to Replace all at once
The line where we build up $re_str is a bit tricky if you haven't seen that kind of thing before. The first trick is that you need to read it from right to left. Here are the steps:$string = "We modify foos and bars but not foobars.\n"; my %trans = qw( foo bar bar foo foobar foobar ); # reverse sort is a hack to put strings before # substrings. (ie foobar before foo) my $re_str = join "|", map quotemeta($_), reverse sort keys %trans; $string =~ s/($re_str)/$trans{$1}/g; print $string;
If this answer totally confuses you from start to finish (which I fear it may), then I recommend picking up merlyn's book, Learning Perl. It should (among other things) give you a fairly digestible overview of what regular expressions are, how to read them, and how to produce them. After you have a handle on regular expressions, the above explanation is likely to make more sense.
UPDATE
In response to japhy, I don't mind. :-)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re (tilly) 1: Replace all at once
by japhy (Canon) on Dec 09, 2001 at 00:15 UTC | |
by dragonchild (Archbishop) on Dec 10, 2001 at 19:26 UTC | |
by japhy (Canon) on Dec 10, 2001 at 19:39 UTC |