| | |
Regular Expression
![]() |
•
•
Join Date: Mar 2008
Posts: 11
Reputation:
Solved Threads: 0
Hi, new to Perl. Learning regular expressions. Im trying to validate a form field using regular expressions. Field criteria is:
- Begin with a letter
- 4-8 characters long
- must include at least 1 digit
So far Ive got:
I read this as "beginning with any non-digit [^0-9], containing between 3 and 7 alphanumeric characters and having one or more of the previous character which is any digit"
I dont understand it, but \w{3,7} seems to satisfy having 4-8 alphanumeric long?
The following test usernames fall over:
a1bcdefg
ab1cdefg
abc1defg
The following are accepted:
abcd1efg
abcd1efg
abcdef1g
abcdefg1
So the location of the digit seems to be a factor, but not sure why.
- Begin with a letter
- 4-8 characters long
- must include at least 1 digit
So far Ive got:
Perl Syntax (Toggle Plain Text)
$fieldValid=$username=~/^\D\w{3,7}\d+/;
I read this as "beginning with any non-digit [^0-9], containing between 3 and 7 alphanumeric characters and having one or more of the previous character which is any digit"
I dont understand it, but \w{3,7} seems to satisfy having 4-8 alphanumeric long?
The following test usernames fall over:
a1bcdefg
ab1cdefg
abc1defg
The following are accepted:
abcd1efg
abcd1efg
abcdef1g
abcdefg1
So the location of the digit seems to be a factor, but not sure why.
•
•
Join Date: Sep 2007
Posts: 176
Reputation:
Solved Threads: 20
You may have to define 'character' a little better for your purposes. (Will you allow *?!@#$%^&()<>:;'~`)?
But the following works, while a bit kludgy:
But the following works, while a bit kludgy:
Perl Syntax (Toggle Plain Text)
#! /usr/bin/perl use strict; use warnings; # Begin with a letter # 4-8 characters long # must include at least 1 digit while (<DATA>) { chomp; if ( (length($_) >= 4) && (length($_) <= 8) ) { if ( /^[a-z]/) { if ( /\d+/g) { print "$_ : OK\n" ; } } } else { print "$_ : NO\n"; } } __DATA__ 1ab abc a1b ab1 abc1 1abc a1bc ab1c 1a1bcdefg a1bcdefg ab1cdefg abc1defg abcd1efg abcd1efg abcdef1g abcdefg1 a1234567 12345678 123456789 abcdefgh abcdefghi
Amer Neely - Web Mechanic
"Others make web sites. We make web sites work!"
"Others make web sites. We make web sites work!"
•
•
Join Date: Mar 2008
Posts: 11
Reputation:
Solved Threads: 0
Not sure how to use two regexps. Can you put them in the same statement like so:
or would I have to use a while loop, something like:
Which I read as "while there is a digit in $username, test the regular expression which if satisfied returns $fieldValid = true. If not satisfied, $fieldValid = false. Unless $fieldValid is true, then set $formValid = 0. If there is NOT a digit in $userName, then set $formValid = 0".
If the second way works, it sounds like a long winded way of doing it.
Perl Syntax (Toggle Plain Text)
$fieldValid=$username=~/^[a-zA-Z][a-zA-Z0-9]{3,7}/ && /\d/;
Perl Syntax (Toggle Plain Text)
$formValid=1; while ($username=~/\d/){ $fieldValid=$username=~/^[a-zA-Z][a-zA-Z0-9]{3,7}/; unless ($fieldValid) {$formValid=0 } }$formValid=0;
If the second way works, it sounds like a long winded way of doing it.
Perl Syntax (Toggle Plain Text)
if ($username =~ /\d/ && $username =~ /^[a-zA-Z][a-zA-Z0-9]{3,7}/) { $username is good do whatever you want } else { $username is bad }
But that might still not be good enough. The above will match strings like:
a1111111111111111111111111111111111111111111111111111111111111.....
if you need to match a specific length you have to add the end of string anchor ($) to the second regexp:
Perl Syntax (Toggle Plain Text)
if ($username =~ /\d/ && $username =~ /^[a-zA-Z][a-zA-Z0-9]{3,7}$/) { $username is good do whatever you want } else { $username is bad }
you could do something like
but i would rather
perl Syntax (Toggle Plain Text)
if (str =~ /^[a-z](?:\d\w{2,6}|\w{2,6}\d|\w\d\w{1,5}|\w{2}\d\w{0,4}|\w{3}\d\w{0,3}|\w{4}\d\w{0,2}|\w{5}\d\w?)$/i) { ... stuff ... }
but i would rather
perl Syntax (Toggle Plain Text)
if (str =~ /^[a-z](\w{3,7})/ ) { if ($1 =! /\d/ || $1 =! /[A-Z]/ || $1 != /[a-z]/) { die "must have one number, cap, and lowercase!\n"; } ... stuff ... }
Last edited by jephthah; Mar 16th, 2008 at 7:19 pm.
•
•
Join Date: Mar 2008
Posts: 11
Reputation:
Solved Threads: 0
Hi guys, thanks for all your responses. Think I found what I was after, all in one regex.
The one offered by jephthah allows non-letters at the start, so &? etc allowed.
I managed to use a condensed regex using a lookahead:
Curious about the /x modifier tho, because I have another regex thats taking up about two page widths - is /x a way to split your code over 2 or more lines?
The one offered by jephthah allows non-letters at the start, so &? etc allowed.
I managed to use a condensed regex using a lookahead:
Perl Syntax (Toggle Plain Text)
/(?=^[a-zA-Z0-9]{4,8}$)[a-zA-Z][a-zA-Z0-9]{0,2}\d+/
Curious about the /x modifier tho, because I have another regex thats taking up about two page widths - is /x a way to split your code over 2 or more lines?
•
•
•
•
The one offered by jephthah allows non-letters at the start, so &? etc allowed.
and i'm afraid to tell you, your last attempt won't work at all, for anything. maybe mine ain't pretty, but it works.
•
•
•
•
Curious about the /x modifier tho, because I have another regex thats taking up about two page widths - is /x a way to split your code over 2 or more lines?
Last edited by jephthah; Mar 17th, 2008 at 2:49 pm.
![]() |
Similar Threads
- Regular Expression (Perl)
- How to write javascript regular expression for this case? (JSP)
- regular expression (PHP)
- Regular Expression Matching New Line (Perl)
- Regular expression (Perl)
- Looking for table rows with Regular expression. (PHP)
Other Threads in the Perl Forum
- Previous Thread: urgent help in perl script
- Next Thread: Newbie questions: how to show a listing in pages
| Thread Tools | Search this Thread |







