Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

realizing AND in regex?

by LanX (Chancellor)
on Sep 13, 2012 at 09:59 UTC ( #993441=perlquestion: print w/replies, xml ) Need Help??
LanX has asked for the wisdom of the Perl Monks concerning the following question:

Hi

I'm using the regex engine to identify delimited fields matching certain conditions.

Thanks to Perl's internal trie optimization of OR-conditions¹ it's far faster than using LIKE in mysql especially with hundreds of patterns to check

$text = 'A0 peter Z0 ... A42 peter, paul and mary Z42 ... A99 mary Z9 +9'; my @or_matches = ( $text =~ m/A(\d+)[^Z]*(peter|mary)[^Z]*Z/g ); print "@or_matches \n"; __END__ 0 peter 42 mary 99 mary

But now I got the requirement to find fields which match multiple regex at the same time ... and AFAIK the regex grammar doesn't have an AND operator

The best guess I have is using zero-look-ahead assertions:

$text = 'A0 peter Z0 ... A42 peter, paul and mary Z42 ... A99 mary Z9 +9'; my @and_matches =( $text =~ m/ A(\d+)[^Z]* ( (?=mary) [^Z]* peter | (?=peter) [^Z]* mary ) [^Z]*Z /xg ); print "@and_matches \n"; __END__ 42 peter, paul and mary

Well, already rather complicated for just two patterns ... and I doubt that it's fast ... any better suggestions?

Cheers Rolf

UPDATE:

Ok the following is already much better since it avoids or-chaining all possible orders of patterns just by anchoring the look-ahead at field-start.

print @and_matches =( $text =~ m/ A(\d+) ( (?= [^Z]* mary ) (?= [^Z]* peter ) [^Z]* ) Z\1 /xg );
Footnotes:

¹) >5.10 IIRC

Replies are listed 'Best First'.
Re: realizing AND in regex?
by BillKSmith (Priest) on Sep 13, 2012 at 12:26 UTC

    It is certainly easier and posibly faster to use separate regexes. Use perl's logic rather than the regex engine to combine the results.

    Bill
Re: realizing AND in regex?
by pvaldes (Chaplain) on Sep 13, 2012 at 22:39 UTC

    I got the requirement to find fields which match multiple regex at the same time

    while (<DATA>){ if (/foo/ && /bar/){ print $_ } } __DATA__ fobur barfoo foobar fobar bar foo

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://993441]
Approved by chacham
help
Chatterbox?
[choroba]: We're hiring
[Discipulus]: wow praha!
[Tanktalus]: choroba: do I have to move? :)
Discipulus has too much roots in a 2770 yo town
[marioroy]: I want a job after completing MCE in about a week. But feel that I've moved to the wrong place.
[choroba]: I fear so. We have offices in Prague, Brno, Saigon and San Francisco.
[Discipulus]: choroba if let your company to hire Tanktalus and marioroy ... what a big fishing!
[Discipulus]: i choice prague!!
[choroba]: 1200 years is enough :-)
[marioroy]: Lady_Aleena my $ans = qx! find . -name "*.pl" -exec ls -l {} \\; !;

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (9)
As of 2017-04-23 20:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I'm a fool:











    Results (432 votes). Check out past polls.