iMelody O. wrote:
puts show_regexp(“abc as;”,/as$/)
/as$/ means “match ‘a’ then ‘s’ then end-of-line”. But there is a
semicolon after the ‘s’ in the source string, so the match fails.
puts show_regexp(“abc as;”,/\zas/)
\z matches end of string, and you’ve asked to match characters beyond
the end of string!
Your other cases are just variants of this.
Try matching with /as;$/ or /as;\z/
[There are subtle differences. \z matches only the end of the string. \Z
matches end of string or a newline character at the end of the string. $
matches end of string or a newline anywhere within the string]
Also, don’t bother with a show_regexp function while you’re working this
out. Just use irb to try it out interactively.
$ irb --simple-prompt
src = “abc as;”
=> “abc as;”
src =~ /as$/
src =~ /as;$/
[$`, $&, $’]
=> ["abc ", “as;”, “”]
nil means no match, 4 means matched at position 4 (counting 0 as the
first character of the string)