[英]Using regular expressions to modify a string
How can I use regular expressions to change this string如何使用正则表达式更改此字符串
':: 1:62 2:31 :: 3:4 4:32'
to:至:
':: 1:62.0 2:31.0 :: 3:4.0 4:32.0'
Is there any clean way to do it without using re.split
?有没有不使用
re.split
的干净方法?
>>> re.sub("([0-9]+:[0-9]+)", "\\1.0", ':: 1:62 2:31 :: 3:4 4:32')
':: 1:62.0 2:31.0 :: 3:4.0 4:32.0'
Is that adequate?这样够吗?
>>> re.sub(r'(\d)( |$)', r'\1.0\2', ':: 1:62 2:31 :: 3:4 4:32')
':: 1:62.0 2:31.0 :: 3:4.0 4:32.0'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.