#!/usr/bin/ruby
r = /ab|aac/ # find either "ab" or "aac"
while s = gets # loop as in Perl
print "s: ", s
m = r.match(s) # Ruby's O-O reg. exprs.
if m != nil
print "pre_match: ", m.pre_match
print ", match: ", m[0]
print ", post_match: ", m.post_match, "\n"
else
print "no match", "\n"
end
end
| cccaabaccc
s: cccaabaccc
pre_match: ccca, match: ab, post_match: accc
cccacaccaaacabcc
s: cccacaccaaacabcc
pre_match: cccacacca, match: aac, post_match: abcc
bbbaccc
s: bbbaccc
no match
bacbabccaaccbb
s: bacbabccaaccbb
pre_match: bacb, match: ab, post_match: ccaaccbb
bacbaaccabcc
s: bacbaaccabcc
pre_match: bacb, match: aac, post_match: cabcc
|