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

Re: RegEx ignoring intervening characters?

by ikegami (Patriarch)
on Jan 19, 2007 at 15:53 UTC ( [id://595589]=note: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.


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 16: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!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://595589]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.