Instead of focusing on what 2 spaces isn't, use a negative look ahead for what it is:

^(?!.*  ).+

Or if the 1st and last must be non-spaces (not stated):

^\S((?!.*  ).*\S)?$

Which also allows exactly 1 non-space as the entire input.


The primary thing that makes this work is this expression ^(?!.* ), which a negative look-ahead (?!.* ) anchored to start of input by ^. It asserts, without consuming input, that the text that follows the current point does not match the given expression, in this case ".* " (quotes added for clarity), which is "anything then two spaces". In other words "two spaces do not appear at any point after here".

The second option could have been written ^\S(?!.* ).*\S$ - that same as the first but with \S at either end, but that would require different characters at each end because \S consumes input, so it wouldn't match a single letter eg "X". My making all characters other than the first optional, it allows the single character to pass (as per your requirements), while retaining the prevention of two spaces.

Answer from Bohemian on Stack Overflow
🌐
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.
🌐
Regexsonline
regexsonline.com › find-double-space-using-regular-expression-in-c
Double Space using Regular Expression in C Sharp | Regexsonline.com
December 24, 2022 - If some text string contains a double space, you can use the Matches() method from the Regex class object. You need to pass the regex expression ”.\S\s\s\S.” to the Matches() method. This method searches for one or multiple occurrences of double spaces between two non-whitespace characters, ...
🌐
Alteryx Community
community.alteryx.com › t5 › Alteryx-Designer-Desktop-Discussions › Regex-finding-leading-trailing-and-double-or-more-spaces › td-p › 635026
Regex finding leading, trailing and double or more spaces
June 13, 2024 - Hi All, I'd like to request your support to understand what's wrong with the way I'm using the RegEx tool. I'm tring to find the leading, trailing and double or more spaces in a string. I've tested the regular expressions and seems to be working fine but, these does not work in Alteryx. Best!
🌐
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.

🌐
RegExr
regexr.com › 4s7hp
RegExr: Learn, Build, & Test RegEx
Regular expression tester with syntax highlighting, PHP / PCRE & JS Support, contextual help, cheat sheet, reference, and searchable community patterns.
Find elsewhere
🌐
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…
🌐
Notepad++ Community
community.notepad-plus-plus.org › topic › 19649 › i-would-like-to-find-lines-where-there-are-2x-double-spaces
I would like to find lines where there are 2x double spaces | Notepad++ Community
July 26, 2020 - Read the official NPP Searching / Regex docs and the forum’s Regular Expression FAQ. If you follow these guidelines, you’re much more likely to get helpful replies that solve your problem in the shortest number of tries. ... That said, based solely on your title, I would think typing two spaces into the FIND box would do what you want. ... This post is deleted! ... Subject title contains an error … should be: I would like to find lines where there are 2x double spaces
🌐
UiPath Community
forum.uipath.com › help
How to remove a double-space from a variable thats used in Regex? - Help - UiPath Community Forum
September 21, 2018 - But the regex expression is not working because of a little annoying thing as a double-space between the text inside my In_SenderName variable. I want to get rid of that so it has the exact same output as the file name (with single space). This is my text input: The name of the file Ibra UiPath ...
🌐
YourBasic
yourbasic.org › golang › remove-duplicate-whitespace
Remove all duplicate whitespace · YourBasic Go
space := regexp.MustCompile(`\s+`) s := space.ReplaceAllString("Hello \t \n world!", " ") fmt.Printf("%q", s) // "Hello world!"