简体   繁体   中英

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.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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM