我的输入变体为字符串:
'12345 67890'
'abc 123'
'123 abc'
'abc def'
我的目的是如果两侧的字符都是数字而不是字母,则删除字符之间的空格。我正在考虑使用re模块,也许是re.sub()函数或类似的东西。
所需的输出:
'1234567890'
'abc 123'
'123 abc'
'abc def'
谢谢
regex
与前瞻性结合使用:
>>> import re
>>> re.sub(r'(?<=\d)\s(?=\d)', '', '12345 67890')
'1234567890'
>>> re.sub(r'(?<=\d)\s(?=\d)', '', 'abc 123')
'abc 123'
>>> re.sub(r'(?<=\d)\s(?=\d)', '', '123 abc')
'123 abc'
>>> re.sub(r'(?<=\d)\s(?=\d)', '', 'abc def')
'abc def'
>>> re.sub(r'(?<=\d)\s(?=\d)', '', '123 abc 1234 456')
'123 abc 1234456'
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句