10

Here is what I currently have, this will match alphanumerical characters and space:

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

What I would like to do is to ensure that there will only be a match if there is no more than one (1) space. The above will match "This is a test", but I would only want it to match if the input is "This isatest", or "T hisisatest". As soon as there is more than one space total it will no longer match.

Ideally it would also not match if the space is leading or trailing, but that's just icing.

EDIT: The idea is that this will be used to verify account names upon account creation. The account name may only contain a Latin letter, a number, and a single space. However, it could be all letters or all numbers, the space is not required. This is definitely about space and not whitespace.

2
  • by "no more than 1 space" do you mean 0 or 1 ? , ie do you also want to match "Thisisatest" ?
    – phoxis
    May 28, 2011 at 9:59
  • Yeah, like in my first edit, the user name could be "Thisisatest" it doesn't have to have a space in it but if it does then there should not be more than a single space. So that's really at least 0 and no more than 1. May 28, 2011 at 10:05

5 Answers 5

11

Will allow atmost one space in between.

^[a-zA-Z0-9]+\s?[a-zA-Z0-9]+$

Will allow exactly one space in between

^[a-zA-Z0-9]+\s[a-zA-Z0-9]+$

EDIT1:

Above solutions does not accept single character strings.

The below solutions also matches single character strings

^[a-zA-Z0-9]+([ ][a-zA-Z0-9]+)?$

3
  • Matches leading and trailing spaces too. * should be +.
    – GolezTrol
    May 28, 2011 at 9:41
  • This regex will fail to match single character strings.
    – Geert
    May 28, 2011 at 9:50
  • i think matching a single character was not required by the asker, because that would also mean matching "Thisisatest" EDIT: it is required by the asker in his/her edited versio
    – phoxis
    May 28, 2011 at 10:07
5

First clean the username by trimming leading and trailing whitespace.

^[a-z0-9]+(?: [a-z0-9]+)?$
1
  • Possessive quantifiers are absolutely unnecessary here. Removed. Sep 3, 2019 at 20:44
1

Should be as simple as this. Matches 0 or 1 whitespace character in the middle of the text. Although it matches tabs too.

[a-z0-9]+\s{0,1}[a-z0-9]+
1
  • This regex will fail to match single character strings.
    – Geert
    May 28, 2011 at 9:46
1

It's fairly easy, just build it in chunks.

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

One or more alphanumerics, then an optional group consisting of a single space followed by one or more alphanumerics. (BTW, \s matches a whitespace character, not a space. That is, it actually matches [ \t\n]. Which do you actually want?)

2
  • This seems to ignore the first character even if the first character is not a space. May 28, 2011 at 9:40
  • @Reality Extractor: Not sure about that specifically, but I managed to drop a trailing $ on the floor and without it that regex is likely to do nothing at all useful. (I can't quite get what you're saying from the result, though.) The test printf 'aaa bbb\na b a b\n a b a \nabab\n ba b\nav d \n ac\nde \n' | perl -lne 'print if /^[a-z0-9]+(?: [a-z0-9]+)?$/' does the right thing for me.
    – geekosaur
    May 28, 2011 at 9:46
0

Try:

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

(Edited: your follow-up suggests you want to check for a space rather than for whitespace).

1
  • Not sure whether positive lookahead assertion placed in front of the searched token will perform as expected. AFAIK lookahead assertion are placed behind the target token. foo(?=bar) will find all foo's followed by bar. On the other hand (?!bar)foo will match all foo's unconditionally. Even those preceded by bar
    – ripat
    May 28, 2011 at 10:20

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.