writing a regular expression in ruby to find out if a string contains a certain word code example

Example 1: ruby match word in string

text = "A regular expression is a sequence of characters that define a search pattern."

puts 'Found "A" at the beginning of the string.' if text.match(/^A/)
puts 'Found "O" at the beginning of the string.' if text.match(/^O/)

puts 'Found the string "character".' if text.match(/character/)
puts 'Found the word "character".' if text.match(/character\b/)

Example 2: hoow to match a complete word in ruby?

p text.scan /\bcat\b/

Tags:

Ruby Example