Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: simple perl regex question (or is it?)

by LanX (Canon)
on Nov 11, 2013 at 00:02 UTC ( #1061937=note: print w/ replies, xml ) Need Help??


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

I think you need to anchor the end not the start of your match.

something like /(.*?)(AND|OR|$)/g

if you really need the preceding AND|OR do a positive look ahead

/(AND|OR)(.+?)(?=AND|OR|$)/g

sorry can't test ATM.

(Hopefully you don't expect this to work with nested ANDs and ORs within the parens?)

update

looks good for me HTH

DB<100> $str = q!AND (random text) AND (more random text) AND (yet m +ore)!; => "AND (random text) AND (more random text) AND (yet more)" DB<101> print "$1: $2\n" while $str =~ /(AND|OR)(.+?)(?=AND|OR|$)/g AND: (random text) AND: (more random text) AND: (yet more)

update

if you can deal with leading empty strings try split

DB<103> @matches = split /(AND|OR)/,$str => ( "", "AND", " (random text) ", "AND", " (more random text) ", "AND", " (yet more)", )

Cheers Rolf

( addicted to the Perl Programming Language)


Comment on Re: simple perl regex question (or is it?)
Select or Download Code
Re^2: simple perl regex question (or is it?)
by cadphile (Beadle) on Nov 11, 2013 at 01:41 UTC
    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

      ... 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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1061937]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (11)
As of 2014-10-23 06:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (124 votes), past polls