RegEx for encoding substitutions with index of match?
Posted on 2006-03-20
<alert comment="regex and perl newbie">
I'm a C++ programmer using the Greta regex library to do some conversions of html.
I've got input such as:
<p>The chair could be red or blue. It might be green.</p>
I want to convert lines like the above to set up links so the line above becomes something like:
<p>The chair could be <a=0>red</a> or <a=1>blue</a>. It might be <a=2>green</a>.</p>
I could write three separate statements, such as:
$var =~ s/\bred\b/<a=0>red</a>/i;
(not sure if above is legal perl ... just to illustrate what I'm trying to do)
$var =~ s/\bblue\b/<a=1>blue</a>/i;
$var =~ s/\bgreen\b/<a=2>green </a>/i;
Can this be done in one statement, so that the index of the matched alternative can be used as a variable:
In other words, can perl detect which of the alternative was actually matched?