How to extract numbers from a string in Python?

后端 未结 17 2642
星月不相逢
星月不相逢 2020-11-21 05:19

I would extract all the numbers contained in a string. Which is the better suited for the purpose, regular expressions or the isdigit() method?

Example:

17条回答
  •  生来不讨喜
    2020-11-21 06:01

    I was looking for a solution to remove strings' masks, specifically from Brazilian phones numbers, this post not answered but inspired me. This is my solution:

    >>> phone_number = '+55(11)8715-9877'
    >>> ''.join([n for n in phone_number if n.isdigit()])
    '551187159877'
    

提交回复
热议问题