RE: Regex Help




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:jkipp5@xxxxxxxxxxx]
Sent: Tuesday, April 18, 2006 5:52 PM
To: beginners@xxxxxxxx
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


.



Relevant Pages

  • Re: Exact matching using GREP.
    ... > I saw the Unix man pages and did other searches and found '-x' switch to use for exact matching with GREP and '-e' switch for pattern matching but dont' know how to incorporate it within my code. ... Any regex wildcards in your variable are interpolated into the regex ... of the string, 'php', and the end of the string." ...
    (perl.beginners)
  • Re: regular expression again
    ... I want to retrieve all occurences of a string "aa". ... So in the search string ... matching failed, ... According to the regex enginebehind Perl, ...
    (comp.lang.perl.misc)
  • Re: regular expression again
    ... I want to retrieve all occurences of a string "aa". ... So in the search string ... matching failed, ... According to the regex enginebehind Perl, ...
    (comp.lang.perl.misc)
  • Re: locate regex
    ... grep and awk return the matched regular expression line. ... interested in retrieving the WORD matching the regex and not the entire ... But what I am interested in retrieving is just tpe_VerfSetting.htm ... Here teh regex is enclosed in <%@...%? ...
    (comp.lang.awk)
  • Re: 40tude Dialog (regex query): How to test for no vowels in Subject?
    ... whole string in a RegEx, while they seem to be comfortable with partial ... matches in a string match. ... Two reasons: a special one applying here and a more general one: The ... it will be very hard for many to extend the partial matching form... ...
    (news.software.readers)