in reply to Re: Regular Expression Translation Help!
in thread Regular Expression Translation Help!

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 }

Replies are listed 'Best First'.
Re^3: Regular Expression Translation Help!
by kennethk (Abbot) on Nov 30, 2010 at 20:47 UTC
    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?
        I'm having some trouble understanding the question here, so I will list off some possible inputs and what the output from the conditional operator would be.
        • USA111 => 111
        • BRA111 => 111
        • BRA2#$ => 2#$
        • ABC111 => ABC111
        • USAabcXY => abc
        • usa111xy => usa111xy

        In cases where the string changes, the regular expression matched and the result is what was stored in $2. In cases where the string didn't change, the regular expression did not match.

        ... if something else is not ... Otherwise it will grab ... no?

        Why not try it and see?

        >perl -wMstrict -le "while (<>) { chomp(my $acc = $_); my $ternary = ($acc =~ /(^USA|^BRA|^CAN)?(.*?)(IN)?$/i) ? $2 : $acc; my $capture2 = defined($2) ? $2 : 'undefined'; print qq{input: '$acc'}; print qq{ternary: '$ternary' \\2: '$capture2'}; } " USA1111 input: 'USA1111' ternary: '1111' \2: '1111' BRA1111 input: 'BRA1111' ternary: '1111' \2: '1111' xxxUSA1111 input: 'xxxUSA1111' ternary: 'xxxUSA1111' \2: 'xxxUSA1111' foo input: 'foo' ternary: 'foo' \2: 'foo' ^Z