如果存在执行递归替换的正则表达式操作,例如:
>>> import re
>>> pattern = re.compile(r'[?!]')
>>> s = 'what the?!'
>>> print(pattern.sub(r' \g<0> ', s))
what the ? !
有没有办法撤消递归操作?
我有这个,但它不起作用:
>>> import re
>>> pattern = re.compile(r'[?!]')
>>> s2 = pattern.sub(r' \g<0> ', s)
>>> s2
'what the ? ! '
>>> pattern2 = re.compile(r'\s[?!]\s')
>>> s3 = pattern2.sub(r'\g<0>', s2)
>>> s3
'what the ? ! '
>>> pattern2 = re.compile(r' [?!] ')
>>> s3 = pattern2.sub(r'\g<0>', s2)
>>> s3
'what the ? ! '
答案 0 :(得分:2)
您必须将您的角色类包装在括号中以创建group。然后在替换期间,您可以将整个匹配(包括空格)替换为组(它没有空格)。
>>> import re
>>> s2 = 'what the ? ! '
>>> pattern2 = re.compile(r'\s([?!])\s') # capture the punctuation part as group 1
>>> s3 = pattern2.sub(r'\g<1>', s2) # replace the matches with the captured group 1
>>> s3
'what the?!'