By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,086 Members | 1,875 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,086 IT Pros & Developers. It's quick & easy.

Regular Expressions

P: n/a
I am trying to work out a regular expression which will validate a
password box.

The following rules apply
Must be 8 characters
Must have at least one digit (0-9) and at least one character (a-z or
A-Z)
No special characters allowed except for full stop.

I am struggling to get to grips with regular expressions. Can anyone
help me with this please.
Thanks in anticipation

Ros Lee
Nov 22 '05 #1
Share this Question
Share on Google+
14 Replies


P: n/a
Maybe we need to && the conditional statements with three regex patterns:
r1 = [a-zA-Z0-9]{8}
r2 = [a-zA-Z]{1,7}
r3 = [0-9]{1,7}

or just use r2 "anded" to r3 with a string length of 8.

Eg.:
option 1: (I like this one better. It has a more definitive and tighter
constraint on the string pattern compared to opt2)
if ((Regex.IsMatch(sPassword, r1)) &&
(Regex.IsMatch(sPassword, r2)) &&
(Regex.IsMatch(sPassword, r3)))

option 2:
if ((Regex.IsMatch(sPassword, r2)) &&
(Regex.IsMatch(sPassword, r3)) &&
(sPassword.Length == 8))
http://www.regular-expressions.info/ has a good tutorial on regex.

Try it see if either works and good luck,
/Js.

"Roz Lee" <ro**@panztel.com> wrote in message
news:b7************************@posting.google.com ...
I am trying to work out a regular expression which will validate a
password box.

The following rules apply
Must be 8 characters
Must have at least one digit (0-9) and at least one character (a-z or
A-Z)
No special characters allowed except for full stop.

I am struggling to get to grips with regular expressions. Can anyone
help me with this please.
Thanks in anticipation

Ros Lee

Nov 22 '05 #2

P: n/a
Maybe we need to && the conditional statements with three regex patterns:
r1 = [a-zA-Z0-9]{8}
r2 = [a-zA-Z]{1,7}
r3 = [0-9]{1,7}

or just use r2 "anded" to r3 with a string length of 8.

Eg.:
option 1: (I like this one better. It has a more definitive and tighter
constraint on the string pattern compared to opt2)
if ((Regex.IsMatch(sPassword, r1)) &&
(Regex.IsMatch(sPassword, r2)) &&
(Regex.IsMatch(sPassword, r3)))

option 2:
if ((Regex.IsMatch(sPassword, r2)) &&
(Regex.IsMatch(sPassword, r3)) &&
(sPassword.Length == 8))
http://www.regular-expressions.info/ has a good tutorial on regex.

Try it see if either works and good luck,
/Js.

"Roz Lee" <ro**@panztel.com> wrote in message
news:b7************************@posting.google.com ...
I am trying to work out a regular expression which will validate a
password box.

The following rules apply
Must be 8 characters
Must have at least one digit (0-9) and at least one character (a-z or
A-Z)
No special characters allowed except for full stop.

I am struggling to get to grips with regular expressions. Can anyone
help me with this please.
Thanks in anticipation

Ros Lee

Nov 22 '05 #3

P: n/a
Just out of curiousity - why use regular expressions? Your problem is
just a few lines of non-regular expression code - why not just code
it the boring old way?

Nov 22 '05 #4

P: n/a
Just out of curiousity - why use regular expressions? Your problem is
just a few lines of non-regular expression code - why not just code
it the boring old way?

Nov 22 '05 #5

P: n/a
Hi Jimi,
Just out of curiousity - why use regular expressions? Your problem is
just a few lines of non-regular expression code - why not just code
it the boring old way?


I am almost writting that forever however in this case, I think it will
needs more lines.
What did you think of?

Cor

Nov 22 '05 #6

P: n/a
Hi Jimi,
Just out of curiousity - why use regular expressions? Your problem is
just a few lines of non-regular expression code - why not just code
it the boring old way?


I am almost writting that forever however in this case, I think it will
needs more lines.
What did you think of?

Cor

Nov 22 '05 #7

P: n/a
I think you can use a positive lookahead to put the "and" into the regex:
"^(?=.*\d)(?=.*[a-zA-Z])(?:.{8,})$"
I tested it only on a few cases, but it seems to work.

(I assume for password verification efficiency isn't that important)

Niki

"John Smith" <.@.> wrote in news:De*****************@twister.socal.rr.com...
Maybe we need to && the conditional statements with three regex patterns:
r1 = [a-zA-Z0-9]{8}
r2 = [a-zA-Z]{1,7}
r3 = [0-9]{1,7}

or just use r2 "anded" to r3 with a string length of 8.

Eg.:
option 1: (I like this one better. It has a more definitive and tighter
constraint on the string pattern compared to opt2)
if ((Regex.IsMatch(sPassword, r1)) &&
(Regex.IsMatch(sPassword, r2)) &&
(Regex.IsMatch(sPassword, r3)))

option 2:
if ((Regex.IsMatch(sPassword, r2)) &&
(Regex.IsMatch(sPassword, r3)) &&
(sPassword.Length == 8))
http://www.regular-expressions.info/ has a good tutorial on regex.

Try it see if either works and good luck,
/Js.

"Roz Lee" <ro**@panztel.com> wrote in message
news:b7************************@posting.google.com ...
I am trying to work out a regular expression which will validate a
password box.

The following rules apply
Must be 8 characters
Must have at least one digit (0-9) and at least one character (a-z or
A-Z)
No special characters allowed except for full stop.

I am struggling to get to grips with regular expressions. Can anyone
help me with this please.
Thanks in anticipation

Ros Lee


Nov 22 '05 #8

P: n/a
I think you can use a positive lookahead to put the "and" into the regex:
"^(?=.*\d)(?=.*[a-zA-Z])(?:.{8,})$"
I tested it only on a few cases, but it seems to work.

(I assume for password verification efficiency isn't that important)

Niki

"John Smith" <.@.> wrote in news:De*****************@twister.socal.rr.com...
Maybe we need to && the conditional statements with three regex patterns:
r1 = [a-zA-Z0-9]{8}
r2 = [a-zA-Z]{1,7}
r3 = [0-9]{1,7}

or just use r2 "anded" to r3 with a string length of 8.

Eg.:
option 1: (I like this one better. It has a more definitive and tighter
constraint on the string pattern compared to opt2)
if ((Regex.IsMatch(sPassword, r1)) &&
(Regex.IsMatch(sPassword, r2)) &&
(Regex.IsMatch(sPassword, r3)))

option 2:
if ((Regex.IsMatch(sPassword, r2)) &&
(Regex.IsMatch(sPassword, r3)) &&
(sPassword.Length == 8))
http://www.regular-expressions.info/ has a good tutorial on regex.

Try it see if either works and good luck,
/Js.

"Roz Lee" <ro**@panztel.com> wrote in message
news:b7************************@posting.google.com ...
I am trying to work out a regular expression which will validate a
password box.

The following rules apply
Must be 8 characters
Must have at least one digit (0-9) and at least one character (a-z or
A-Z)
No special characters allowed except for full stop.

I am struggling to get to grips with regular expressions. Can anyone
help me with this please.
Thanks in anticipation

Ros Lee


Nov 22 '05 #9

P: n/a
> Cor Ligthertwrote:
I am almost writting that forever however in this case, I think it
will needs more lines. What did you think of?
Cor


Suppose the regular expression approach takes 4 lines and 1 hour. My
approach to this trivial problem takes 12 lines and 5 minutes. This
is an easy business decision - there is no need to minimize the
number of lines of code if the tradeoff is this drastic.

I can see using regular expressions for complex problems, but it's
massive overkill for small problems.

Nov 22 '05 #10

P: n/a
> Cor Ligthertwrote:
I am almost writting that forever however in this case, I think it
will needs more lines. What did you think of?
Cor


Suppose the regular expression approach takes 4 lines and 1 hour. My
approach to this trivial problem takes 12 lines and 5 minutes. This
is an easy business decision - there is no need to minimize the
number of lines of code if the tradeoff is this drastic.

I can see using regular expressions for complex problems, but it's
massive overkill for small problems.

Nov 22 '05 #11

P: n/a
Hi Jimi,

We agree your text completly, and now looking the question over again, I
think we agree completly about this subject.

Probably is meant 2 X the 26 basic characters plus 10 numeric characters
plus an limited set of special characters, while I was thinking about all
full possible unicode set for that.

I think now also that it is simple possible without the Regex and that is
the one I always avoid. To add something to your text:

"And it needs 24 hours when updating the program, to find out what was meant
with it"

:-)

Cor

Nov 22 '05 #12

P: n/a
Hi Jimi,

We agree your text completly, and now looking the question over again, I
think we agree completly about this subject.

Probably is meant 2 X the 26 basic characters plus 10 numeric characters
plus an limited set of special characters, while I was thinking about all
full possible unicode set for that.

I think now also that it is simple possible without the Regex and that is
the one I always avoid. To add something to your text:

"And it needs 24 hours when updating the program, to find out what was meant
with it"

:-)

Cor

Nov 22 '05 #13

P: n/a
Hi Jimi,

I definitely agree with your line of reasoning. But in this regard, I'm not
sure if the 1h to 5 mins ratio apply. We are, after all, talking about
validating only a few characters, and not a full text file. So short and
expressiveness wherever applicable is also a good choice.

Niki also gave a more concise pattern. So :) in the end it depends to the
programmer to choose which method she wants to use. We're merely responding
to her question with an expected type of answer, rather than ask why not
this or that.

/Js.

"Jimi" <ji************@yahoo-dot-ca.no-spam.invalid> wrote in message
news:40**********@127.0.0.1...
Cor Ligthertwrote:

I am almost writting that forever however in this case, I think it
will needs more lines. What did you think of?
Cor


Suppose the regular expression approach takes 4 lines and 1 hour. My
approach to this trivial problem takes 12 lines and 5 minutes. This
is an easy business decision - there is no need to minimize the
number of lines of code if the tradeoff is this drastic.

I can see using regular expressions for complex problems, but it's
massive overkill for small problems.

Nov 22 '05 #14

P: n/a
"Jimi" <ji************@yahoo-dot-ca.no-spam.invalid> wrote in
news:40**********@127.0.0.1...
...
Suppose the regular expression approach takes 4 lines and 1 hour.
I didn't spend more than 5 minutes on that regex (including testing).
Maybe your regex skills need some training ;-)
My approach to this trivial problem takes 12 lines and 5 minutes.
Including testing? (buffer-overruns, chinese unicode characters...)
(The .net regex-engine already is well-tested)
This is an easy business decision - there is no need to minimize the
number of lines of code if the tradeoff is this drastic.
You don't know the OP's code.
Maybe the password-check-rules should be stored in a configuration file?
Or maybe an admin should be able to modify them?
Maybe the OP uses a validation framework that will only accept regular
expressions?
Or maybe she simple wants to get a better understanding of regular
expressions.
I can see using regular expressions for complex problems, but it's
massive overkill for small problems.


Definitely, but it's hard to tell if this is one of the complex problems or
only a simple one. (see above)

Niki
Nov 22 '05 #15

This discussion thread is closed

Replies have been disabled for this discussion.