user2958725
user2958725

Reputation: 1435

How can I make a regex for one or more space delimited numbers?

No matter what I just can't figure it out. I can have \d and then repeat it, but whatever gets repeated either has to have a trailing space, or it doesn't.

I want to be able to match any of the following:

"1"
"1 2"
"1 2 3"
"1 2 3 5 6 7 8 9    9"

(with arbitrary repetition)

But absolutely none of the following:

"1 "
"1 2 "
" 1 2"
" 1 3 4 56 6  "

How can this be done?

Perhaps the language which I would like to validate is simply irregular?

Upvotes: 0

Views: 76

Answers (3)

Lajos Veres
Lajos Veres

Reputation: 13725

I think this regexp should work for you:

/^(\d+\s*)*\d+$/

Upvotes: 0

anubhava
anubhava

Reputation: 785146

You can use this regex:

/(\d+ +)*\d+/

Upvotes: 0

OGHaza
OGHaza

Reputation: 4795

This should work

^(\d\s+)*\d$

String must always end in a digit (no trailing space), for any digit before the end it must be followed by 1 to many spaces (and the initial number cannot be preceded by space)

Upvotes: 1

Related Questions