How do you translate this regular-expression idiom from Perl into Python?

后端 未结 15 631
情深已故
情深已故 2020-12-04 11:16

I switched from Perl to Python about a year ago and haven\'t looked back. There is only one idiom that I\'ve ever found I can do more easily in Perl than in Python:<

15条回答
  •  南方客
    南方客 (楼主)
    2020-12-04 11:44

    import re
    
    s = '1.23 Million equals to 1230000'
    
    s = re.sub("([\d.]+)(\s*)Million", lambda m: str(round(float(m.groups()[0]) * 1000_000))+m.groups()[1], s)
    
    print(s)
    

    1230000 equals to 1230000

提交回复
热议问题