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

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.

Replies are listed 'Best First'.
Re^4: Regular Expression Translation Help!
by Anonymous Monk on Nov 30, 2010 at 21:02 UTC
    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.

        and if i'm reading the regex context correctly, if there's no match at all, then the string sent for matching is returned intact without changes.
        my personal preference would be for a code block that is a bit clearer, as combining the search & replace regex and the trinary conditional operator makes it a bit confusing.
        the hardest line to type correctly is: stty erase ^H
      ... 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