Oh, there are many possibilities ...
Say we have two lines: a = "This-Test has a" and b = "This has a-Test .
Since you want to match any line that ends in "Test" , a good RegEx would be /Test$/ , which means "capital T" followed by e , then s , then t , then the end of the line ( $ ) ".
Ruby has a =~ operator that matches RegEx with a string (or string-like object):
a =~ /Test$/
You can also use String#match :
a.match(/Test$/)
Or you can use String#scan :
a.scan(/Test$/)
Or you can just use === :
/Test$/ === a
Or can you use String#end_with? :
a.end_with?('Test')
... or one of several other methods. Make your choice.
source share