使用正则表达式扫描子字符串并忽略区分大小写

时间:2014-01-06 00:28:05

标签: 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"

更新

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

如果有其他人需要,请使用str =~ (/word/i)(感谢@steenslag)

1 个答案:

答案 0 :(得分:4)

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"

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