SantoshGupta7
SantoshGupta7

Reputation: 6197

How to split a string but keep multiple delimiters with the original chunk

Say my string is

st = 'Walking happened at 8 am breakfast happened at 9am baseball happened at 12 pm lunch happened at 1pm'

I would like to split on 'am' or 'pm', but I want those deliminters to be a part of the original chunk.

So the desired result is

splitlist = ['Walking happened at 8 am',
             'breakfast happened at 9am',
             'baseball happened at 12 pm',
             'lunch happened at 1pm']

There are many solutions for keeping the delimiter, but keeping it as a separate item in the list like this one

In Python, how do I split a string and keep the separators?

Upvotes: 1

Views: 32

Answers (1)

mozway
mozway

Reputation: 262149

You can use a lookbehind:

import re

splitlist = re.split(r'(?<=[ap]m)\s+', st)

Output:

['Walking happened at 8 am',
 'breakfast happened at 9am',
 'baseball happened at 12 pm',
 'lunch happened at 1pm']

If you want to ensure having a word boundary or a digit before am/pm (i.e not splitting after words such as "program"):

import re

splitlist = re.split(r'(?:(?<=\d[ap]m)|(?<=\b[ap]m))\s+', st)

Example:

['Walking happened at 8 am',
 'breakfast happened at 9am',
 'baseball happened at 12 pm',
 'beginning of program happened at 1pm']

Upvotes: 3

Related Questions