[ ]{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
🌐
RegExr
regexr.com › 4s7hp
Multiple Spaces
Full RegEx Reference with help & examples.
Discussions

regex - Regular expression to allow spaces between words - Stack Overflow
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... More on stackoverflow.com
🌐 stackoverflow.com
January 13, 2018
How can I use regex on multiple spaces?
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
🌐 r/javahelp
12
3
January 4, 2025
Regex to match multiple spaces but not if they are after a period(.).
Got the answer - https://stackoverflow.com/a/76447308/6492573 Regex - https://regex101.com/r/imlcbQ/1 More on reddit.com
🌐 r/regex
4
6
June 10, 2023
[Vscode] Select spaces between words
\b\s\b Or if you want any number of spaces: \b\s+\b https://regex101.com/r/7cW56h/1 More on reddit.com
🌐 r/regex
5
1
April 17, 2020
🌐
regex101
regex101.com › library › jS1iV6
regex101: Remove multiple white spaces between words
gmOpen regex in editor · Remove multiple white spaces between words and replace it with the char of your choosing.Submitted by binary_fm
🌐
TextPad Community
forums.textpad.com › home › board index › peer group support › general
Matching 0 or more spaces in searches - Community - TextPad
August 18, 2023 - \s Matches any whitespace character. For example, use this character to specify a space between words in a phrase: stock\stips matches the phrase stock tips
Top answer
1 of 13
571

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.

2 of 13
167

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

🌐
Reddit
reddit.com › r/javahelp › how can i use regex on multiple spaces?
r/javahelp on Reddit: How can I use regex on multiple spaces?
January 4, 2025 -

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)

Top answer
1 of 5
4
Not the answer but you can go to regex101.com and practice yourself
2 of 5
1
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
Find elsewhere
🌐
Reddit
reddit.com › r/regex › regex to match multiple spaces but not if they are after a period(.).
r/regex on Reddit: Regex to match multiple spaces but not if they are after a period(.).
June 10, 2023 -
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

  1. (?<!\.)\s{2,} - https://regex101.com/r/jAkQM1/1

  2. (?<!\.)\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.

🌐
TutorialsPoint
tutorialspoint.com › article › how-to-replace-multiple-spaces-in-a-string-using-a-single-space-using-java-regex
How to replace multiple spaces in a string using a single space using Java regex?
November 21, 2019 - import java.util.Scanner; public class Test { public static void main(String args[]) { //Reading String from user System.out.println("Enter a String"); Scanner sc = new Scanner(System.in); String input = sc.nextLine(); //Regular expression to match space(s) String regex = "\s+"; //Replacing the pattern with single space String result = input.replaceAll(regex, " "); System.out.print("Text after removing unwanted spaces: \n"+result); } }
🌐
Iditect
iditect.com › program-example › regex--regular-expression-to-allow-spaces-between-words.html
Regular expression to allow spaces between words
Description: Specify a regex to validate sentences containing spaces between words. ... This regex pattern validates sentences that start with an alphabetical character, followed by zero or more groups of a space (\s) and alphabetical characters. jersey-2.0 launchctl cumsum linked-tables multiple-input powershell-1.0 spring-mongo tinymce spring-data-elasticsearch tty
🌐
Ablebits
ablebits.com › ablebits blog › excel › regex › remove whitespaces and empty lines in excel using regex
Remove whitespaces and empty lines in Excel using Regex
March 10, 2023 - To remove extra whitespace (i.e. more than one consecutive spaces), use the same regex \s+ but replace the found matches with a single space character. ... Please pay attention that this formula keeps one space character not only between words ...
🌐
Devgex
devgex.com › en › article › 00020371
Methods and Implementation of Regex for Matching Multiple Consecutive Spaces - DevGex
November 23, 2025 - Using the \w metacharacter to match any word character (letters, digits, or underscores), patterns like \w[ ]{2,}\w require word characters before and after the spaces, limiting matches to within or between words.
🌐
UiPath Community
forum.uipath.com › help › activities
Regular Expression for Inconsistent Multiple Spaces - Activities - UiPath Community Forum
August 15, 2022 - Good day fellow UiPath RPA Developers, Does anyone know how to parse a string with inconsistent number of spaces using hard coded Regex(like Regex.Replace-code because I will apply it inside a LINQ)? These are the stri…
🌐
RegexOne
regexone.com › lesson › whitespaces
RegexOne - Learn Regular Expressions - Lesson 9: All this whitespace
The most common forms of whitespace you will use with regular expressions are the space (␣), the tab (\t), the new line (\n) and the carriage return (\r) (useful in Windows environments), and these special characters match each of their respective whitespaces.