-2

I'm looping through an array of string-based song titles. My goal is to find if my base title is present within the array identical titles but with possible/additional characters. I'm running into a few problems with the few techniques I'm currently using. Using includes? I must test both ways while downcasing:

"The Girl Is Mine (Paul McCartney)".downcase.include? "The Girl Is Mine" || "The Girl Is Mine".downcase.include? "The Girl Is Mine (Paul McCartney)"

I came across match? but once additional the additional characters come in things don't work.

"The Girl Is Mine".match? /The Girl Is Mine (feat. Paul McCartney)/i
=> false

The goal is to find the beginning sequence of characters, case insensitive: "The Girl Is Mine". I don't really need anything to complex so if this can be done without an algorithm I'm all ears. If not I'm still all ears.

Carl Edwards
  • 13,826
  • 11
  • 57
  • 119
  • Escape special chars. – Wiktor Stribiżew Oct 12 '20 at 13:53
  • @WiktorStribiżew I don't agree with the dup. The asker is not looking specifically to embed a string in a regexp, they are looking for some more generic string-based search functionality (probably would still close it as too broad) – Max Oct 12 '20 at 15:53
  • @Max See "*once additional the additional characters come in things don't work*" - the regex string contains parenetheses and a dot, these are special regex metacharacters, and these are the root cause of the question. The duplicate is good, I have added another one explaining which chars must be escaped in regex. – Wiktor Stribiżew Oct 12 '20 at 15:57
  • @WiktorStribiżew regexs are pointless in this case. Ruby can explicitly check for substrings without constructing one. I think the asker is confused because even without special characters getting in the way, they don't seem to know what kind of strings should match (see my answer) – Max Oct 12 '20 at 17:25
  • @Max Then why another answer if there already is https://stackoverflow.com/questions/8258517/how-to-check-whether-a-string-contains-a-substring-in-ruby? – Wiktor Stribiżew Oct 12 '20 at 17:27

1 Answers1

-1

If you actually want to do this with RegEx it looks like you are missing the wildcards neccesary to match anything before and after your actual search term.

"The Girl Is Mine (Paul McCartney)".match(/.*The girl is mine.*/i)

https://regex101.com/r/aGTEmQ/1

Oli
  • 830
  • 7
  • 20