regex not matching high end

A

Anatase

if(!($input2 =~ /[a-zA-Z]{4,12}/))
{
print "try the input again\n";
Usernamesub();
}

aaa - does not match so steps in
aaaa - match so steps past
aaaaaaaaaaaaa - 13+ should step in - but instead steps past

I'm using active states perl 5.8 it seems.
 
A

Anno Siegel

Anatase said:
if(!($input2 =~ /[a-zA-Z]{4,12}/))
{
print "try the input again\n";
Usernamesub();
}

aaa - does not match so steps in
aaaa - match so steps past
aaaaaaaaaaaaa - 13+ should step in - but instead steps past

No. There *are* 12 characters in "aaaaaaaaaaaaa", and the regex matches
them.

To get the behavior you expect you must anchor the regex on both ends:

/^[a-zA-Z]{4,12}$/

Anno
 
J

John Bokma

Anatase said:
if(!($input2 =~ /[a-zA-Z]{4,12}/))
{
print "try the input again\n";
Usernamesub();
}

aaa - does not match so steps in
aaaa - match so steps past
aaaaaaaaaaaaa - 13+ should step in - but instead steps past

because it matches, it contains at least 12 a's.

/^[a-zA-Z]{4,12}$/

is probably what you want, at least 4, max 12 letters.

Also, if(!...) { is often more readable written as unless () {
 
C

Chris Mattern

Anatase said:
if(!($input2 =~ /[a-zA-Z]{4,12}/))
{
print "try the input again\n";
Usernamesub();
}

aaa - does not match so steps in
aaaa - match so steps past
aaaaaaaaaaaaa - 13+ should step in - but instead steps past

Well, of course it does. The string has twelve "a"s in it.
It has some more "a"s after that, but that doesn't change the
fact that it has twelve "a"s.
I'm using active states perl 5.8 it seems.

--
Christopher Mattern

"Which one you figure tracked us?"
"The ugly one, sir."
"...Could you be more specific?"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,147
Messages
2,570,835
Members
47,382
Latest member
MichaleStr

Latest Threads

Top