Reputation: 51
I'm trying to create a regex that will take a string and replace certain characters
"Hello, Wor--ld! 1$2@3- " -> "hello-wor-ld-1-dollars-2-at-3"
My code:
name = "Hello, World! 1$2@3- "
name = re.sub("[^a-zA-Z0-9]+","-",name.lower())
print(name)
But it results in "hello-world-1-2-3-"
Upvotes: 0
Views: 108
Reputation: 626748
Here is the code that you may use as a basis to solve your issue:
import re
name = "Hello, World! 1$2@3- "
name = re.sub("[^a-zA-Z0-9@#$&]+", "-", " ".join(name.lower().split()))
dct = {'#': 'number', '@': 'at', '$': 'dollars', '&': 'and'}
name = re.sub(r'[$@#]', lambda x: f"-{dct[x.group()]}-", name)
print(name.strip('-'))
# => hello-world-1-dollars-2-at-3
See the Python demo.
Notes:
" ".join(name.lower().split())
- removes leading/trailing whitespaces, shrinks multiple whitespaces to a single occurrence between words and splits with whitespacere.sub("[^a-zA-Z0-9@#$&]+", "-", ...)
- replaces all one or more consecutive chars other than alphanumeric, #
, @
, $
and &
chars with a hyphenre.sub(r'[$@#]', lambda x: f"-{dct[x.group()]}-", name)
- replaces specified special chars with wordsname.strip('-')
removes leading/trailing hyphens.Upvotes: 1