TheRealFakeNews
TheRealFakeNews

Reputation: 8153

Regex: How to search in reverse, obtaining all characters up to a specific character?

Say I have wordsandnumbers!morestuff!morelettersandnumbers, I want to get everything after the last ! (meaning there may be more than 2 !). How can I do this using regex?

Upvotes: 0

Views: 272

Answers (1)

trincot
trincot

Reputation: 350270

You can anchor your regular expression match to the end of your string with $, and then take all the consecutive non-exclamation marks you can up to that point:

[^!]*$

Upvotes: 2

Related Questions