in reply to Skip quoted string in regex

Like LanX, I have some reservations about your basic approach, but that said, just using a split limit of 2 seems to do the trick here:

c:\@Work\Perl\monks>perl -wMstrict -e "my $clause = qq(failure_reason <> 'Response=X'); my @operators = qw ( <> != !< !> <= >= < > = ); my $opr_regex = '(\s*' . join('\s*|\s*', @operators) . '\s*)'; my ($fld,$opr,$val) = split /$opr_regex/, $clause, 2; print \"\n\$opr_regex = $opr_regex\n\n\"; print \"\$clause = $clause\n\n\"; print \"\$fld = ^|$fld^|\n\"; print \"\$opr = ^|$opr^|\n\"; print \"\$val = ^|$val^|\n\n\"; " $opr_regex = (\s*<>\s*|\s*!=\s*|\s*!<\s*|\s*!>\s*|\s*<=\s*|\s*>=\s*|\ +s*<\s*|\s*>\s*|\s*=\s*) $clause = failure_reason <> 'Response=X' $fld = |failure_reason| $opr = | <> | $val = |'Response=X'|
(Update: In the  qq(failure_reason <> 'Response=X') example given, you only actually want to split once.)


Give a man a fish:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^2: Skip quoted string in regex
by LanX (Saint) on Nov 09, 2015 at 20:59 UTC
    Some background...

    > In the qq(failure_reason <> 'Response=X') example given, you only actually want to split once.)

    I had the same idea but 3 seemed right, since 3 fields are returned.

    It's a bit confusing, b/c according to the docs "LIMIT... represents the maximum number of fields the EXPR will be split into".

    But the middle field originates from the (regex-group) within the $opr_regex.

    But still, split is the wrong approach here, b/c the LHS could be quoted too.

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

      ... the LHS could be quoted too.

      I missed that bit in your reply. I was thinking of responding with a semi-elaborate regex approach, but if there's an SQL parser available on CPAN, I agree that's the way to go: wheel, reinvention, and all that.


      Give a man a fish:  <%-{-{-{-<