Could you help me with this please. This regular expression seems to match for the wrong input
#!/usr/bin/perl
my $inputtext = "W1a$%XXX";
if($inputtext =~ m/[a-zA-Z][a-zA-Z0-9]+X+/)
{
print "matches\n";
}
The problem seems to be %. if inputtext is W1a$XXX, the regex doesnot match. but putting a % makes it match.
As you've placed the input text in double quotes which will expand variables, $inputtext may not really be what you think it is!
actually i changed the code a little...the snippet i am using is this and getting the same answer.
#!/usr/bin/perl
my $inputtext = <STDIN>;
if($inputtext =~ m/[a-zA-Z][a-zA-Z0-9]+X+/)
{
print "matches\n";
}
turk451
September 20, 2010, 8:18pm
4
Let me give some output from your script to make sure we're on the same page here:
eturk-linux 17:12:11 (~/bin)> ./test.pl
aaX
matches
eturk-linux 17:14:19 (~/bin)> ./test.pl
W1X
matches
eturk-linux 17:14:34 (~/bin)> ./test.pl
XXX
matches
eturk-linux 17:14:39 (~/bin)> ./test.pl
W1a$XXX
matches
eturk-linux 17:14:50 (~/bin)> ./test.pl
W1a$%XXX
matches
eturk-linux 17:15:02 (~/bin)> ./test.pl
W1a$XX
eturk-linux 17:15:15 (~/bin)> ./test.pl
W1a$%XX
Not to dwell too much on what you have written, just note that your regex matches:
one alphabetic character, one or more alphanumeric characters, one or more 'X' characters
So, the '$' and '%' are not being matched at all. It is the 'XXX' string that is being matched (following the '$' and/or '%'). 'XXX' satisfies the pattern.
1 Like
turk451
September 21, 2010, 2:56pm
6
Yeah, with the beginning and end of line special characters, you are restricting the pattern effectively.
Glad to know it worked for you!