I want to split the strings and the numbers. So if the string that is concatenated would be:
Hans went to house number 10 92384 29349
It should split the text into:
Hans went to house number | 10 | 92384 | 29349
I am confused on how to tackle this as split won't work because it will also split Hans | went | to | house | number..
Pretty easy with regular expressions:
>>> import re
>>> s = "Hans went to house number 10 92384 29349"
>>> re.split(r'\s+(?=\d+\b)', s)
['Hans went to house number', '10', '92384', '29349']
That said your question is confusing, if you want to add the | char to the output, simply join the output again:
>>> ' | '.join(_)
'Hans went to house number | 10 | 92384 | 29349'
If your goal is to implement a function that does the trick, you can write this:
def split_numbers(string, join=None):
from re import split
split = re.split(r'\s+(?=\d+\b)', string)
return join.join(split) if join else split
Notice that I added the word boundary \b on my regex to avoid matching words starting with a number like the 2cups in the sentence Hans went to house number 10 92384 29349 and drank 2cups of coffee
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With