Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Regular expression matching when it shouldn't

by OeufMayo (Curate)
on Aug 15, 2001 at 13:30 UTC ( #104999=note: print w/ replies, xml ) Need Help??


in reply to Regular expression matching when it shouldn't

Your regular expression does match because it is really present inside the string:
"[someRandomName]." (The text in bracket is the matched text)
"[someRandomName]-"

What you want is anchoring the regular expression. Try adding a '^' at the beginning and a '$' at the end of the regular expression. The Regex will then try to match against the whole string, and not only on a part of it:

/^[A-Z0-9]+[A-Z0-9-]+[A-Z0-9]{1}$/i

Hope this helps. See perlre for more infos about this

#!/usr/bin/perl -w use strict; my $domain = $ARGV[0]; print "Got $domain\n"; if ($domain =~ /^[A-Z0-9]+[A-Z0-9-]+[A-Z0-9]$/i) { print "$domain matched\n\n"; } else { print "$domain did not match\n\n"; } __END__ Got someRandomName. someRandomName. did not match Got someRandomName- someRandomName- did not match Got someRandomName someRandomName matched Got someRandomName-X someRandomName-X matched Got someRandomName-Foo someRandomName-Foo matched
<kbd>--
my $OeufMayo = new PerlMonger::Paris({http => 'paris.mongueurs.net'});</kbd>


Comment on Re: Regular expression matching when it shouldn't
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://104999]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (17)
As of 2014-07-14 14:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (263 votes), past polls