Solved

Some pretty basic questions regarding regexp's

Posted on 2003-10-27
8
204 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Get data from two MySQL tables 6 66
Load string Array from file 23 42
Conditional Array Element 7 28
Tell me if I understand this correctly... 6 21
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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 …

710 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