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.
For example, when using this regular expression "HelloWorld" is fine, but "Hello World" does not match.
How can I tweak it to allow spaces?
This question is tagged with
~ Asked on 2013-03-18 08:52:52
Just add a space in your character class.
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:
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...
Which, in my flavor (without using
\w) translates to:
(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:
If you want to allow tabs and newlines (whitespace characters), then replace the space with a
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.
Check what dialect of regular expressions you're using.* In languages like Java you'll have to escape your backslashes, i.e.
\\s. In older or more basic languages and utilities, like
\s aren't defined, so write them out with character classes, e.g.
* I know this question is tagged vb.net, 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.
~ Answered on 2013-03-18 08:54:56
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)
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
~ Answered on 2013-03-18 09:46:43