282

I want a regular expression that prevents symbols and only allows letters and numbers. The regex below works great, but it doesn't allow for spaces between words.

^[a-zA-Z0-9_]*$

For example, when using this regular expression "HelloWorld" is fine, but "Hello World" does not match.

How can I tweak it to allow spaces?

0

13 Answers 13

562

tl;dr

Just add a space in your character class.

^[a-zA-Z0-9_ ]*$

 


Now, if you want to be strict...

The above isn't exactly correct. Due to the fact that * means zero or more, it would match all of the following cases that one would not usually mean to match:

  • An empty string, "".
  • A string comprised entirely of spaces, "      ".
  • A string that leads and / or trails with spaces, "   Hello World  ".
  • A string that contains multiple spaces in between words, "Hello   World".

Originally I didn't think such details were worth going into, as OP was asking such a basic question that it seemed strictness wasn't a concern. Now that the question's gained some popularity however, I want to say...

...use @stema's answer.

Which, in my flavor (without using \w) translates to:

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

(Please upvote @stema regardless.)

Some things to note about this (and @stema's) answer:

  • If you want to allow multiple spaces between words (say, if you'd like to allow accidental double-spaces, or if you're working with copy-pasted text from a PDF), then add a + after the space:

    ^\w+( +\w+)*$
    
  • If you want to allow tabs and newlines (whitespace characters), then replace the space with a \s+:

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

    Here I suggest the + by default because, for example, Windows linebreaks consist of two whitespace characters in sequence, \r\n, so you'll need the + to catch both.

Still not working?

Check what dialect of regular expressions you're using.* In languages like Java you'll have to escape your backslashes, i.e. \\w and \\s. In older or more basic languages and utilities, like sed, \w and \s aren't defined, so write them out with character classes, e.g. [a-zA-Z0-9_] and [\f\n\p\r\t], respectively.

 


* I know this question is tagged , but based on 25,000+ views, I'm guessing it's not only those folks who are coming across this question. Currently it's the first hit on google for the search phrase, regular expression space word.

9
  • 4
    it allows empty string Commented Sep 12, 2013 at 14:23
  • 1
    Wow, so simple! thanks. Isnt there a site or something one can use to generate regex expressions, for noobs I mean...
    – Pierre
    Commented Apr 28, 2014 at 6:33
  • 3
    @Pierre - It's fairly difficult to take human instructions and convert them to explicit rules. (The human language is fluid and full of ambiguities, and our brains do most of the work required to resolve things and fill in the gaps. Computers don't have such a brain, and clever attempts to mimic one are not yet powerful enough.) There do exist tools like debuggex.com that represent your regex visually, but as appealing as it is it may not be very helpful for complete beginners. I recommend an interactive tutorial to get the basics down, however. Commented Apr 28, 2014 at 9:55
  • 1
    Yes, also your regex will match if there are just spaces. My reply was to Neha choudary's comment. Commented Mar 23, 2016 at 5:49
  • 1
    @Pierre Three years later -- I came across this question today, saw your comment; I use regex hero (regexhero.net) for testing regular expressions. I think the online version only works in Internet Explorer with Silverlight but it's better than nothing. Commented Jul 26, 2017 at 14:33
166

One possibility would be to just add the space into you character class, like acheong87 suggested, this depends on how strict you are on your pattern, because this would also allow a string starting with 5 spaces, or strings consisting only of spaces.

The other possibility is to define a pattern:

I will use \w this is in most regex flavours the same than [a-zA-Z0-9_] (in some it is Unicode based)

^\w+( \w+)*$

This will allow a series of at least one word and the words are divided by spaces.

^ Match the start of the string

\w+ Match a series of at least one word character

( \w+)* is a group that is repeated 0 or more times. In the group it expects a space followed by a series of at least one word character

$ matches the end of the string

3
  • This : regex101.com/#javascript also provides as good an explanation for the regex pattern you want to analyse.
    – Dark Star1
    Commented Sep 9, 2014 at 11:35
  • Nice Regex, much simpler then a lot of [0-9a-z] etc Commented Jul 6, 2016 at 1:16
  • I found in my regex interpreter that I needed to wrap the entire string in parentheses in order for the first match to be the entire string, and not just the words coming after the first space. That is ^- (\w+( \w+)*)$ worked for me. Commented Jul 16, 2020 at 15:57
43

This one worked for me

([\w ]+)
1
  • 11
    This answer lacks an explanation. Commented Jun 1, 2019 at 8:46
15

Try with:

^(\w+ ?)*$

Explanation:

\w             - alias for [a-zA-Z_0-9]
"whitespace"?  - allow whitespace after word, set is as optional
3
  • 2
    This is so going to induce backtracking hell.
    – nhahtdh
    Commented Mar 18, 2013 at 8:58
  • 2
    For example, given a non-matching string ggggggggggggggggggggggggggggggggggggg;, your regex is going to take very long time to reach the result due to excessive backtracking.
    – nhahtdh
    Commented Mar 18, 2013 at 8:59
  • 1
    Ok, so what do you suggest ?
    – hsz
    Commented Mar 18, 2013 at 9:01
13

I assume you don't want leading/trailing space. This means you have to split the regex into "first character", "stuff in the middle" and "last character":

^[a-zA-Z0-9_][a-zA-Z0-9_ ]*[a-zA-Z0-9_]$

or if you use a perl-like syntax:

^\w[\w ]*\w$

Also: If you intentionally worded your regex that it also allows empty Strings, you have to make the entire thing optional:

^(\w[\w ]*\w)?$

If you want to only allow single space chars, it looks a bit different:

^((\w+ )*\w+)?$

This matches 0..n words followed by a single space, plus one word without space. And makes the entire thing optional to allow empty strings.

5
  • Space and \s are not equivalent. \s matches more than just space.
    – nhahtdh
    Commented Mar 18, 2013 at 9:31
  • @nhahtdh: Thanks for the comment. I'm too used to matching whitespace in general I guess.. Answer is fixed.
    – creinig
    Commented Mar 18, 2013 at 9:43
  • Is it possible that you are missing a closing parenthesis ) in the first expression ? I'm not sure I did not try it.
    – ssinfod
    Commented May 1, 2020 at 14:43
  • 1
    @ssinfod: Good catch. Actually the opening parenthesis is superfluous in that example. Thanks.
    – creinig
    Commented May 2, 2020 at 15:56
  • 1
    Be aware that this answer will not match a single-character string (it will match a minimum of two characters). To fix this you can add a test for a single character: ^\w$|^\w[\w ]*\w$ Commented Dec 17, 2020 at 8:49
6

This regular expression

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

will only allow a single space between words and no leading or trailing spaces.

Below is the explanation of the regular expression:

  1. ^ Assert position at start of the string
  2. \w+ Match any word character [a-zA-Z0-9_]
    1. Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
  3. 1st Capturing group (\s\w+)*
    1. Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
    2. \s Match any white space character [\r\n\t\f ]
    3. \w+ Match any word character [a-zA-Z0-9_]
      1. Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
  4. $ Assert position at end of the string
6

Just add a space to end of your regex pattern as follows:

[a-zA-Z0-9_ ]
1
3

This does not allow space in the beginning. But allowes spaces in between words. Also allows for special characters between words. A good regex for FirstName and LastName fields.

\w+.*$
1
  • This answer is incorrect/inaccurate. This pattern matches one or more alphanumeric, underscores, then zero or more of any non-newline character. No good for the OP. Commented Jun 1, 2019 at 8:48
3

This regex allow only alphabet and spaces:

^[a-zA-Z ]*$
3
  • Thanks for the pattern and I think it is only match with single whitespace right?
    – Cuong Vo
    Commented Dec 12, 2022 at 19:54
  • No, It works for more than one whitespaces . @CuongVo Commented Dec 17, 2022 at 8:56
  • good, but it doesn't match numbers which is asked by the OP. check this demo. Commented Jan 26 at 21:35
2

For alphabets only:

^([a-zA-Z])+(\s)+[a-zA-Z]+$

For alphanumeric value and _:

^(\w)+(\s)+\w+$
1
  • 2
    it is not good example, because (something)+ is not the same as (something+) . In first example, only single character will be captured as $1 .
    – Znik
    Commented Nov 13, 2018 at 13:56
2

If you are using JavaScript then you can use this regex:

/^[a-z0-9_.-\s]+$/i

For example:

/^[a-z0-9_.-\s]+$/i.test("") //false
/^[a-z0-9_.-\s]+$/i.test("helloworld") //true
/^[a-z0-9_.-\s]+$/i.test("hello world") //true
/^[a-z0-9_.-\s]+$/i.test("none alpha: ɹqɯ") //false

The only drawback with this regex is a string comprised entirely of spaces. "       " will also show as true.

0
1

It was my regex: @"^(?=.{3,15}$)(?:(?:\p{L}|\p{N})[._()\[\]-]?)*$"

I just added ([\w ]+) at the end of my regex before *

@"^(?=.{3,15}$)(?:(?:\p{L}|\p{N})[._()\[\]-]?)([\w ]+)*$"

Now string is allowed to have spaces.

-1

Try with this one:

result = re.search(r"\w+( )\w+", text)

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