473,406 Members | 2,467 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 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 1032
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

77
by: Hunn E. Balsiche | last post by:
in term of its OO features, syntax consistencies, ease of use, and their development progress. I have not use python but heard about it quite often; and ruby, is it mature enough to be use for...
9
by: Tim Conner | last post by:
Is there a way to write a faster function ? public static bool IsNumber( char Value ) { if (Regex.IsMatch( Value.ToString(), @"^+$" )) { return true; } else return false; }
3
by: Tom | last post by:
I have a poitionally delimited flat file (.txt) with large records being prepared for uploading to a mainframe. I need to make sure that any invalid characters within fields are translated into...
20
by: jeevankodali | last post by:
Hi I have an .Net application which processes thousands of Xml nodes each day and for each node I am using around 30-40 Regex matches to see if they satisfy some conditions are not. These Regex...
1
by: Terry Olsen | last post by:
I download xml logs from several servers every day and read the data out of them using the XmlTextReader. But about 10% of them each day throw exceptions because they are not well formed. I don't...
6
by: Extremest | last post by:
I have a huge regex setup going on. If I don't do each one by itself instead of all in one it won't work for. Also would like to know if there is a faster way tried to use string.replace with all...
7
by: Extremest | last post by:
I am using this regex. static Regex paranthesis = new Regex("(\\d*/\\d*)", RegexOptions.IgnoreCase); it should find everything between parenthesis that have some numbers onyl then a forward...
3
by: aspineux | last post by:
My goal is to write a parser for these imaginary string from the SMTP protocol, regarding RFC 821 and 1869. I'm a little flexible with the BNF from these RFC :-) Any comment ? tests= def...
4
by: James Kanze | last post by:
On Jul 16, 10:53 pm, Mirco Wahab <wa...@chemie.uni-halle.dewrote: It depends. You might like to have a look at my "Hashing.hh" header (in the code at kanze.james.neuf.fr/code-en.html---the...
23
by: tkpmep | last post by:
I have a list that starts with zeros, has sporadic data, and then has good data. I define the point at which the data turns good to be the first index with a non-zero entry that is followed by at...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.