The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages - More
> Regex Programming
|
Regexp with positive look-ahead
Discuss Regexp with positive look-ahead in the Regex Programming forum on Dev Shed. Regexp with positive look-ahead Regular expressions forum covering PCRE and POSIX techniques, practices, and standards. Regular expressions help shorten coding time by providing the ability to compact many lines of code into one string.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

September 26th, 2008, 12:25 PM
|
|
|
|
Regexp with positive look-ahead
Hi,
I need to evaluate a string; the string should be atleast 6 characters in length, consist of letters and numbers and at least 2 of those characters should be numbers.
As far as I know, I need something like a 'positive look-ahead' to achive this? I googled around a bit, used some existing patterns and finally added a bit of my own basic knowledge of regular expressions and came up with this:
Code:
^(?=.*[0-9])[a-z0-9]{6,}$
Which actually works fine, but also validates strings with only one number in it.
Can anyone tell me what I need to change to make it work like I want it to? 
|

September 26th, 2008, 04:30 PM
|
|
|
|
Personally, I would do this in two steps. First verify the string is 6 alphanumerics, then count the instances of digits. Putting everything in one regex works for simple cases but can easily grow out of control.
Anyway, the solution to your problem is in your look ahead that finds a digit, look for a second digit after the first with some number of intervening characters.
__________________
sub{*{$::{$_}}{CODE}==$_[0]&& print for(%:: )}->(\&Meh);
|

September 27th, 2008, 12:51 AM
|
|
|
Quote: | Originally Posted by OmegaZero Anyway, the solution to your problem is in your look ahead that finds a digit, look for a second digit after the first with some number of intervening characters. |
Thanks for your reply, unfortunately you are overestimating my Regex capabilities.  Do you know what exactly I need to change to get the desired result?
|

September 27th, 2008, 03:27 PM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
|
Your lookahead only finds one number. Modify it so that it finds two numbers.
Hint: look for any amount of non-numbers, a digit, more non-numbers, and another digit.
|

September 27th, 2008, 11:02 PM
|
|
|
Quote: | Originally Posted by OmegaZero Anyway, the solution to your problem is in your look ahead that finds a digit, look for a second digit after the first with some number of intervening characters. |
Quote: | Originally Posted by requinix Hint: look for any amount of non-numbers, a digit, more non-numbers, and another digit. |
Now that I read OmegaZero's post again, and the hint from requinix, I finally "got it".
Code:
^(?=.*[0-9].*[0-9])[a-z0-9]{6,}$
I don't know if this Regex is perfect, but it does exactly what I want it to.
-> Reputation given to both of you. 
Last edited by Andreas19 : September 27th, 2008 at 11:05 PM.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|