Lior Elrom
Lior Elrom

Reputation: 20862

Scan for substring and Ignore case sensitivity using Regex

I have a very long text and I'm trying to scan it in order to find few words in it. I'm looking for a function that will do the trick but will disregard lower or upper case sensitivity.

Very important: I can't change the string to lower or upper case! It is a post app and if I will change it, it will be tricky to roll back.

Example:

str = "According to a new report from TechCrunch, Apple has acquired the photo technology startup SnappyLabs. The company is a one-man development team known for creating the SnappyCam app."

word = "the"

str.scan(/word/) # need to find "the" and "The"

UPDATE

How to get the string index of the first appearance of the word?

In case someone else needs it, by using str =~ (/word/i)(thanks to @steenslag)

Upvotes: 2

Views: 1929

Answers (1)

steenslag
steenslag

Reputation: 80065

str = "According to a new report from TechCrunch, Apple has acquired the photo technology startup SnappyLabs. The company is a one-man development team known for creating the SnappyCam app."

word = "the"

str.scan(/#{word}/i) # need to find "the" and "The"

The "i" after the regexp makes it case-ignoring.

Upvotes: 5

Related Questions