Regex to replace all unless end of line ( $ in [ ] issue )

Viewed 120

Let's say I have a string:

s = "4n5n6n"

I would like to create a regex that removes everything except numbers and the n at the end of a string. So the final solution should look like this:

s = "456n"

If I use a regex like so:

re.sub(r'[^n$\d]', "", s)

The $ seems to lose its meaning when inside the brackets. What is the solution to this?

2 Answers
Related