我有一个字符串序列的形式
s1 = "Schblaum 12324 tunguska 24 234n"
s2 = "jacarta 331 matchika 22 234k"
s3 = "3239 thingolee 80394 234k"
我需要将这些字符串分成两个字符串,就在字符串中间的数字之后,忽略字符串的第一部分是否有数字。有点像
["Schblaum 12324", "tunguska 24 234n"]
["jacarta 331", "matchika 22 234k"]
["3239 thingolee 80394", "bb 6238"]
我尝试在表单中使用正则表达式
finder = re.compile(""\D(\d+)\D"")
finder.search(s1)
没有用。有没有办法做到这一点,也许不使用正则表达式? 干杯!
编辑:刚刚发现初始字符串只是
"jacarta 43453"
没有其他部分。这应该返回
["jarcata 43453"]
最佳答案
使用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']
关于python - 在 python 中以特定条件匹配字符串上的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33527013/