1

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?

mozway
  • 194,879
  • 13
  • 39
  • 75
SantoshGupta7
  • 5,607
  • 14
  • 58
  • 116

1 Answers1

3

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']
mozway
  • 194,879
  • 13
  • 39
  • 75