Solved

Some pretty basic questions regarding regexp's

Posted on 2003-10-27
8
193 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
  • 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now