You need to use the s/// operator to get more flexible
replacement. Here is a simple snippet using the technique
to get you started:
$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;
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:
- Call keys on the translation hash to get a list of
strings you want to find.
- sort and reverse to put them in reverse sorted
order. As the comment says, this is a hack. If your
RE had foo appear before foobar, then it would never
match "foobar", it would always stop when it found that
it could match "foo". (If this comment makes no sense
to you, then pick up Mastering Regular Expressions. Or
pick up japhy's book when it comes out.)
- You then map them through quotemeta. What map
does is allows you to transform a list. The
transformation that we want in this case is from strings
that might contain characters which are meaningful to
regular expressions (eg "|") and we want to escape them.
Which is what quotemeta does for us.
- Then join with a "|". This gives us a string like
"foobar|foo|bar" which will now match exactly the keys
of our original hash.
And then we do a search and replace. The pattern will
match any of our keys. The replacement replaces the hash
lookup.
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. :-)
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.