-3

I want to know if a string contains a substring.

String examples:

"Hey boy, the cat is in the kitchen ?"
"I want to know if the dog is in the kitchen"

Substring:

"the ... is in the kitchen"

But I need to ignore the words "cat, dog", how can I do that?

string.include? is not the right way because I want the complete sentence with "the" at the beginning.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
  • You could use `r = /\bthe +\w+ +is in the kitchen\b/`. Then `"the cat is in the kitchen".match?(r) #=> true` but `"the dog is in the kitchenette".match?(r) #=> false`. – Cary Swoveland May 10 '20 at 17:03
  • Your question isn't clearly asked. What does `...` signify? The location either "cat" or "dog" MUST appear? Why isn't `include?` sufficient? It tells you if the substring occurs, and that's it. Why do you need "the" at the beginning? Are you trying to work with a regular expression? – the Tin Man May 10 '20 at 20:27
  • Does this answer your question? [How to check whether a string contains a substring in Ruby](https://stackoverflow.com/questions/8258517/how-to-check-whether-a-string-contains-a-substring-in-ruby) – Alex May 11 '20 at 00:38

1 Answers1

0
def the_in_the_kitchen(string)
    return string.match?(/the \w+ is in the kitchen/i)
end

This should work. I've used a regexp, and replace the word cat or dog by \w+, which means a serie of letters. I've also added the i flag to the regexp, it stands for ignore case. Hence The DOG iS In ThE KITchen would work as well.

Ulysse BN
  • 10,116
  • 7
  • 54
  • 82