elmuel has asked for the wisdom of the Perl Monks concerning the following question:

Hi, This is also my first time posting with the Monks, so please let me know if I'm not following the guidelines as well.

I have two regex questions. First, when you're using s/// can you have multiple options for replacing text? I saw that you could do this with a hash array but was looking for just a regex solution if possible.

For instance, I have text such as

oldsmobile_eighty_eight
oldsmobile_ninety_nine

which I'd like to replace with

olds_88
olds_99

I was looking for something similar to translate (but for longer strings instead of individual letters) where I could say change 'eighty eight' to 88 and 'ninety_nine' to 99 if 'oldsmobile' is matched.

My second question is about using capturing with the lookahead. Again I'm looping over vehicle names and would like to get rid of text around a vehicle name, and keep they hybrid designation if it exists. For example,

k1500_silverado_xfe_hybrid_2wd would become silverado_hybrid
and
k1500_silverado_awd would become silverado
I had tried a few different configurations, such as
s/^.*(silverado|sierra)(.*?)((_hybrid)?)(.*)/$1$3/gi
to throw out stuff between the model name and 'hybrid' if it exists. I tried using regex like.*(?!_hybrid) but don't know how to make it work for all cases whether it's a hybrid or not.
Thanks for your help.

Replies are listed 'Best First'.
Re: Substitution with multiple options for capture and replace.
by Anonymous Monk on Nov 09, 2009 at 21:57 UTC
    I saw that you could do this with a hash array but was looking for just a regex solution if possible.

    The left side is the regex, the right side is perl variable interpolation, and if you use the /e flag, its perl code. so

    $_ = "123"; s/(.)/ my $r = " LEFT "; if($1 eq '1'){ $r = " one " } $r /eg; print "$_\n"; __END__ one LEFT LEFT
    More in perlretut, perlre
      Thanks, I'll try that out. Does anyone know how to do the second regex question I asked about?
Re: Substitution with multiple options for capture and replace.
by toolic (Bishop) on Nov 10, 2009 at 01:44 UTC
    Addressing your first question, I can not think of a way to perform the multiple replacements you require using a single substitution operator without using a hash. However, Lingua::EN::Numericalize seems to be relevant to your question.