Match digits on a string with certain conditions in python

后端 未结 2 1738
情深已故
情深已故 2021-01-20 18:56

I have a sequence of strings in the form

s1 = \"Schblaum 12324 tunguska 24 234n\"
s2 = \"jacarta 331 matchika 22 234k\"
s3 = \"3239 thingolee 80394 234k\"
         


        
2条回答
  •  走了就别回头了
    2021-01-20 19:18

    Use re.findall

    >>> import re
    >>> s1 = "Schblaum 12324 tunguska 24 234n"
    >>> re.findall(r'^\S+\D*\d+|\S.*', s1)
    ['Schblaum 12324', 'tunguska 24 234n']
    >>> s2 = "jacarta 331 matchika 22 234k"
    >>> s3 = "3239 thingolee 80394 234k"
    >>> re.findall(r'^\S+\D*\d+|\S.*', s2)
    ['jacarta 331', 'matchika 22 234k']
    >>> re.findall(r'^\S+\D*\d+|\S.*', s3)
    ['3239 thingolee 80394', '234k']
    

提交回复
热议问题