tags:

views:

33

answers:

3

So far I have

^[a-z0-9]+[a-z0-9\x20]+[a-z0-9]+$

Which matches all criteria except not matching double spaces.

+1  A: 

Try this:

^[a-z0-9](?: ?[a-z0-9])*$

As seen on rubular

a_bc   # match (underscore '_' represents space)
abc    # match
a_     # no match
_a     # no match
a__b   # no match

(You can replace the whitespace after the ?: with \x20 if you have to)

NullUserException
Thanks, but it is not working for me (not matching double spaces). I should have specified I'm using php if that matters. I used \x20 because I wanted to be certain I was only matching spaces and no other white spaces.
SystemicPlural
@Systemic I have no issue getting it to work. See this: http://ideone.com/OsYC1
NullUserException
I tried your solution again and I can't duplicate my earlier failure. I may have copy pasted incorrectly - been a long day. Thanks
SystemicPlural
+1  A: 

Something along these lines should work:

^[a-z0-9]+((\x20)?[a-z0-9]+)*$

This will mean you can have letter or number at the start, one or more times, followed by a block containing a space 0 or 1 times, followed by 1 or more letters or numbers, which can be included 0 or more times.

Edit: I think this should work. Can only have 0 or 1 spaces in between blocks of letters/numbers.

Nellius
Fails to match (supposedly) valid string `aa a`
NullUserException
Thanks that works. And I understand what its doing!
SystemicPlural
Sorry, NullUserException is correct
SystemicPlural
+1  A: 

How about:

^[a-z0-9]+(\x20[a-z0-9]+)*$

One or more letter or numbers, followed by a space & one or more letters/numbers, repeated 0 or more times.

David Gelhar
@nulluser - No, this matches `aa a`. http://rubular.com/r/OfOcxGMr9k
David Gelhar
@David Strange, it wasn't working here. Must've copied and pasted wrong.
NullUserException