In python, trying to replace all occurrence of a string found using regex such as:
'10am 11pm 13am 14pm 4am'
becomes
'10 am 11 pm 13 am 14 pm 4 am'
I tried
re.sub('([0-9].*)am(.*)', r'\1 am \2', ddata)
But this only replaces the last occurrence.
and
import re
regex = re.compile('([0-9].*)am+', re.S)
myfile = '10am 11pm 13am 14pm 4am'
myfile2 = regex.sub(lambda m: m.group().replace(r'am',r" am ",1), myfile)
print(myfile2)
only replaces the first occurence of 'am'
Expected results to me '10 am 11pm 13 am 14pm 4 am'
(\d{1,2})(?=[ap]m)replace with\1(see here) or(\d{1,2})([ap]m)replace with\1 \2(see here)>>> re.sub(r'(?<=\d)([ap]m)', r' \1', 'the amphitheater opens at 10am-11am and 3pm-7pm')...#OUTPUT: 'the amphitheater opens at 10 am-11 am and 3 pm-7 pm'10 am - 11 am and 3 pm - 7 pm? now that is another question altogether from the original post. :)