Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Some pretty basic questions regarding regexp's

Posted on 2003-10-27
8
Medium Priority
?
207 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 520 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

This article discusses how to implement server side field validation and display customized error messages to the client.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

715 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