Eaden McKee home blog

Object-Oriented Regular Expressions

14 November, 2005

Can you get any better than OO Regular Expressions?

re = /cat/
re.type     »  Regexp

re = /(\d+):(\d+)/     # match a time hh:mm
md = re.match("Time: 12:34am")
md.type     »  MatchData
md[0]         # == $&   »     "12:34"
md[1]         # == $1   »  "12"
md[2]         # == $2   »  "34"
md.pre_match  # == $`   »  "Time: "
md.post_match # == $'   »  "am"

Just one of many cool features.

Comments

blog comments powered by Disqus