http://www.perlmonks.org?node_id=595593


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');

Replies are listed 'Best First'.
Re^2: RegEx ignoring intervening characters?
by ww (Archbishop) 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.