Roland Tanglao
About

A: use backslash K in a regular expression (regex) to remove any text matched before it CRandER:: How to replace the last occurrence of a substring in ruby? - Stack Overflow

Dec 29, 2024

Discovered: Dec 29, 2024 09:34 A: use backslash K to remove any text matched before it CRandER:: How to replace the last occurrence of a substring in ruby? - Stack Overflow <– QUOTE: Since Ruby 2.0 we can use \K which removes any text matched before it from the returned match. Combine with a greedy operator and you get this <– For me the following ruby snippet seems to work to remove only the last .php in “.php.blah.php”

slug = slug.sub(/.*\K\.[\D]+$/, '') 

Leave a comment on github

Roland Tanglao

  • Roland Tanglao
  • roland@rolandtanglao.com
  • rtanglao
  • rtanglao

flickr.com/roland, twitter.com/rtanglao #firefox #thunderbird