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\"
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']
Even without regex, all you're doing is looking for the number and splitting after it. Try:
s = "Schblaum 12324 tunguska 24 234n"
words = s.split()
for idx, word in enumerate(words[1:], start=1): # skip the first element
if word.isdigit():
break
before, after = ' '.join(words[:idx+1]), \
' '.join(words[idx+1:])
You could also use re.split
to find spaces that lookbehind and see a digit, but you'll have to process afterwards since it'll split after the first one as well.
import re
s3 = "3239 thingolee 80394 234k"
result = re.split(r"(?<=\d)\s", s3, 2) # split at most twice
if len(result) > 2:
before = ' '.join(result[:2])
else:
before = result[0]
after = result[-1]