Regexp matching blocks of text

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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!
 
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
 
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!
 
Back
Top