in reply to Regular Expression Translation Help!

You already got a nice answer from kennethk. I just wanted to add that you may want to move the caret left one position though: Currently USA must be at the start of the string, but BRA and CAN can be anywhere....

...roboticus

When your only tool is a hammer, all problems look like your thumb.

  • Comment on Re: Regular Expression Translation Help!

Replies are listed 'Best First'.
Re^2: Regular Expression Translation Help!
by Anonymous Monk on Nov 30, 2010 at 20:43 UTC
    Would this be OK or the right thing is to move the caret left outside?
    Could the first way work?
    ${ ($acc =~ /(^USA|^BRA|^CAN)?(.*?)(IN)?$/i) ? $2 : $acc }
    or
    ${ ($acc =~ /^(USA|BRA|CAN)?(.*?)(IN)?$/i) ? $2 : $acc }
      The results in the two cases are identical since ^ is a zero-width match. I personally think the second option is better because it is clearer and has fewer characters, hence is less sensitive to typos, but that is wholly subjective.
        For me if something else is not one of the 3 letters; USA or BRA or CAN it will only grab what ever is after the letters and stored it in $2. Otherwise it will grab the letlers and what ever is after, like USA1111 or BRA1111 or CAN11111, no?