这里我用下划线替换空格,但我无法用空格替换下划线。
import re
def repl(string):
pattern = re.compile(' ')
match = pattern.search(string)
new_str = pattern.sub('_', string)
print(new_str)
repl('But I want_to bring_out something_that we_are back to stay.')
Run Code Online (Sandbox Code Playgroud)
输出 :But_I_want_to_bring_out_something_that_we_are_back_to_stay.