| Next | 3.5 Regular expressions | Prev |
Ruby duplicates the syntax of Perl regexpes. However it also provides the OO syntax.
| Usual syntax | Alternative syntax | OO syntax |
|---|---|---|
| /^\s*[a-z]/ | %r{^\s*[a-z]/} | Regexp.new('^\s*[a-z]') |
Many methods (like grep, gsub) accept both regexp and string.
(a = "The moon light") =~ / / a =~ /t/i a =~ /x/ a =~ "oo" |
3 0 nil 5 |
Object oriented internals:
re = /(\d+):(\d+)/; p re.class
md = re.match("Time: 21:15 IST"); p md.class
p md[0] # == $&
p md[1] # == $1
p md[2] # == $2
p md.pre_match # == $`
p md.post_match # == $'
|
Regexp MatchData "21:15" "21" "15" "Time: " " IST" |
| Next | Ruby for Perl programmers | Prev |