I have this string:
auteur = "comte de Flandre et Hainaut, Baudouin, Jacques, Thierry"
I want to remove everything before the first comma i.e. in this case keep "Baudouin, Jacques, Thierry"
Tried this :
nom = auteur.gsub(/.*,/, '')
But that removes every before the last comma and keeps only "Thierry".
auteur.partition(",").last
# => " Baudouin, Jacques, Thierry"
Use #sub
instead of #gsub
to remove only the first occurrence and make the repetition lazy (?
):
auteur = "comte de Flandre et Hainaut, Baudouin, Jacques, Thierry"
nom = auteur.sub(/.*?,/, '') # => " Baudouin, Jacques, Thierry"
Or don't use regexes at all (returns the original string if no commas are present):
auteur.split(',', 2).last # => " Baudouin, Jacques, Thierry"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With