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

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?
  • Comment on Re^4: Regular Expression Translation Help!

Replies are listed 'Best First'.
Re^5: Regular Expression Translation Help!
by kennethk (Abbot) on Nov 30, 2010 at 21:23 UTC
    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
Re^5: Regular Expression Translation Help!
by AnomalousMonk (Archbishop) on Dec 01, 2010 at 00:58 UTC
    ... 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