473,394 Members | 2,160 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,394 software developers and data experts.

Another Reg Exp problem

Alright, I don't have time to learn all the intricacies of regular
expressions, but what I want to do is match a string that has:

1) only digits
2) may have a comma
3) must not start or end with a comma

So basically, a string of numbers separated with commas. valid input
could be

123
123,124
123,125,125
1,2,3
1,12,123

Invalid would be

123,
,123
123 124 125
123abc

TIA!
Jul 23 '05 #1
8 1019
In article <5d**************************@posting.google.com >,
la**********@yahoo.com enlightened us with...
Alright, I don't have time to learn all the intricacies of regular
expressions, but what I want to do is match a string that has:

1) only digits
2) may have a comma
3) must not start or end with a comma

So basically, a string of numbers separated with commas. valid input
could be


Try
/^\d[,\d]*$/

--
--
~kaeli~
Murphy's Law #2030: If at first you don't succeed, destroy
all evidence that you tried.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

Jul 23 '05 #2
kaeli <ti******@NOSPAM.comcast.net> writes:
1) only digits
2) may have a comma
3) must not start or end with a comma
Try
/^\d[,\d]*$/


This can end with a comma. You probably mean
/^\d+(,\d+)*$/

Hmm. In fact, the original poster didn't say that the string must be
non-empty, so a more correct solution would be:
/^(\d+(,\d+)*)?$/

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #3
Lee
Larry Bud said:

Alright, I don't have time to learn all the intricacies of regular
expressions, but what I want to do is match a string that has:

1) only digits
2) may have a comma
3) must not start or end with a comma

So basically, a string of numbers separated with commas. valid input
could be

123
123,124
123,125,125
1,2,3
1,12,123

Invalid would be

123,
,123
123 124 125
123abc


If there must be at least two digits, then it's easy:
/^\d+[,\d]*\d$/

That's a digit at the beginning, any number of [comma or digit],
followed by a digit at the end.

If a single digit is also valid, then it's only a little more complicated:

/(^\d+[,\d]*\d$)|(^\d$)/

That's the same as above OR just one digit.

Jul 23 '05 #4
Lee
kaeli said:

In article <5d**************************@posting.google.com >,
la**********@yahoo.com enlightened us with...
Alright, I don't have time to learn all the intricacies of regular
expressions, but what I want to do is match a string that has:

1) only digits
2) may have a comma
3) must not start or end with a comma

So basically, a string of numbers separated with commas. valid input
could be


Try
/^\d[,\d]*$/


That could end with a comma

Jul 23 '05 #5
In article <3c**********@hotpop.com>, lr*@hotpop.com enlightened us
with...
kaeli <ti******@NOSPAM.comcast.net> writes:
1) only digits
2) may have a comma
3) must not start or end with a comma

Try
/^\d[,\d]*$/


This can end with a comma. You probably mean
/^\d+(,\d+)*$/


So close, and yet so far. :)
I'll get regex eventually.
--
--
~kaeli~
Support your local medical examiner: die strangely!
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

Jul 23 '05 #6
Lee <RE**************@cox.net> wrote in message news:<c8*********@drn.newsguy.com>...
Larry Bud said:

Alright, I don't have time to learn all the intricacies of regular
expressions, but what I want to do is match a string that has:

1) only digits
2) may have a comma
3) must not start or end with a comma

So basically, a string of numbers separated with commas. valid input
could be

123
123,124
123,125,125
1,2,3
1,12,123

Invalid would be

123,
,123
123 124 125
123abc


If there must be at least two digits, then it's easy:
/^\d+[,\d]*\d$/

That's a digit at the beginning, any number of [comma or digit],
followed by a digit at the end.

If a single digit is also valid, then it's only a little more complicated:

/(^\d+[,\d]*\d$)|(^\d$)/

That's the same as above OR just one digit.


Damn, I DID forget to add that it cannot be empty. Will this work for it as well?

What if someone enters

1,,23

There shouldn't be two commas in a row.
Jul 23 '05 #7
JRS: In article <5d**************************@posting.google.com >, seen
in news:comp.lang.javascript, Larry Bud <la**********@yahoo.com> posted
at Mon, 24 May 2004 10:46:58 :
Alright, I don't have time to learn all the intricacies of regular
expressions, but what I want to do is match a string that has:

1) only digits
2) may have a comma
3) must not start or end with a comma

So basically, a string of numbers separated with commas.


And non-empty.

So you want a digit, followed by any number, including zero, of comma-
followed-by-at-least-one-digit - and nothing else.

OK = /^(\d+)(,\d+)*$/.test(S)

Tested in
<URL:http://www.merlyn.demon.co.uk/js-valid.htm>
<URL:http://www.merlyn.demon.co.uk/js-quick.htm>

OK = /^(\d+,)*(\d+)$/.test(S)

should also do. In each case, the parentheses around just \d+ are
superfluous.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #8
Larry Bud wrote:
Lee <RE**************@cox.net> wrote:
Larry Bud said:
Alright, I don't have time to learn all the intricacies of regular
expressions, but what I want to do is match a string that has:

1) only digits
2) may have a comma
3) must not start or end with a comma
[...]
If there must be at least two digits, then it's easy: /^\d+[,\d]*\d$/
[...]
If a single digit is also valid, then it's only a little more
complicated:

/(^\d+[,\d]*\d$)|(^\d$)/

That's the same as above OR just one digit.


Damn, I DID forget to add that it cannot be empty. Will this work for it
as well?


No.
What if someone enters

1,,23
It would be accepted using the above RegExp. /\d+/ matches "1",
/[,\d]*/ matches ",,2" and the final /\d/ matches "3" then.
There shouldn't be two commas in a row.


I think you are looking for /^(\d(,\d)?)+$/. Could be written as
/^(\d,\d|\d)+$/ as well. It remains to be discussed what of them
is more efficient.
HTH

PointedEars
Jul 23 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Chris | last post by:
Hi I have a scenario where I've created another AppDomain to dynamically load a DLL(s) into. In this newly loaded DLL I want to call a static method on a class. The problem arise is that I have...
4
by: Mountain Bikn' Guy | last post by:
I am having serious problems with the following IDE bug: Could not write to output file 'x.dll' -- 'The process cannot access the file because it is being used by another process. ' and BUG:...
188
by: christopher diggins | last post by:
I have posted a C# critique at http://www.heron-language.com/c-sharp-critique.html. To summarize I bring up the following issues : - unsafe code - attributes - garbage collection -...
10
by: Sorin Dolha [MCSD .NET] | last post by:
I would like to start a process from C# code as another user. The C# code is executed as the ASPNET user because it relies in a Web Page class, and I would like that the process will run as another...
3
by: qwerty | last post by:
I´m new to ASP.Net. My workmate has some experience with it. He claimed that in ASP.Net working with frames is much simpler than it was ASP. I asked explanation but he couldn't give me such. (a...
17
by: Bruno | last post by:
I have a feature that is hosted on a different domain from the primary one in a frame, and need to retain values in a cookie. example: A web page at one.com contains a frame which has a page...
17
by: Rabbit | last post by:
Hi, On my 1st page, i have a function which gets a new ID value and need to transfer to another immediately. which I want to get in 2nd page using Request.form("txtID"), but doesn't work, the...
5
by: felipevaldez | last post by:
how can I put an element below another element, that's not absolutely positioned? so I have a n element, called X, and next to that, an element Y X Y XXXXXX
3
by: drummond.ian | last post by:
Hello Everyone, This problem's been causing me a lot of trouble and I'm hoping somebody can help me out!! I have a dialog-based MFC application in visual studio 2003. I want to call a...
11
by: Jan | last post by:
Hi: Here's a problem I've had for a long time. The client is really running out of patience, and I have no answers. Access2003, front- and back-end. Single form with 4 subforms (each...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.