【问题标题】:Scan for substring and Ignore case sensitivity using Regex使用正则表达式扫描子字符串并忽略区分大小写
【发布时间】:2014-01-23 07:56:08
【问题描述】:

我有一个很长的文本,我正在尝试扫描它以便在其中找到几个单词。 我正在寻找一个可以解决问题但忽略大小写敏感性的函数。

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

例子:

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)

【问题讨论】:

    标签: ruby regex string substring


    【解决方案1】:
    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”使其忽略大小写。

    【讨论】:

    • 有没有办法获取单词第一次出现的str索引?
    • 是的,use str =~ (/word/i)(未测试)。
    • @AHK 还有其他选择吗?
    猜你喜欢
    • 2012-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-05
    • 2013-04-10
    相关资源
    最近更新 更多