Skip to main content
ubuntuask.com

Back to all posts

How to Replace Spaces Between Words Using Regex?

Published on
3 min read
How to Replace Spaces Between Words Using Regex? image

Best Regex Tools to Buy in October 2025

1 flex & bison

flex & bison

BUY & SAVE
$16.89 $29.99
Save 44%
flex & bison
2 Text Processing with JavaScript: Regular Expressions, Tools, and Techniques for Optimal Performance

Text Processing with JavaScript: Regular Expressions, Tools, and Techniques for Optimal Performance

BUY & SAVE
$24.51 $51.95
Save 53%
Text Processing with JavaScript: Regular Expressions, Tools, and Techniques for Optimal Performance
3 Hands-On Web Scraping with Python: Perform advanced scraping operations using various Python libraries and tools such as Selenium, Regex, and others

Hands-On Web Scraping with Python: Perform advanced scraping operations using various Python libraries and tools such as Selenium, Regex, and others

BUY & SAVE
$47.42 $49.99
Save 5%
Hands-On Web Scraping with Python: Perform advanced scraping operations using various Python libraries and tools such as Selenium, Regex, and others
4 Parsing with Perl 6 Regexes and Grammars: A Recursive Descent into Parsing

Parsing with Perl 6 Regexes and Grammars: A Recursive Descent into Parsing

BUY & SAVE
$19.24
Parsing with Perl 6 Regexes and Grammars: A Recursive Descent into Parsing
5 Practical Usage of Regular Expressions: An Introduction to Regexes for Translators

Practical Usage of Regular Expressions: An Introduction to Regexes for Translators

BUY & SAVE
$25.00
Practical Usage of Regular Expressions: An Introduction to Regexes for Translators
6 sed & awk: UNIX Power Tools (Nutshell Handbooks)

sed & awk: UNIX Power Tools (Nutshell Handbooks)

BUY & SAVE
$31.99
sed & awk: UNIX Power Tools (Nutshell Handbooks)
+
ONE MORE?

To replace spaces between words using regex, you would use the regular expression pattern "\s+" to match one or more spaces. You would then use a replacement string, such as an underscore or any other desired character, to replace the spaces.

For example, if you wanted to replace spaces between words with an underscore, you could use the following code in Python:

import re

text = "Hello World"

new_text = re.sub("\s+", "_", text)

print(new_text)

This would output:

Hello_World

Alternatively, you could replace the spaces with nothing to simply remove them:

import re

text = "Hello World"

new_text = re.sub("\s+", "", text)

print(new_text)

This would output:

HelloWorld

Using regex to replace spaces between words can be a useful technique when cleaning up text data or formatting text in a specific way.

What is the best way to test a regex pattern for replacing spaces?

One way to test a regex pattern for replacing spaces is to use a regular expression tester tool online. These tools allow you to input the regex pattern and a sample text string, and then show you the matches and replacements that would occur based on the pattern.

Another way to test a regex pattern for replacing spaces is to use a programming language that supports regex operations, such as Python or JavaScript. You can write a small script that uses the regex pattern to replace spaces in a sample text string and print the result.

You can also manually test the regex pattern by using a text editor that supports regex find and replace functionality. Simply input the regex pattern and sample text string, and observe the results of the find and replace operation.

Overall, the best way to test a regex pattern for replacing spaces depends on your preference and the tools available to you.

How to replace whitespace at the beginning of a string using regex?

You can use the following regex pattern to replace whitespace at the beginning of a string:

^\s+

In this regex pattern, ^ indicates the start of the string, and \s+ matches one or more whitespace characters. To replace the leading whitespace with another character or remove it entirely, you can use the regex pattern with your desired replacement string in a regex replace function.

Here is an example in Python:

import re

string = " Hello, world!" new_string = re.sub(r"^\s+", "", string)

print(new_string)

This will output:

Hello, world!

In this example, the leading whitespace at the beginning of the string has been removed.

How to prevent replacing spaces within quoted strings using regex?

To prevent replacing spaces within quoted strings using regex, you can use negative lookahead and lookbehind assertions to make sure that the spaces being replaced are not within quoted strings. Here is an example using a regex pattern to replace spaces outside of quoted strings:

import re

def replace_spaces(text): # Match spaces that are not within quoted strings pattern = r'(?![^"]*("([^\\"]|\\.)*")[^"]*$)\s' return re.sub(pattern, '_', text)

text = 'Hello "World lorem ipsum" foo bar' new_text = replace_spaces(text) print(new_text)

In this example, the regex pattern (?![^"]*("([^\\"]|\\.)*")[^"]*$)\s matches spaces that are not within quoted strings. The (?![^"]*("([^\\"]|\\.)*")[^"]*$) part is a negative lookahead assertion that checks if a space is not followed by a quoted string, and the \s part matches the space that should be replaced.

Using this regex pattern, the replace_spaces function will replace spaces that are not within quoted strings with an underscore.