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 Overflowpython - How to replace whitespaces with underscore? - Stack Overflow
How to replace space with underscores using a regex in EPLAN?
Replace space with underscore using python - Stack Overflow
python - How to effectively replace special chars and spaces with single underscore? - Stack Overflow
You don't need regular expressions. Python has a built-in string method that does what you need:
mystring.replace(" ", "_")
Replacing spaces is fine, but I might suggest going a little further to handle other URL-hostile characters like question marks, apostrophes, exclamation points, etc.
Also note that the general consensus among SEO experts is that dashes are preferred to underscores in URLs.
import re
def urlify(s):
# Remove all non-word characters (everything except numbers and letters)
s = re.sub(r"[^\w\s]", '', s)
# Replace all runs of whitespace with a single dash
s = re.sub(r"\s+", '-', s)
return s
# Prints: I-cant-get-no-satisfaction"
print(urlify("I can't get no satisfaction!"))
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:
import re
new_s1 = re.sub(r'[\W_]+','_',s1)
new_s2 = re.sub(r'[\W_]+','_',s2)
Input:
s1 = 'Hi th@re gu^ys!@#()tes t;"[]ing'
s2 = 'Hi th@re gu^ys!@#()tes t___;"[]ing'
Output:
>>> print(new_s1)
>>> Hi_th_re_gu_ys_tes_t_ing
>>> print(new_s2)
>>> Hi_th_re_gu_ys_tes_t_ing
import re
s = 'Hi th@re gu^ys!@#()tes t;"[]ing'
new_s = re.sub(r'[\W]+','_',s)
print(new_s)
Underscore is considered a "word character" in PCRE regular expressions. If what you want to match is "anything that is not a word character or an underscore", try this:
new_str = re.sub(r'[\W_]', ' ', new_str)
The underscore is part of the \w character group. Use this instead:
new_str = re.sub(r'[^a-zA-Z0-9]', ' ', new_str)
which is the same as \w but minus the underscore.
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.
Use regex to match single spaces between words and replace them with underscore:
import re
inp = "This is a regular text"
print(re.sub(r'(?<! ) (?! )', '_', inp))
# This is_a regular_text
My take on your problem is that you want to interpolate spaces between words which are even with an underscore, instead of whitespace. This means that the second and fourth spaces would be replaced with underscore.
We can try splitting the input string on (\s+), which will preserve the text and the whitespace. Then, iterate the resulting list and interpolate underscore for the even spaces.
input = "This is a regular text"
parts = re.split(r'(\s+)', input)
output = ""
for i in range(0, len(parts)):
if (i+1) % 4 == 0:
output = output + "_"
else:
output = output + parts[i]
print(output)
This outputs:
This is_a regular_text