2

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)

1 Answer 1

5
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.

Sign up to request clarification or add additional context in comments.

3 Comments

Is there a way of getting the str index of the first appearance of the word?
Yes, use str =~ (/word/i) (not tested).
@AHK any alternative?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.