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

Regexp matching blocks of text

P: n/a
Hi,
I'm using RegExp to try and match a particular string value and can't figure
out why it's not working!

Possible valid values for the string are EITHER a single number (any length
of digits) OR a series of numbers, delimited by "|". (e.g. valid examples are
"1", "123", "123|", "123|456|", etc. The following would be invalid, "|",
"|123").

I've tried several things but only ever seem to match the first part e.g. :

(\d+\|)*|\d+ will match "123|" but not "123"
\d+|(\d+\|)* will match "123" but not "123|"

I eventually also want to anchor this with ^ and $. Can someone spot what's
wrong?
Thanks!

Feb 5 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Hi,
I'm using RegExp to try and match a particular string value and can't
figure
out why it's not working!
Possible valid values for the string are EITHER a single number (any
length of digits) OR a series of numbers, delimited by "|". (e.g.
valid examples are "1", "123", "123|", "123|456|", etc. The following
would be invalid, "|", "|123").

I've tried several things but only ever seem to match the first part
e.g. :

(\d+\|)*|\d+ will match "123|" but not "123"
\d+|(\d+\|)* will match "123" but not "123|"
I eventually also want to anchor this with ^ and $. Can someone spot
what's
wrong?
Thanks!
Try \d+(\|\d+)* or (\d+|)*\d+

Hans Kesting
Feb 5 '07 #2

P: n/a
Hi, your reply led me to look at the problem a little differently! I decided
to go with:

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

I think this is the exact validation I need (it's basically your first
version with a few extra brackets)

Thanks a million!

"Hans Kesting" wrote:
Hi,
I'm using RegExp to try and match a particular string value and can't
figure
out why it's not working!
Possible valid values for the string are EITHER a single number (any
length of digits) OR a series of numbers, delimited by "|". (e.g.
valid examples are "1", "123", "123|", "123|456|", etc. The following
would be invalid, "|", "|123").

I've tried several things but only ever seem to match the first part
e.g. :

(\d+\|)*|\d+ will match "123|" but not "123"
\d+|(\d+\|)* will match "123" but not "123|"
I eventually also want to anchor this with ^ and $. Can someone spot
what's
wrong?
Thanks!

Try \d+(\|\d+)* or (\d+|)*\d+

Hans Kesting
Feb 5 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.