Reputation: 711
I need to take a string 'list' as input and format it accordingly. Here is some example input:
string = "This is the input:1. A list element;2. Another element;3. And another one."
And I'd like the output to be a list in the following format:
list " ["This is the input:", "A list element;", "Another element;", "And another one."]
I have tried doing the following:
list = string.split('(\d+). ')
Hoping that it would split on all integers followed by a full stop and a space, but that doesn't seem to work: only a single element list is returned, indicating that none of the split criteria is found.
Anyone see what I'm doing wrong?
Upvotes: 1
Views: 55
Reputation: 473873
You can use the re.split()
method splitting by :
or ;
followed by one or more digits followed by a dot and a space:
>>> re.split(r"[:;]\d+\.\s", s)
['This is the input', 'A list element', 'Another element', 'And another one.']
To keep the :
and ;
inside the splits, you can use a positive lookbehind check:
>>> re.split(r"(?<=:|;)\d+\.\s", s)
['This is the input:', 'A list element;', 'Another element;', 'And another one.']
Upvotes: 2