Regular Expression

Reply

Join Date: Mar 2008
Posts: 11
Reputation: bradleykirby is an unknown quantity at this point 
Solved Threads: 0
bradleykirby bradleykirby is offline Offline
Newbie Poster

Regular Expression

 
0
  #1
Mar 13th, 2008
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:

  1. $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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 176
Reputation: trudge is an unknown quantity at this point 
Solved Threads: 20
trudge trudge is offline Offline
Junior Poster

Re: Regular Expression

 
0
  #2
Mar 14th, 2008
You may have to define 'character' a little better for your purposes. (Will you allow *?!@#$%^&()<>:;'~`)?

But the following works, while a bit kludgy:
  1. #! /usr/bin/perl
  2. use strict;
  3. use warnings;
  4.  
  5. # Begin with a letter
  6. # 4-8 characters long
  7. # must include at least 1 digit
  8.  
  9. while (<DATA>)
  10. {
  11. chomp;
  12. if ( (length($_) >= 4) && (length($_) <= 8) )
  13. {
  14. if ( /^[a-z]/)
  15. {
  16. if ( /\d+/g)
  17. {
  18. print "$_ : OK\n" ;
  19. }
  20. }
  21. }
  22. else
  23. {
  24. print "$_ : NO\n";
  25. }
  26. }
  27.  
  28. __DATA__
  29. 1ab
  30. abc
  31. a1b
  32. ab1
  33. abc1
  34. 1abc
  35. a1bc
  36. ab1c
  37. 1a1bcdefg
  38. a1bcdefg
  39. ab1cdefg
  40. abc1defg
  41. abcd1efg
  42. abcd1efg
  43. abcdef1g
  44. abcdefg1
  45. a1234567
  46. 12345678
  47. 123456789
  48. abcdefgh
  49. abcdefghi
Amer Neely - Web Mechanic
"Others make web sites. We make web sites work!"
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 898
Reputation: KevinADC has a spectacular aura about KevinADC has a spectacular aura about 
Solved Threads: 67
KevinADC's Avatar
KevinADC KevinADC is offline Offline
Practically a Posting Shark

Re: Regular Expression

 
0
  #3
Mar 14th, 2008
/^\D\w{3,7}\d+/;

the above means:

^\D starts with one non-digit character
\w{3,7} followed by 3 to 7 word characters, same as a-zA-Z0-9_
\d+ followed by one or more digits

what you probably want is is two regexps:

/\d/ has at least one digit

/^[a-zA-Z][a-zA-Z0-9]{3,7}/;
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 11
Reputation: bradleykirby is an unknown quantity at this point 
Solved Threads: 0
bradleykirby bradleykirby is offline Offline
Newbie Poster

Re: Regular Expression

 
0
  #4
Mar 14th, 2008
Not sure how to use two regexps. Can you put them in the same statement like so:

  1. $fieldValid=$username=~/^[a-zA-Z][a-zA-Z0-9]{3,7}/ && /\d/;
or would I have to use a while loop, something like:
  1.  
  2. $formValid=1;
  3. while ($username=~/\d/){
  4. $fieldValid=$username=~/^[a-zA-Z][a-zA-Z0-9]{3,7}/;
  5. unless ($fieldValid)
  6. {$formValid=0
  7. }
  8. }$formValid=0;
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 898
Reputation: KevinADC has a spectacular aura about KevinADC has a spectacular aura about 
Solved Threads: 67
KevinADC's Avatar
KevinADC KevinADC is offline Offline
Practically a Posting Shark

Re: Regular Expression

 
0
  #5
Mar 15th, 2008
  1. if ($username =~ /\d/ && $username =~ /^[a-zA-Z][a-zA-Z0-9]{3,7}/) {
  2. $username is good do whatever you want
  3. }
  4. else {
  5. $username is bad
  6. }

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:

  1. if ($username =~ /\d/ && $username =~ /^[a-zA-Z][a-zA-Z0-9]{3,7}$/) {
  2. $username is good do whatever you want
  3. }
  4. else {
  5. $username is bad
  6. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,602
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 120
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: Regular Expression

 
0
  #6
Mar 16th, 2008
you could do something like
  1. 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)
  2. {
  3. ... stuff ...
  4. }

but i would rather
  1. if (str =~ /^[a-z](\w{3,7})/ )
  2. {
  3. if ($1 =! /\d/ || $1 =! /[A-Z]/ || $1 != /[a-z]/)
  4. {
  5. die "must have one number, cap, and lowercase!\n";
  6. }
  7.  
  8. ... stuff ...
  9. }
Last edited by jephthah; Mar 16th, 2008 at 7:19 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,602
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 120
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: Regular Expression

 
0
  #7
Mar 17th, 2008
hmm.... my long regex above is cut off by the webcode formatting.

that's kind of crappy. and all that after everyone bitches about people not using the webcode tags

anyhow, you'll have to click the "toggle plain text" to see it.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,625
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 716
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Regular Expression

 
0
  #8
Mar 17th, 2008
>that's kind of crappy.
Yes, it is. You should take advantage of the /x modifier.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 11
Reputation: bradleykirby is an unknown quantity at this point 
Solved Threads: 0
bradleykirby bradleykirby is offline Offline
Newbie Poster

Re: Regular Expression

 
0
  #9
Mar 17th, 2008
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:

  1. /(?=^[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?
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,602
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 120
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: Regular Expression

 
0
  #10
Mar 17th, 2008
Originally Posted by bradleykirby View Post
The one offered by jephthah allows non-letters at the start, so &? etc allowed.
what are you talking about? it does not in any sense allow non letters at the start, or anywhere else for that matter ... it will allow the underscore "_" in any position other than the first, which is typically allowed (and desired) anywhere alphanumerics are 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?
yeah, narue just busted me. it means "ignore whitespace" ... so you can put a CR/LF in there for readability.
Last edited by jephthah; Mar 17th, 2008 at 2:49 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC