Friday, 15 April 2011

python - Regex match text starting with word in a set -



python - Regex match text starting with word in a set -

suppose have wish match below in html sources:

mowe 04:30pm - 05:50pm fr 12:00pm - 01:20pm

the rule text must start 1 or more words in set {mo, tu, we...} , have formatted time afterwards. (i want whole thing matched weekday + time)

i know (mo|tu|we|th|fr|sa|su) can grab weekdays , latter time can captured [\d:- pm]{17}, how can glue regex together?

thanks help in advance!

thanks fellas, regexes work magic!

you can utilize next regex:

(mo|tu|we|th|fr|sa|su)+\s\d{2}:\d{2}(am|pm)\s-\s\d{2}:\d{2}(am|pm)

debuggex demo:

python demo:

>>> import re >>> pattern = re.compile("(mo|tu|we|th|fr|sa|su)+\s\d{2}:\d{2}(am|pm)\s-\s\d{2}:\d{2}(am|pm)") >>> pattern.match('mowe 04:30pm - 05:50pm').group(0) 'mowe 04:30pm - 05:50pm' >>> pattern.match('fr 12:00pm - 01:20pm').group(0) 'fr 12:00pm - 01:20pm'

python regex

No comments:

Post a Comment