Take the following string:
"Hello,world,how-are you?h"
If I were to split it using:
import re
x = re.split("[^a-zA-Z]", string)
I would get:
["Hello","world","how","are","you","h"]
Then, to each element of the new list I would run a function, say:
y = map(str.upper, x)
How could I rejoin it using the original separators? In the above example, the rejoining process would result with:
"HELLO,WORLD,HOW-ARE-YOU?H"
Use re.sub
instead:
import re
def change(m):
return str.upper(m.group(0))
x = re.sub("[a-zA-Z]", change, string)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With