By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,835 Members | 1,173 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,835 IT Pros & Developers. It's quick & easy.

How good is my regex

P: n/a
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
Share this Question
Share on Google+
2 Replies

P: n/a
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

P: n/a
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.