You need to anchor your regex.  Your regex is matching '!!' because it
is matching an exclamation point followed by zero or more non
exclamation-point characters anywhere in the string.

Thus the first '!' is matching the regex, and the second '!' is outside
of your match.

Try this:

##################

use strict;
use warnings;

my $str = '!!';

print "$str\n" if $str =~ /^\![^!]*$/;

#################

The ^ in the match denotes the start of the string, while the $ denotes
the end (with an optional \n character).

-----Original Message-----
From: Jim [mailto:[EMAIL PROTECTED] 
Sent: Tuesday, April 18, 2006 5:52 PM
To: beginners@perl.org
Subject: Regex Help

i am trying to match a '!' followed by any char but a '!' or no chars
(string is only a '!')

this is what I have and it is not working:

$str = "!!";
# this is not working.  it is matching "!!"
print "$str\n" if $str =~ /\![^!]*/;

Thanks for any help



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to