[ ]{2,}
SPACE (2 or more)
You could also check that before and after those spaces words follow. (not other whitespace like tabs or new lines)
\w[ ]{2,}\w
the same, but you can also pick (capture) only the spaces for tasks like replacement
\w([ ]{2,})\w
or see that before and after spaces there is anything, not only word characters (except whitespace)
^\s[^\s]
Answer from Alex on Stack Overflow[ ]{2,}
SPACE (2 or more)
You could also check that before and after those spaces words follow. (not other whitespace like tabs or new lines)
\w[ ]{2,}\w
the same, but you can also pick (capture) only the spaces for tasks like replacement
\w([ ]{2,})\w
or see that before and after spaces there is anything, not only word characters (except whitespace)
^\s[^\s]
Simple solution:
/\s{2,}/
This matches all occurrences of one or more whitespace characters. If you need to match the entire line, but only if it contains two or more consecutive whitespace characters:
/^.*\s{2,}.*$/
If the whitespaces don't need to be consecutive:
/^(.*\s.*){2,}$/
regex - Regular expression to allow spaces between words - Stack Overflow
How can I use regex on multiple spaces?
Regex to match multiple spaces but not if they are after a period(.).
[Vscode] Select spaces between words
Given that you also want to cover tabs, newlines, etc, just replace \s\s+ with ' ':
string = string.replace(/\s\s+/g, ' ');
If you really want to cover only spaces (and thus not tabs, newlines, etc), do so:
string = string.replace(/ +/g, ' ');
Since you seem to be interested in performance, I profiled these with firebug. Here are the results I got:
str.replace( / +/g, ' ' ) -> 380ms
str.replace( /\s\s+/g, ' ' ) -> 390ms
str.replace( / {2,}/g, ' ' ) -> 470ms
str.replace( / +/g, ' ' ) -> 790ms
str.replace( / +(?= )/g, ' ') -> 3250ms
This is on Firefox, running 100k string replacements.
I encourage you to do your own profiling tests with firebug, if you think performance is an issue. Humans are notoriously bad at predicting where the bottlenecks in their programs lie.
(Also, note that IE 8's developer toolbar also has a profiler built in -- it might be worth checking what the performance is like in IE.)
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 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.
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
Hi, fairly new to Java here. In a project I’m doing, part of it requires me to split up a line read from a file and store each part in an array for later use (well it’s not required per say but it’s the way I’m doing it), and I’d like to use regex to do it. The file reading part is all fine, the thing is, the line I’m reading is split up by multiple spaces (required in the project specification), so it’s like: [Thing A] [Thing B] [Thing C] and so on, each line has letters, numbers and slashes.
I’ve been looking through Stack Overflow, YouTube, other sites and such and I haven’t found anything that works exactly as I need it to. The main 3 things I remembered trying that I found were \\s\\s, \\s+ and \\s{2} but none of those worked for me, \\s+ works for one or more spaces, but I need it to exclusively be more than one space. Using my previous example, [Thing C] is a full name, so if I did it for only one space then the name would get split up, which I need to avoid. Point being: is there any way for me to use the regex and split features that lets me split up the parts of the string separated by 2 spaces? So like:
String line = “Insert line here”;
String regex = “[x]”; (with “x“ being a placeholder)
String[] array = line.split(regex);
Something like that? If there‘s no way to do it like that then I’m open to using other ideas. (Also sorry, I couldn’t figure out how to get the code block to work)
Example: My Name is Mohit. Surname is Kumar.
The regex should only match the spaces after is as there are multiple spaces after it, but not after other words as there is only one space after them and not after Mohit. as there is a period after the word.
I have tried
-
(?<!\.)\s{2,} - https://regex101.com/r/jAkQM1/1
-
(?<!\.)\s+ - https://regex101.com/r/PfYX26/1
but both these expressions are matching multiple spaces after Mohit. expect for the first space. I'm testing my regex at Regex101.
Thanks for the help.
I want to select only that spaces that have one char before and after. Ex:
Word one
It would select the space that it's after Word and before one
My problem is that i have other lines with identation and it's selecting the spaces of the TAB.
Find this Regular Expression:
/\[\[do\s+("[\w\s]+")\s*\]\]/
And do the following replacement:
'cool($1)'
The only special thing that's being done here is using character classes to our advantage with
[\w\s]+
Matches one or more word or space characters (a-z, A-Z, 0-9, _, and whitespace). That';; eat up your internal stuff no problem.
'[[do "hi i am Bob"]]'.replace(/\[\[do\s+("[\w\s]+")\s*\]\]/, 'cool($1)')
Spits out
cool("hi i am Bob")
Though - if you want to add punctuation (which you probably will), you should do it like this:
/\[\[do\s+("[^"]+")\s*\]\]/
Which will match any character that's not a double quote, preserving your substring. There are more complicated ones to allow you to deal with escaped quotation marks, but I think that's outside the scope of this question.
To match "all words with single or multiple spaces", you cannot use \s*, as it will match even no spaces.
On the other hand, it looks like you want to match even "hi", which is one word with no spaces.
You probably want to match one or more words separated by spaces. If so, use regex pattern
(\w+(?:$|\s+))+
or
\w+(\s+\w+)*
Hi everyone,
i have the following string:
Test Tester AndTest (2552)
and try to get only the word (they can be one or more words) before "(" without the last space
I've tried the following pattern:
([A-Z].* .*?[a-z]*)
but with this one the last space is also included.
Is there a way to get only the words?
Thanks in advance,
greetings
Flosul