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 January 30th, 2012, 03:41 PM
marcushjortsber marcushjortsber is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2012
Posts: 9 marcushjortsber User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 3 m 43 sec
Reputation Power: 0
Other - Regex for validating multiple numbers?

I hav tried this:

regex=/^(([1-9][0-9]*)|0)(,(([1-9][0-9]*)|0))*$/;

And that validates all numbers separated with comma...

Now I want to evolve that thought

Now I want to validate a given string of numbers!

I have a string that I fetch from db.
The string contains numbers.
The numbers is connected to the user so the validation is to check if so the user only enter numbers connected to that specific user.

How do I write a regex so that the input matches one or more of the numbers in the string?

All numbers entered in the input must be in the string.
The input must have the form number,number,number,...,...
That is: number followed by comma, no spaces or other chars...

Thanx in advance!

Reply With Quote
  #2  
Old January 30th, 2012, 11:44 PM
abareplace abareplace is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2011
Posts: 29 abareplace User rank is First Lieutenant (10000 - 20000 Reputation Level)abareplace User rank is First Lieutenant (10000 - 20000 Reputation Level)abareplace User rank is First Lieutenant (10000 - 20000 Reputation Level)abareplace User rank is First Lieutenant (10000 - 20000 Reputation Level)abareplace User rank is First Lieutenant (10000 - 20000 Reputation Level)abareplace User rank is First Lieutenant (10000 - 20000 Reputation Level)abareplace User rank is First Lieutenant (10000 - 20000 Reputation Level)abareplace User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 8 h 25 m 9 sec
Reputation Power: 0
This is probably what you want:

Code:
/^(123|456|78)(,(123|456|78))*$/


123, 456, and 78 are the allowed numbers; their order does not matter in the input string. But if the allowed numbers are taken from database, then it's easier to split the input by comma (using explode() function in PHP) and compare each number against the set of allowed numbers.

Reply With Quote
  #3  
Old January 31st, 2012, 09:47 AM
marcushjortsber marcushjortsber is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2012
Posts: 9 marcushjortsber User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 3 m 43 sec
Reputation Power: 0
[resolved]

Thanx!

It seems like it works

I'll be back if I find some errors

Thanx again!

Reply With Quote
  #4  
Old February 2nd, 2012, 09:10 AM
marcushjortsber marcushjortsber is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2012
Posts: 9 marcushjortsber User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 3 m 43 sec
Reputation Power: 0
Ok!

Now I have a follow up question

If I want to validate so that each number only can be in input once, how do I do that?

Reply With Quote
  #5  
Old February 2nd, 2012, 06:50 PM
ragax's Avatar
ragax ragax is offline
Turn left at the third duck
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2011
Location: Nelson, NZ
Posts: 93 ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level)ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level)ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level)ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level)ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level)ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level)ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Day 24 m 37 sec
Reputation Power: 92
Quote:
Originally Posted by marcushjortsber
If I want to validate so that each number only can be in input once, how do I do that?


Hi Marcus,

Not a problem!

Let's just add some conditions to aba's expression: at the front, for each number, we add a lookahead that checks the number is not present in the string twice.
Code:
^(?!.*?123.*?123)(?!.*?456.*?456)(?!.*?78.*?78)(123|456|78)(,(123|456|78))*$


If you have numbers that can be embedded in other numbers (for instance, 78 and 782 are both possible), then we need to refine that by adding word boundaries:
Code:
^(?!.*?\b123\b.*?\b123\b)(?!.*?\b456\b.*?\b456\b)(?!.*?\b78\b.*?78)(123|456|78)(,(123|456|78))*$


I know you know that, but for the record for someone who might be studying regex on the thread: If you have an array of numbers, it's a simple task build the regex programmatically.

Wishing you a fun day.

__________________
Regex Tutorial | Latest RegexBuddy Demo

Reply With Quote
  #6  
Old February 2nd, 2012, 07:28 PM
ragax's Avatar
ragax ragax is offline
Turn left at the third duck
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2011
Location: Nelson, NZ
Posts: 93 ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level)ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level)ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level)ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level)ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level)ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level)ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Day 24 m 37 sec
Reputation Power: 92
Hi again Marcus,

Do note that at the moment, the regex doesn't specify how many numbers your user should input, so unless you enforce this beforehand, s/he can get away with inputting a single number, e.g. 11

If you'd like to check that at least two numbers are present, you can add another lookahead:
Code:
^(?=(?:\d+(?:,|$)){2})(?!.*?\b11\b.*?\b11\b)(?!.*?\b55\b.*?\b55\b)(?!.*?\b64\b.*?\b64\b)((?:11|55|64  )(?:,|$))++$


Just change the {2} to the minimum number of numbers you require.

Note slight tweaks also in the matching part of the expression (after the lookaheads).


Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreRegex Programming > Other - Regex for validating multiple numbers?

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