Regex Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming Languages - MoreRegex Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 26th, 2008, 12:25 PM
Andreas19 Andreas19 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2006
Posts: 124 Andreas19 User rank is Sergeant (500 - 2000 Reputation Level)Andreas19 User rank is Sergeant (500 - 2000 Reputation Level)Andreas19 User rank is Sergeant (500 - 2000 Reputation Level)Andreas19 User rank is Sergeant (500 - 2000 Reputation Level)Andreas19 User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 Weeks 5 Days 2 h 38 m 47 sec
Reputation Power: 15
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?

Reply With Quote
  #2  
Old September 26th, 2008, 04:30 PM
OmegaZero OmegaZero is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: May 2007
Posts: 738 OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 5 h 46 m 27 sec
Reputation Power: 928
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);

Reply With Quote
  #3  
Old September 27th, 2008, 12:51 AM
Andreas19 Andreas19 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2006
Posts: 124 Andreas19 User rank is Sergeant (500 - 2000 Reputation Level)Andreas19 User rank is Sergeant (500 - 2000 Reputation Level)Andreas19 User rank is Sergeant (500 - 2000 Reputation Level)Andreas19 User rank is Sergeant (500 - 2000 Reputation Level)Andreas19 User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 Weeks 5 Days 2 h 38 m 47 sec
Reputation Power: 15
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?

Reply With Quote
  #4  
Old September 27th, 2008, 03:27 PM
requinix's Avatar
requinix requinix is offline
Still alive
Dev Shed God 16th Plane (12500 - 12999 posts)
 
Join Date: Mar 2007
Location: Washington, USA
Posts: 12,860 requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)  Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 5 Months 1 Week 5 Days 5 h 30 m 31 sec
Reputation Power: 8977
Send a message via AIM to requinix Send a message via MSN to requinix Send a message via Yahoo to requinix Send a message via Google Talk to requinix
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.

Reply With Quote
  #5  
Old September 27th, 2008, 11:02 PM
Andreas19 Andreas19 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2006
Posts: 124 Andreas19 User rank is Sergeant (500 - 2000 Reputation Level)Andreas19 User rank is Sergeant (500 - 2000 Reputation Level)Andreas19 User rank is Sergeant (500 - 2000 Reputation Level)Andreas19 User rank is Sergeant (500 - 2000 Reputation Level)Andreas19 User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 Weeks 5 Days 2 h 38 m 47 sec
Reputation Power: 15
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreRegex Programming > Regexp with positive look-ahead

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap