I am trying to separate the time from AM in a string. The code
content= "11:20pm"
content = re.findall(r"[^Wd_]+|d+", content)
print(content)
I expect ['11:20','pm']
but with my code I get ['11','20','pm']
What should I do?
I am trying to separate the time from AM in a string. The code
content= "11:20pm"
content = re.findall(r"[^Wd_]+|d+", content)
print(content)
I expect ['11:20','pm']
but with my code I get ['11','20','pm']
What should I do?
You may match the time by adding (?::d+)?
pattern to the d+
alternative:
content= "11:20pm"
content = re.findall(r"[^Wd_]+|d+(?::d+)?", content)
print(content) # => ['11:20', 'pm']
See the Python demo and the regex demo.
Note you might want to extend the pattern to also match float values, and if yes, you would need to use r"[^Wd_]+|d+(?:[:.]d+)?"
.
Details:
[^Wd_]+
- one or more letters|
- ord+
- one or more digits(?::d+)?
- an optional sequence of a `:~ and one or more digits.