How do I remove all characters in a string until a substring is matched, in Ruby?

后端 未结 7 921
时光说笑
时光说笑 2021-02-13 16:48

Say I have a string: Hey what\'s up @dude, @how\'s it going?

I\'d like to remove all the characters before@how\'s.

7条回答
  •  醉酒成梦
    2021-02-13 17:28

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

    Case insensitive

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

    Or using scan()

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

提交回复
热议问题