Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

How would I go about making a regex that would only match the data provided in a scalar ,nothing more, nothing less. Would I use a specific quantifier??

For example, say that I am matching a scalar provided from STDIN that looks like this:
user1. How would I get it to match ONLY the user1 not the user11 or user12 and so on.....

Any help would be appreciated..

Replies are listed 'Best First'.
Re: Exclusive pattern matching..
by Abigail-II (Bishop) on Jun 21, 2002 at 18:17 UTC
    By not using a regex of course! If you want exact matching, you'd use the eq operator.

    Abigail

      that wouldn't work too well for "i am user1" eq "user1", he probably meant something like  m{\b?\Q$word\E\b?}
Re: Exclusive pattern matching..
by Dog and Pony (Priest) on Jun 21, 2002 at 18:17 UTC
    Use eq instead. :)
    my $input = shift; if($input eq $scalar) ....
    If you really want a regexp for this though, you can use ^ and $ like this:
    $string =~ /^user1$/;
    ^ matches only at the beginning of the string, and $ only at the end, so this way you force it to match only if it matches the whole string.
    You have moved into a dark place.
    It is pitch black. You are likely to be eaten by a grue.
Re: Exclusive pattern matching..
by robobunny (Friar) on Jun 21, 2002 at 18:13 UTC
    you can indicate the beginning of a string with \A and the end of a string with \Z. ^ matches the beginning of lines, and $ matches the end of lines. so you could use something like /^user1$/ or /\Auser1\Z/. of course, if you just want an exact match, you can always use $var eq 'user1'.