0

An example of the python string is '8:30 AM- 10:00 PM Subject: Math'. In general, the string contains a start time, end time, and the subject I want to separate this string into 3 components: the start time, end time, and subject. For example 8:30 AM, 10:00 PM, and Subject: Math.

How can I do this using regex in python?

1 Answer 1

2

You can use re.split() using a positive lookbehind to AM or PM having an optional - and a space character as a delimiter:

>>> import re
>>>
>>> s = "8:30 AM- 10:00 PM Subject: Math"
>>> re.split(r"(?<=AM|PM)-?\s", s)
['8:30 AM', '10:00 PM', 'Subject: Math']
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.