Showing posts with label regex. Show all posts
Showing posts with label regex. Show all posts

Monday, February 18, 2019

python remove text from string after character(s) without using regex

pivot = '...'
required = text.split(pivot, 1)[0]

regex to find datetime in text format

import re
r = re.compile('(?P<dow>[a-zA-Z]+,) (?P<date>[0-9]{2}) (?P<month>[A-Za-z]+) (?P<year>[0-9]{4}) (?P<hour>[0-9]{2}):(?P<minute>[0-9]{2}) (?P<ampm>[A-Z]{2})')

s = "Monday, 14 January 2019 11:50 PM"

print([m.groupdict() for m in r.finditer(s)][0])


output :
 
{'dow': 'Monday,', 'date': '14', 'month': 'January', 'year': '2019', 'hour': '11', 'minute': '50', 'ampm': 'PM'}