有没有办法撤消递归正则表达式操作?
Is there a way to undo recursive regex operations?
如果有进行递归替换的正则表达式操作,例如:
>>> 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 ? ! '
您必须将字符 class 括在括号中才能创建 group。然后在替换期间,您可以将 whole 匹配(包括空格)替换为 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?!'
如果有进行递归替换的正则表达式操作,例如:
>>> 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 ? ! '
您必须将字符 class 括在括号中才能创建 group。然后在替换期间,您可以将 whole 匹配(包括空格)替换为 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?!'