Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: RegEx ignoring intervening characters?

by imp (Priest)
on Jan 19, 2007 at 20:59 UTC ( #595593=note: print w/ replies, xml ) Need Help??


in reply to RegEx ignoring intervening characters?

You could do something like this:

use strict; use warnings; my $stuff = qr/[^A-Z]*/; my $regex = qr{ A $stuff B $stuff C $stuff D }x; while (my $line = <DATA>) { if ($line =~ $regex) { print "Matched: $line"; } } __DATA__ ABhere is intervening textC D A B C, lots of text and numbers and equals and slashes DEFG ABhere IS intervening TEXTC D
And an alternate way of forming the pattern:
my $stuff = qr/[^A-Z]*/; my $regex = join($stuff, split '','ABCD');


Comment on Re: RegEx ignoring intervening characters?
Select or Download Code
Re^2: RegEx ignoring intervening characters?
by ww (Bishop) on Jan 19, 2007 at 22:17 UTC
    Niggle:

    edge case? specs?

    To illustrate; note the use of three (In some ways, more precise, I think but am inviting begging for other views, please?) distinct regexen, $stuff1, $stuff2, and $stuff3 and the last line of __DATA__ and the LAST line of output

    use strict; use warnings; my $stuff1 = qr/[^A]|[^C-Z]*/; my $stuff2 = qr/[^A-B]|[^D-Z]*/; my $stuff3 = qr/[^A-C]|[^E-Z]*/; my $regex = qr{ A $stuff1 B $stuff2 C $stuff2 D }x; while (my $line = <DATA>) { chomp($line); if ($line =~ $regex) { print "Matched: \" $line \"\n"; } else { print "**Did NOT match \"$line\"\n"; } } __DATA__ ABhere is intervening textC D A B C, lots of text and numbers and equals and slashes DEFG ABhere IS intervening TEXTC D A BIG foo is B intervening text CD A Big Cat interjects itself into text before CD

    OUTPUT:

    Matched: " ABhere is intervening textC D "
    Matched: " A B C, lots of text and numbers and equals and slashes DEFG "
    **Did NOT match "ABhere IS intervening TEXTC D"
    **Did NOT match "A BIG foo is B intervening text CD"
    Matched: " A Big Cat interjects itself into text before CD "

    In the last line of __DATA__ an uppercase "C" preceeds another uppercase "C" (penultimate character), yet the regex does not object (i.e., says there's a match).

    Update: Someone upvoted this as I was updating it -- to fix mental and typographic glitches; the said updating may have removed what the ++er thought was meritorious. Sorry.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (13)
As of 2014-08-20 15:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (118 votes), past polls