Solved

Some pretty basic questions regarding regexp's

Posted on 2003-10-27
8
201 Views
Last Modified: 2008-07-03
Hi,

I'm using this regexp with preg_match:

"/^(\D*\d\D*){1,3}$/"

to determine if a string contains minimum 1 maximum 3 numbers in it. (For a new, user-entered password)
It seems to work, but I was wondering if this is right and/or this could be done more easy!

I also want to check if a character (a-z, A-Z) was not entered more then 3 times subsequently (Donno how to say it correctly in english :-) )
Like rTaaaa12 -> the "aaaa" should make the pass invalid.

If anyone has suggestions/links to existing password validation regexp's, they could be usefull too offcourse.

Any help appreciated!
0
Comment
Question by:mrjoe
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 8

Accepted Solution

by:
inq123 earned 130 total points
ID: 9631226
Hi mrjoe,

that's correct and I'm afraid it can't be simpler than that.  For 'aaa' or longer, I wrote this regex for you (verified it and it works well):

/(.)(?=\1{2,})/

Cheers!
0
 
LVL 8

Expert Comment

by:inq123
ID: 9631229
just noticed that you don't want a char to be more than 3 times, then use this regex:

/([a-zA-Z])(?=\1{2,})/
0
 

Author Comment

by:mrjoe
ID: 9633922
I'm having problems with your regexps, and with my other regexps in general. See when I try your first regex:

/(.)(?=\1{2,})/       it doesn't work.

I looked through the php manual and understand completely (I think =) ) what you are doing, but some things just don't seem to work with me :/

When I try to simplify the expression in order to find anything wrong and I try this:

/(a)(?=a{2,})/

..then this works, matches 'aaa' or longer.

But /([a-zA-Z])(?=\1{2,})/   or /(\D)(?=\1{2,})/ won't work either...

How come this backreferencing with \1 is not working? And I get the impression that sometimes other things that won't work, are actually right and should work. What could there be wrong? Any help greatly appreciated, I'm getting curious about this problem. Points slightly increased too.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Expert Comment

by:inq123
ID: 9633984
My regex definitely works as I tried both in Perl and PHP.  The only reason why it doesn't work my guess would be that your php version is too low to support lookahead regex (or rather, the \1 back reference?).  Try $1 if that doesn't work, since in php there's a move away from \1 to $1 since old syntax was always \1 but newer php it's $1.  So this might be a problem if your php is too new.  But my php's rather new too (4.3.3) and also php was trying to move closer to perl (it's using PCRE) so because Perl's still using \1, php should too.  But it doesn't hurt to try.

Nothing against you at all, but I feel rather frustrated that simple Q/A like this one would always go wrong, and it always has something to do with OS, version of the interpreter, or user execution.  Wouldn't it be nice that every software has only one version and all OS are the same? :-)
0
 

Author Comment

by:mrjoe
ID: 9634231
Wouldn't that be something :-)

And since indeed your regex's look perfectly right to me, and it's a personal problem here, the points are yours.

Thanks for your help.
0
 
LVL 8

Expert Comment

by:inq123
ID: 9634269
Thanks for the points and I do want to help you solve this!  So please let me know if $1 works for you, and maybe let me know what other problem you have with your regex in general?  Would that give a little clue? (desperately looking for clue here ... :-) )
0
 

Author Comment

by:mrjoe
ID: 9634440
Very nice of you :-)

$1 didn't work, upgrading my local php server (did had an old pcre lib :/ ) didn't worked either, but then... I tried ' instead of " for the pattern string.....

preg_match('/(.)(?=\1{2,})/',$pass) instead of preg_match("/(.)(?=\1{2,})/",$pass)

This works! Offcourse, I can also just add an extra backslash ( \\1 instead of \1 ) for it to work.

*shame* why didn't I think of this earlier! But these are my first steps with regexp's :p

But since this was a problem, I guess that most of you always use ' ? At the moment, I find it strange I found so much examples with " since you probably have to escape more characters.

Thanks again for helping me out with this, I'm happy it works now :-)
0
 
LVL 8

Expert Comment

by:inq123
ID: 9634496
I see.  For some reason, if I didn't see preg_match("/regex/", $somestring) then I never think of the problem of " or '.  Should've thought of that earlier.  I'm on a different computer now, so I can't check why it worked for me on php, I must have done something almost subconciously.  But on perl, there's no complication like this as quote is not needed.

Anyway glad it finally worked.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question