...

View Full Version : help with "regular expression validator"



simon47
01-11-2013, 04:06 AM
im creating a login page but for the ID part it requires a validation nd the ID should be "s11111111" and there should be different numbers in the ID for multiple students....how can i validate this under the regular expression editor so that it accepts the ID type listed above? i need a validation expression for it..can anyone please help me with these :)

j4v3d
01-11-2013, 05:37 PM
im creating a login page but for the ID part it requires a validation nd the ID should be "s11111111" and there should be different numbers in the ID for multiple students....how can i validate this under the regular expression editor so that it accepts the ID type listed above? i need a validation expression for it..can anyone please help me with these :)

Surely when they are logging you would want to be able to store the ID for that user only and if another user signs up to the website he/she has their own ID generated.

Or are you doing it so the ID is what ever they set?

alykins
01-11-2013, 07:20 PM
I don't know all the BI rules of your validation, but this regex should work for you

^([a-z][A-Z]{0,1})+\d{1,8}

simon47
01-15-2013, 10:53 PM
thanks it works :)

Old Pedant
01-16-2013, 09:10 PM
I don't know all the BI rules of your validation, but this regex should work for you

^([a-z][A-Z]{0,1})+\d{1,8}

That's not correct, Alykins. Not really even close.

That reads:
-- no characters BEFORE what follows (good)
-- a lower case letter
-- FOLLOWED by an optional upper case letter (huh???)
-- that pattern (lower then optional upper) repeated 1 *OR MORE* times
-- followed by from 1 to 8 digits
-- any number of any kinds of characters AFTER the preceding.

So that expression would *ACCEPT*:
s11111111 (as expected)
cZ3xx@#$--+111BB (after the \d{1,8}, *ANYTHING* is allowed)
s33333333333333333333333333333333333333333333 (8 digits accepted, but with no limit on what follows them)
aBcDeFgHiJKlMn4 (lower upper repeated any number of times, 1 digit minimum okay)
zzzzzzzzz77777777 (lower [no optional upper] repeated)
and many others.

From what he posted, I think it should be:
^[a-zA-Z]\d{8,8}$

Which reads:
-- no characters BEFORE what follows
-- one letter, either upper or lower case
-- followed by exactly 8 digits
-- no characters AFTER the preceding.

alykins
01-17-2013, 01:22 PM
thanks for correction- my regex is not too great... i thought the {0,1} specified the first place on the substring (spec'ing it as an a-zA-Z) and then the + \d part was spec'ing only digits for the substring 1-8 {1,8} making it only 9 chars long... obviously not- so what do the {}'s mean?

Edit: Also are regex's standardized across languages (do you know)?

Old Pedant
01-17-2013, 08:51 PM
Each *group* of characters in [...] is treated as "choose one of these".

So when you did
[a-z][A-Z]
that said "choose a lower case letter" and then "choose an upper case letter".

But then you actually used
[A-Z]{0,1}
which means "choose that expression [in this case upper case letter] from 0 to 1 times.

{a,b} means "repeat from a through b times".
{0,1} is equivalent to just ? -- as in [A-Z]?
{1,} is equivalent to just + -- as in [A-Z]+
{0,} is equivalent to just * -- as in [A-Z]*

Anyway, you then grouped all that:
([a-z][A-Z]{0,1}) -- lower case letter followed by 0 through 1 upper case letters
and added the +
([a-z][A-Z]{0,1})+
and as I just said, the + is equivalent to {1,} which means 1 through ANY NUMBER of occurrences
So
([a-z][A-Z]{0,1})+ means "lower case letter followed by optional upper case letter and the whole thing repeated one or more times"

aaaaaaaaaaaaaaaBaBaBaBaBaaaaaaaaaaa would fit that pattern, just for example.

You then did
\d{1,8}
which means (as above) "any digit, repeated 1 through 8 times." SO
7
7131
871122
81712290
would all match that.

But then the big mistake you made was in omitting the $ at the end. Without that, your expression places NO LIMIT on what can follow the REQUIRED part!!!

*********

Not all features of regular expressions are standard across all languages. For example, I have seen some where \d is not recognized as being the same as [0-9]. But most features are the same, including the use of ( ) for grouping, [...] for character choices, ^ and $ for beginning/ending of text matches, and {...} for repetition counts. I don't think I 've seen any language that doesn't support all those.

MySQL has some really funky requirements, just to pick the one oddball I am most familiar with. But it only takes a couple of minutes looking at their docs to get them right.

alykins
01-18-2013, 12:43 PM
Thanks for that info- I hope it is of use to OP as well :)
I have a Regex snap-in but it only tells you if you are *sort of* right... ie it was saying mine was right, but only bc I didn't know all the wrong ones that would get through. Nice break down, the msdn Regex class is a little cryptic (IMO); I need to practice

simon47
01-23-2013, 10:40 AM
nice and interesting...thanks for that very important information..it helped me alot for my studies..thanks :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum