[英]How to modify a text within string using regular expressions in python?
[英]Using regular expressions to modify a string
如何使用正則表達式更改此字符串
':: 1:62 2:31 :: 3:4 4:32'
至:
':: 1:62.0 2:31.0 :: 3:4.0 4:32.0'
有沒有不使用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'
這樣夠嗎?
>>> 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.