Regexp

From RubySpec

Jump to: navigation, search

Regexp is the class for representing regular expressions in Ruby. It provides a clean way to find the pre-match, post-match and other match data.

Here is an example

 timeexp = Regexp.new(/(\d+)\:(\d+)\:(\d+)/)
 matchdata = timeexp.match(Time.now.to_s)
 matchdata[0] #gives the current time
 matchdata.pre_match #day
 matchdata.post_match #offset from UTC and the year
Personal tools