Return to Snippet

Revision: 5543
at March 13, 2008 21:41 by cczona


Initial Code
# pass a Regexp into a String as if it were an Array index

 "cat"[/c/]
 #=> "c" 
 "cat"[/z/]
 #=> nil


# pass in an optional second argument, to return the content of the nth matching group

 re_phone = /(\d{3})-(\d{3})-(\d{4})/
 "986-235-1001"[re_phone, 2]
 #=> "235"

Initial URL
http://whytheluckystiff.net/articles/rubyOneEightOh.html

Initial Description


Initial Title
Use regex to match a Ruby substring, and reference it like an index

Initial Tags
regex, ruby

Initial Language
Ruby