in reply to RegEx ignoring intervening characters?

$s =~ /A[^A-Z]*B[^A-Z]*C[^A-Z]*D/;
or
# Same thing, but built dynamically. my $re = join '[^A-Z]*', split //, 'ABCD'; $s =~ /$re/;

or

my $temp = $s; $temp =~ s/[^A-Z]//g; $temp =~ /ABCD/;

Replies are listed 'Best First'.
Re^2: RegEx ignoring intervening characters?
by mdunnbass (Monk) on Jan 19, 2007 at 21:05 UTC
    Re: (my $x = $s) =~ s/[^A-Z]//g;

    If I understand your code right, that'd delete anything matching [^A-Z] first, and then match the pattern second, right?

    I guess I should have explained better, but I don't want to modify anything interspersed within the matching ABCD characters. In fact, I very emphatically want them to remain unmolested. So, while your approach looks like it would work, it's not quite what I was looking for.

    As for $s =~ /A[^A-Z]*B[^A-Z]*C[^A-Z]*D/;

    if the $x pattern I am looking for is from uc(chomp($x = <STDIN>)), would I just need to use split, inserting the [^A-Z]* after every character? would that work?

    Thanks
    Matt

      if the $x pattern I am looking for is from uc(chomp($x = <STDIN>)), would I just need to use split, inserting the ^A-Z* after every character? would that work?
      Yes, you would do this:
      my $x = <STDIN>; chomp $x; $x = uc($x); my $regex = join('[^A-Z]*', split //, $x);
      (my $x = $s) =~ s/[^A-Z]//g;

      That assigns the value of $s to $x, then performs the substitution on $x. $s is left unmolested.

      $ perl -e 'chomp($orig = <STDIN>); ($tmp = $orig) =~ s/[^A-Z]//g; prin +t "Copy: $tmp\nOriginal: $orig\n";' WeDfT Copy: WDT Original: WeDfT

      So it's a perfectly valid element of your solution

      Update: Hey, how about I actually show it in action?

      use strict; use warnings; chomp(my $input = <STDIN>); (my $test = $input) =~ s/[^A-Z]//g; if ($test =~ /ABCD/) { print "'$input' matches!\n"; } else { print "'$input' does not match.\n"; }
      $ perl test.pl WaFFe 'WaFFe' does not match. $ [bwisti@w3d145 tmp]$ perl test.pl AeBwaffleCfrenchtoastD 'AeBwaffleCfrenchtoastD' matches!