使用Regex扫描子字符串并忽略大小写

Lio*_*rom 2 ruby regex string substring

我的文字很长,我正在尝试对其进行扫描,以便在其中找到一些单词。我正在寻找可以解决问题的函数,但是会忽略大小写的敏感性。

非常重要:我无法将字符串更改为小写或大写!这是一个后期应用程序,如果我要更改它,回滚将非常棘手。

例:

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"
Run Code Online (Sandbox Code Playgroud)

更新

如何获取单词首次出现的字符串索引?

如果有人需要它,可以使用str =~ (/word/i)(感谢@steenslag)

ste*_*lag 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"
Run Code Online (Sandbox Code Playgroud)

正则表达式后的“ i”使其忽略大小写。

  • 是的,`use str =〜(/ word / i)(未测试)。 (2认同)