oxo oxo - 2 months ago 10
Ruby Question

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

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
.

Answer

or with the regex:

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

you can read about lookaround at here

Comments