Splitting a string where it switches between numeric and alphabetic characters

前端 未结 4 1181
闹比i
闹比i 2020-12-30 02:20

I am parsing some data where the standard format is something like 10 pizzas. Sometimes, data is input correctly and we might end up with 5pizzas i

4条回答
  •  爱一瞬间的悲伤
    2020-12-30 02:43

    To split the string at digits you can use re.split with the regular expression \d+:

    >>> import re
    >>> def my_split(s):
        return filter(None, re.split(r'(\d+)', s))
    
    >>> my_split('5pizzas')
    ['5', 'pizzas']
    >>> my_split('foo123bar')
    ['foo', '123', 'bar']
    

    To find the first number use re.search:

    >>> re.search('\d+', '5pizzas').group()
    '5'
    >>> re.search('\d+', 'foo123bar').group()
    '123'
    

    If you know the number must be at the start of the string then you can use re.match instead of re.search. If you want to find all the numbers and discard the rest you can use re.findall.

提交回复
热议问题