in reply to Re: RegEx ignoring intervening characters?
in thread RegEx ignoring intervening characters?

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

Replies are listed 'Best First'.
Re^3: RegEx ignoring intervening characters?
by imp (Priest) on Jan 19, 2007 at 21:12 UTC
    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);
Re^3: RegEx ignoring intervening characters?
by webfiend (Vicar) on Jan 19, 2007 at 21:59 UTC
    (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!