You don't need regular expressions. Python has a built-in string method that does what you need:

mystring.replace(" ", "_")
Answer from rogeriopvl on Stack Overflow
🌐
w3resource
w3resource.com › python-exercises › re › python-re-exercise-23.php
Python: Replace whitespaces with an underscore and vice versa - w3resource
July 28, 2025 - Write a Python program to replace all spaces in a string with underscores and then convert underscores back to spaces. Write a Python script to perform a bidirectional swap between spaces and underscores in a given string. Write a Python program to toggle all spaces to underscores in a string and then reverse the process. Write a Python program to search for both spaces and underscores in a string and swap them using regex substitution.
🌐
Finxter
blog.finxter.com › home › learn python blog › how to replace whitespaces with underscores
How to Replace Whitespaces with Underscores - Be on the Right Side of Change
July 22, 2022 - Next, join() is called and passed the argument orig_quote.split(). This argument splits orig_quote on the whitespace character (' ') by default and is replaced with the underscore character ('_'). The string is re-joined, saved to new_quote and output to the terminal. This method calls in the regex library and uses re.sub() to replace each whitespace character with the underscore character.
Discussions

python - How to replace whitespaces with underscore? - Stack Overflow
I want to replace whitespace with underscore in a string to create nice URLs. So that for example: "This should be connected" Should become "This_should_be_connected" I am u... More on stackoverflow.com
🌐 stackoverflow.com
How to replace space with underscores using a regex in EPLAN?
Simple character-by-character replacements generally don't need regex, but it can be used. Just replace " " with "_". https://regex101.com/r/kVg9Jn/1 More on reddit.com
🌐 r/regex
9
1
August 26, 2024
Replace space with underscore using python - Stack Overflow
I need some help, I have a a txt file with spaces between words, I want to replace the space with underscore. fileHandler = open('nog_rename_update.txt') for eachline in fileHandler: new_name = More on stackoverflow.com
🌐 stackoverflow.com
python - How to effectively replace special chars and spaces with single underscore? - Stack Overflow
56 Replace special characters in a string in Python ... 0 How to replace all spaces by underscores and remove all the parentheses without using replace() function? 1 Replacing the space character with underscore within a matched group? 2 Regex to replace words separated by underscore in Python More on stackoverflow.com
🌐 stackoverflow.com
🌐
Java2Blog
java2blog.com › home › python › python string › replace space with underscore in python
Replace space with underscore in Python [4 ways] - Java2Blog
January 22, 2022 - The re.sub() function replaces the substring that matches with a given pattern. We can use it to replace whitespaces with an underscore in a string. See the code below. ... We import the re module that lets us work with regular expressions in Python. The \s+ pattern identifies any space in ...
🌐
Myprogrammingtutorial
myprogrammingtutorial.com › python-replace-space-with-underscore
Python Replace Space With Underscore In String (4 Ways)
August 18, 2022 - And if it matches we will replace it with the underscore and concatenate the character in the new string. ... my_string = "My Programming Tutorial" new_string = "" # using for loop for char in my_string: if char == " ": new_string += "_" else: new_string += char print("String with space ->", my_string) print("String with underscore ->", new_string) ... In Python Regular Expression or Regex is a special sequence of characters that helps to match or find the other strings.
🌐
CodeSpeedy
codespeedy.com › home › replace space with underscore in python
Replace space with underscore in Python - CodeSpeedy
November 9, 2022 - The substring that fits the specified pattern is replaced using the re.sub() method. It may be used to replace any whitespace in a string with an underscore. ... Using a separator, the split() method will divide the text into a list.
🌐
Reddit
reddit.com › r/regex › how to replace space with underscores using a regex in eplan?
r/regex on Reddit: How to replace space with underscores using a regex in EPLAN?
August 26, 2024 -

Hey, guys. I’m a total newbie when it comes to regex and have no idea what I’m looking at, so I’m asking for your help. How can I replace spaces with underscores using a regex in EPLAN?

Example string: "This is a test" --> "This_is_a _test"

I also have an image of something else I’ve done where I removed '&E5/' from the string so that only "011" was left.

In EPLAN:

Where there are a Source Text and Output Text, one can put RegEx expressions.

Solution:

🌐
Tutor Python
tutorpython.com › python-replace-space-with-underscore
Python Replace Space with Underscore - Tutor Python
October 19, 2023 - It’s a common Python practice to manipulate strings, and there are a variety of situations where you might need to swap out certain characters for others. For instance, there are circumstances wherein it becomes necessary to substitute spaces with underscores. Such as when handling file names, URLs, or other scenarios where spaces are prohibited or likely to create complications. This aspect bears significant importance in such situations. The replace() method is a built-in function in Python that allows us to replace occurrences of a specified substring with another substring.
Find elsewhere
🌐
Bobby Hadz
bobbyhadz.com › blog › python-replace-spaces-with-underscores
How to replace Spaces with Underscores in Python | bobbyhadz
Call the replace() method on the string. Pass a string containing a space and a string containing an underscore to the method.
🌐
Stack Abuse
stackabuse.com › bytes › replace-underscores-with-spaces-in-python
Replace Underscores with Spaces in Python
July 1, 2022 - >>> underscore_str = "hello_world" >>> underscore_str.split("_") ['hello', 'world']
🌐
ItSolutionstuff
itsolutionstuff.com › post › how-to-replace-whitespace-with-underscore-in-pythonexample.html
How to Replace Whitespace with Underscore in Python? - ItSolutionstuff.com
October 30, 2023 - We will use how to replace whitespace with underscore in python string. In this example, I will add myString variable with hello string. Then we will use replace() function to replace spaces with underscore in python string.
🌐
Stack Overflow
stackoverflow.com › questions › 71401739 › replace-space-with-underscore-using-python
Replace space with underscore using python - Stack Overflow
I need some help, I have a a txt file with spaces between words, I want to replace the space with underscore. fileHandler = open('nog_rename_update.txt') for eachline in fileHandler: new_name =
🌐
Python Guides
pythonguides.com › replace-whitespaces-with-underscore-in-python
Python Replace Whitespaces With Underscore
August 25, 2025 - If you’re just starting with Python, you might want to see how this can be done manually with a loop. Although I don’t usually use this in production, it’s a good way to understand how string manipulation works. ... # Example: Replace spaces with underscores using a for loop text = "Chicago Population Growth Report" result = "" for char in text: if char == " ": result += "_" else: result += char print("Original:", text) print("Modified:", result)
🌐
Bubble
forum.bubble.io › need help
How to convert all the spaces in an input to underscores - Need help - Bubble Forum
February 21, 2023 - Here for this input i want to covert all the spaces to underscores Kindly guide
🌐
Reddit
reddit.com › r/regex › help replacing spaces with underscores and limiting the amount of underscores in fibery
r/regex on Reddit: Help replacing spaces with underscores and limiting the amount of underscores in Fibery
July 24, 2024 -

I'm using Fibery to manage a bunch of business processes and trying to build a formula that uses their ReplaceRegex function, but struggling to achieve what I want.

ChatGTP keeps giving me solutions that don’t seem to work in Fibery’s approved RegEx format. I'm not entirely sure what they accept but they do link to this page in their documentation: https://medium.com/tech-tajawal/regular-expressions-the-last-guide-6800283ac034

If the input was:

Hello. I'm "___BOB___"! I'm feeling happy / healthy

I want the output to be:

hello_im_bob_im_feeling_happy_healthy

So basically:

  • All spaces should be replaced with underscores

  • All special characters (except for underscores) should be removed

  • There should never be more than 1 underscore in a row in the final output

I’ve got it mostly working with the following

Lower(
ReplaceRegex(
ReplaceRegex(
"Hello.  I'm "___BOB___"! I'm feeling happy / healthy", "[\s_]+", "_"),
"[^a-zA-Z0-9_]", "")
)

but it still spits out the following (based on my example):

hello_im__bob__im_feeling_happy__healthy

As you can see there’s a few spots that have double underscores.

How can I ensure the final output doesn’t have more than 1 underscore in a row? I know there's probably no Fibery experts here, but figured it was worth a shot...appreciate any help that could be provided.

🌐
freeCodeCamp
forum.freecodecamp.org › curriculum help
Chain regex to match underscore,space and caps - Curriculum Help - The freeCodeCamp Forum
April 26, 2019 - How can i chain regex to match caps,spaces and underscore in one regex?I can match spaces and underscore using: str.replace(/[\s,‘_’]/g,‘-’); · Part of me twitches whenever I see A-Z in a regex, what with all the world not being ASCII and all that. I guess we’ll have to wait a while ...
🌐
Finxter
blog.finxter.com › home › learn python blog › 5 best ways to replace spaces in a python string with a specific character
5 Best Ways to Replace Spaces in a Python String with a Specific Character - Be on the Right Side of Change
February 26, 2024 - This snippet iterates over each character in text, replacing it with an underscore if it is a space, or leaving it unchanged otherwise. This new sequence of characters is then joined back into a string without any spaces. Python’s re module is well-suited for string manipulation tasks that require pattern matching.