【问题标题】:How do I remove all characters in a string until a substring is matched, in Ruby?如何在 Ruby 中删除字符串中的所有字符,直到匹配子字符串?
【发布时间】:2011-03-31 14:13:49
【问题描述】:

假设我有一个字符串:Hey what's up @dude, @how's it going?

我想删除所有@how's之前的字符。

【问题讨论】:

    标签: ruby regex string


    【解决方案1】:

    或使用正则表达式:

    str = "Hey what's up @dude, @how's it going?"
    str.gsub!(/.*?(?=@how)/im, "") #=> "@how's it going?"
    

    您可以在here 阅读有关环视的信息

    【讨论】:

    • 考虑到它在任何情况下都可能是@how 的变体(例如@How、@HOW、@HoW 等),是否可以这样完成:str.gsub(/.*?(? =@[Hh][Oo][Ww])/, "")?
    • 当然,已修复。 (/我已被添加)
    • sub! 可以使用时,不需要gsub!,对吧?另请注意(根据我的回答),您需要 \A 锚和/或 m 标志,以防字符串在要匹配的文本之前有换行符。
    • 谢谢!抱歉,我不知道 gsub 和 sub 之间的区别。你能解释一下吗?
    • Suuuuper 老了,您现在显然可能知道其中的区别,但很高兴看到 n00bs 的答案发生了变化:gsub 进行全局搜索和替换,而 sub 在第一次匹配后停止。 gsub 可能会导致意外结果或性能不佳是较长的字符串。
    【解决方案2】:

    使用String#slice

    s = "Hey what's up @dude, @how's it going?"
    s.slice(s.index("@how")..-1)
    # => "@how's it going?"
    

    【讨论】:

    • +1 非常干净。指数不是关闭了吗?不应该是0..s.index("@how")吗?
    • 他想在赛前把所有东西都剥离掉,也就是“在赛后保留所有东西”。
    • 哦,哎呀,我误解了拼接的工作原理。我认为它删除了字符串,就像删除方法一样。我没有意识到它返回了拼接的文本以供使用。
    • 我在下面评论了另一个答案,但我想知道,因为它在任何情况下都可能是@how 的变体(例如@How、@HOW、@HoW 等),是否可以这样做: str.slice(str.index("@[Hh][Oo][Ww]")..-1)
    • 如果搜索字符串没有出现,index 将返回nil。因此,他应该在执行slice 之前检查nil,或者计划挽救因将nil 的索引传递给slice 而产生的错误。
    【解决方案3】:

    实际上有数十种方法可以做到这一点。以下是我会使用的:

    如果您想保留原始字符串

    str = "Hey what's up @dude, @how's it going?"
    str2 = str[/@how's.+/mi]
    p str, str2
    #=> "Hey what's up @dude, @how's it going?"
    #=> "@how's it going?"
    

    如果你想改变原始字符串

    str = "Hey what's up @dude, @how's it going?"
    str[/\A.+?(?=@how's)/mi] = ''
    p str
    #=> "@how's it going?"
    

    ...或...

    str = "Hey what's up @dude, @how's it going?"
    str.sub! /\A.+?(?=@how's)/mi, ''
    p str
    #=> "@how's it going?"
    

    您需要\A 锚定在字符串的开头,并使用m 标志来确保您匹配多行。

    也许最简单的方法是改变原始版本:

    str = "Hey what's up @dude, @how's it going?"
    str.replace str[/@how's.+/mi]
    p str
    #=> "@how's it going?"
    

    【讨论】:

      【解决方案4】:

      String#sliceString#index 工作正常,但如果针不在大海捞针中,则会出现 ArgumentError: bad value for range

      在这种情况下使用String#partitionString#rpartition 可能会更好:

      s.partition "@how's"
      # => ["Hey what's up @dude, ", "@how's", " it going?"]
      s.partition "not there"
      # => ["Hey what's up @dude, @how's it going?", "", ""]
      s.rpartition "not there"
      # => ["", "", "Hey what's up @dude, @how's it going?"]
      

      【讨论】:

        【解决方案5】:

        一种只获取您感兴趣的部分的简单方法。

        >> s="Hey what's up @dude, @how's it going?"
        => "Hey what's up @dude, @how's it going?"
        >> s[/@how.*$/i]
        => "@how's it going?"
        

        如果您确实需要更改字符串对象,您可以随时使用s=s[...]

        【讨论】:

          【解决方案6】:
          >> "Hey what's up @dude, @how's it going?".partition("@how's")[-2..-1].join
          => "@how's it going?"
          

          不区分大小写

          >> "Hey what's up @dude, @HoW's it going?".partition(/@how's/i)[-2..-1].join
          => "@HoW's it going?"
          

          或使用scan()

          >> "Hey what's up @dude, @HoW's it going?".scan(/@how's.*/i)[0]
          => "@HoW's it going?"
          

          【讨论】:

          • 您需要在最后一个正则表达式上添加一个 m 标志来匹配后面可能出现的任何换行符。
          【解决方案7】:

          您也可以直接在字符串上调用[](与slice相同)

          s = "Hey what's up @dude, @how's it going?"
          start_index = s.downcase.index("@how")
          start_index ? s[start_index..-1] : ""
          

          【讨论】:

            猜你喜欢
            • 2015-12-02
            • 1970-01-01
            • 2021-04-07
            • 1970-01-01
            • 2023-03-29
            • 1970-01-01
            • 2014-02-22
            • 1970-01-01
            • 2020-11-11
            相关资源
            最近更新 更多