【问题标题】:Ruby 2.5, get first part of string depending substringRuby 2.5,根据子字符串获取字符串的第一部分
【发布时间】:2021-01-13 19:08:01
【问题描述】:

在 Ruby 中获取字符串的第一部分并删除第二部分的最佳方法是什么? 我知道每个案例要保留的第一部分

例子:

"The Cuvée titi" => "The Cuvée"
"The Cuvée toto" => "The Cuvée"
"The Cuvée toto 1234" => "The Cuvée"
"1234 The Cuvée" => need to do nothing

"The wine 45 67" => "The wine"
"The wine not good" => " The wine"
"What's The wine ?" => need to do nothing

在阅读了一些讨论后,我尝试了很多东西,包括:

  • sub(), delete() => 保留第二部分,而不是第一部分
  • 即使是 ruby​​ 2.5 的 delete_suffix() => 在某些情况下也不起作用
  • string[0..x] => 但在字符串开头不好时也可以使用

是否需要正则表达式? 谢谢

【问题讨论】:

    标签: ruby string substring


    【解决方案1】:

    好吧,这就是我所做的:

    if string.start_with? string_to_keep
     new_string = string[0.. string_to_keep.length-1]
    end
    

    当然不是最好的 Ruby 方式,但可以工作......

    【讨论】:

      【解决方案2】:
      key_string = "The Cuvée"
      example_string_1 = "The Cuvée toto 1234"
      example_string_2 = "1234 The Cuvée"
      

      index 将返回您的示例中第一次出现的键字符串。然后,您可以使用 slice 返回所有内容,包括第一次出现的密钥字符串,例如:

      result1 = example_string_1.slice(0, example_string_1.index(key_string) + key_string.length)
       => "The Cuvée"
      
      result2 = example_string_2.slice(0, example_string_2.index(key_string) + key_string.length)
       => "1234 The Cuvée"
      

      【讨论】:

        【解决方案3】:

        我不太明白这个问题。

        如果你知道字符串的前半部分是什么,并且只想保留字符串的前半部分,则无需做任何事情,因为你已经知道字符串的前半部分是什么。

        if string.start_with?(string_to_keep)
          string_to_keep
        end
        

        【讨论】:

        • 真的。对不起,我觉得我的夜晚太短了! ;)
        • @CarySwoveland:在问题中,OP 给出了几个示例,其中字符串不以搜索字符串开头。 (#4 和 #7。)
        【解决方案4】:

        在我看来,目标可以被视为删除给定字符串后面的字符串部分。

        def doit(str, given_str)
          str.gsub(/(?<=\b#{given_str}\b).*/, '')
        end
        
        given_str = "The Cuvée"
        doit "The Cuvée titi", given_str       #=> "The Cuvée"
        doit "The Cuvée toto", given_str       #=> "The Cuvée"
        doit "The Cuvée toto 1234", given_str  #=> "The Cuvée"
        doit "1234 The Cuvée", given_str       #=> "1234 The Cuvée" 
        
        given_str = "The wine"
        doit "The wine 45 67", given_str       #=> "The wine"
        doit "The wine not good", given_str    #=> "The wine"
        doit "What's The wine?", given_str     #=> "What's The wine"
        

        请注意,在最后一个示例中,问号已被删除。

        对于

        given_str = "The wine"
        

        正则表达式是

        /(?<=\b#{given_str}\b).*/
          #=> /(?<=\bThe wine\b).*/
        

        (?&lt;=\bThe wine\b) 是一个正向向后看,这意味着匹配在"The wine" 之前,两边都有断字。 .* 匹配零个或多个字符,尽可能多,表示字符串中的剩余字符。


        given_str = "The wine" 的另一个示例:

        doit "Go to The winery for The wine", given_str
          #=> "Go to The winery for The wine" 
        

        如果没有单词中断 (\b),这将是:

        doit "Go to The winery for The wine", given_str
          #=> "Go to The wine"
        

        这个问题并没有告诉我想要哪个结果。

        【讨论】:

        • 你好,卡里。感谢您提供的正则表达式。想要的结果更容易,因为“葡萄酒总是在我要分析的字符串的开头。
        • 我不关注。 "The Wine" 不在"What's The wine ?" 的开头。你说你必须“什么都不做”。这是否意味着如果字符串确实以"The wine" 开头,则要返回整个字符串? "The winery is there" 要返回什么?
        • 嗨,卡里。抱歉耽搁了。当给定的字符串不是开始时,需要返回完整的字符串。所以“What's The Wine” => 返回“What's The Wine” AND “The Winery” => 返回“The Winery”。所以我需要检查空格。亚历克斯。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-02-17
        • 1970-01-01
        • 2019-07-07
        • 2021-12-11
        • 1970-01-01
        • 2011-07-28
        • 2022-01-02
        相关资源
        最近更新 更多