in reply to Re: simple perl regex question (or is it?)
in thread simple perl regex question (or is it?)

Hi Rolf, thanks for your quick reply. The look-ahead did the trick:
$ perl -le '$str = q{AND (random text) OR (more random text) AND (yet +(more))};print "$1: $2" while ($str =~ /(AND|OR)\s+(.+?)(?=AND|OR|$)/ +g)' AND: (random text) OR: (more random text) AND: (yet (more))
In this exercise, I don't know in advance whether $1 will match "AND" or "OR", so I do want that value captured, which is why I didn't want to use split.

Funny that you should ask about embedded nested ANDs or ORs. The system I'm working on would benefit from this, but I'd have to deal with balanced parentheses and use recursion probably to parse the entire structure. Assuming I could refine to code to do this accurately, I'd probably just end up hung by my own petard...

Many thanks! -Harry

Replies are listed 'Best First'.
Re^3: simple perl regex question (or is it?)
by AnomalousMonk (Archbishop) on Nov 11, 2013 at 18:30 UTC
    ... embedded nested ANDs or ORs. The system I'm working on would benefit from this ...

    Easily enough done with an approach like that of Re: simple perl regex question (or is it?) (and probably others as well): just take whatever you get from the  $nested_parens portion of the match (with or without the enclosing ()s) and run it through the main regex again! Repeat until there is no match, i.e., no nested  AND|OR(...) to be found.

Re^3: simple perl regex question (or is it?)
by Anonymous Monk on Nov 11, 2013 at 07:45 UTC