match(p1) public

Converts pattern to a Regexp (if it isn’t already one), then invokes its match method on str.

   'hello'.match('(.)\1')      #=> #<MatchData:0x401b3d30>
   'hello'.match('(.)\1')[0]   #=> "ll"
   'hello'.match(/(.)\1/)[0]   #=> "ll"
   'hello'.match('xx')         #=> nil
Show source
Register or log in to add new notes.
March 5, 2009
7 thanks

String#match will match single token only

>> s = “{{person}} ate {{thing}}”

> “{{person}} ate {{thing}}”

>> r = /{{(.*?)}}/

> {{}}

>> s.match®.captures

> [“person”]

Using String#scan pulls out all tokens you were searching for:

>> s.scan®.flatten

> [“person”, “thing”]

February 10, 2009
0 thanks

Cheat Sheet

I have written a short introduction and a colorful cheat sheet for Perl Compatible Regular Expressions (PCRE) as used by Ruby’s Regexp class: