If it won't be simple, it simply won't be. [Hire me, source code] by Miki Tebeka, CEO, 353Solutions

Friday, April 13, 2007

Using Multi-Line String for Regular Expressions

Python (like C), concatenates strings that are separated only by white space.
This helps writing clearer regular expressions:

#!/usr/bin/env python
import re

# 2007-04-01 11:20
find_time = re.compile(
   "(?P<year>\d{4})"  # 4 digit year
   "-"
   "(?P<month>\d{2})"  # 2 digit month
   "-"
   "(?P<day>\d{2})"  # 2 digit day
   "\s+"        # white space(s)
   "(?P<hour>\d{2})"  # 2 digit hour
   ":"
   "(?P<minute>\d{2})"  # 2 digit minute
   ).search

match = find_time("The exact time is 2007-04-01  11:20.")
assert match, "can't find time"
print "MONTH: %s" % match.group("month")

4 comments:

Paddy3118 said...

You would usually set the re.VERBOSE flag in a regular expression then use triple quoted strings to encase your RE. Triple quoted strings can cover multiple lines.

See:
http://www.amk.ca/python/howto/regex/regex.html#SECTION000740000000000000000

- Paddy.

Miki said...

Good point, missed that one.

Thanks Paddy.

Anonymous said...

Unrelated (but related to wxPython)

First, thanks for that simple but lucid take-off to wxPython. I used it to begin coding an app and it really helped.

A minor glitch though, which might only be a formatting issue, when you bind the handler to the button, the function doing the handling is at the same indentation level on your slide as the Bind statement, which drives wxPython mad. Just thought you should know so that you could perhaps change this so someone else might not get stuck on this.

Once again, thank you.
Vineeth

Miki said...

Thanks Veneeth.

I'm always having problems with the indentation and Blogger UI.

If anyone know a good solution to keep indentation while posting a blog, please drop me a line.

(I always try my code 1'st, my machine didn't choke on the code ...)

Blog Archive