471,044 Members | 948 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,044 software developers and data experts.

How good is my regex

I need to validate that a string meets the following criteria.

Is at least 1 digit 0 (1-9)
May contain commas, hyphens and spaces
Must begin with a digit (1-9)
Must end with a digit (0-9)
Any new group of digits (either the first in the string or immediately
following a comma, space or hyphen) must begin with (1-9)

So:
good
1
123
1 456-567,676

bad
01
123,
,345
123 0456
123,345-087

I've come up with a couple of REGEXs but I'm no regexpert (geddit - I
know, no comedian either), but I'm sure there's a tidier way.

Feb 9 '07 #1
2 953
On Feb 9, 12:43 pm, "Captain Paralytic"
<paul_laut...@yahoo.comwrote:
I need to validate that a string meets the following
criteria.

Is at least 1 digit 0 (1-9)
May contain commas, hyphens and spaces
Must begin with a digit (1-9)
Must end with a digit (0-9)
Any new group of digits (either the first in the string
or immediately following a comma, space or hyphen) must
begin with (1-9)
/^[1-9]\d*([,\- ][1-9]\d*)*$/

--
Pavel Lepin

Feb 9 '07 #2
On 9 Feb, 11:32, p.le...@ctncorp.com wrote:
On Feb 9, 12:43 pm, "Captain Paralytic"

<paul_laut...@yahoo.comwrote:
I need to validate that a string meets the following
criteria.
Is at least 1 digit 0 (1-9)
May contain commas, hyphens and spaces
Must begin with a digit (1-9)
Must end with a digit (0-9)
Any new group of digits (either the first in the string
or immediately following a comma, space or hyphen) must
begin with (1-9)

/^[1-9]\d*([,\- ][1-9]\d*)*$/

--
Pavel Lepin
Looks neater than mine (no surprise there - and I discovered that mine
didn't do all of the rules either!). I'll try it out tonight. Thanks.

Feb 9 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

77 posts views Thread by Hunn E. Balsiche | last post: by
9 posts views Thread by Tim Conner | last post: by
20 posts views Thread by jeevankodali | last post: by
6 posts views Thread by Extremest | last post: by
7 posts views Thread by Extremest | last post: by
3 posts views Thread by aspineux | last post: by
4 posts views Thread by James Kanze | last post: by
23 posts views Thread by tkpmep | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.