0

I have a string "foo bar man chu" and I want to select the last 5 characters from it.

The regex expression /.{5}$/ does the job of selecting them, but how do I save them to a string in Rails? gsub(/.{5}$/,'') removes them, kind of the opposite of what I want. Thanks!

1 Answers1

0

The match method will return the result of attempting to match the string with the regular expression

result = "foo bar man chu".match(/.{5}$/)

puts result
=> "n chu"

If the regular expression is not matched, then nil will be returned.

Carlos Ramirez III
  • 7,314
  • 26
  • 33