digout
digout

Reputation: 4252

Regex How to match everything after last occurance, but not including the matched character

I have a string, for example:

black-guitar-12-strings-7584

And I am trying to match the set of digits at the end (not always 4 in length).

So far I have:

(-)[^-]*$

Which matches the last part but I don't want to include the last hyphen also.

Any ideas? thanks.

Upvotes: 2

Views: 5781

Answers (4)

Paul Sanwald
Paul Sanwald

Reputation: 11349

(\d+)$

should work. if you have more than just digits, you could use something like

-(.+?)$

I added capturing because I assume that's what you want. it uses a non-greedy quantifier, which will match the minimum.

Upvotes: 0

MRAB
MRAB

Reputation: 20664

If you want to match everything after the last "-", this will do it:

[^-]*$

Upvotes: 2

Wrikken
Wrikken

Reputation: 70540

Just omitting it would work: [^-]+$

For more complex issues then this one you could also look at lookahead / lookbehind, but those aren't necessary here.

Upvotes: 4

Kirill Polishchuk
Kirill Polishchuk

Reputation: 56222

Try this pattern: \d+$ or [0-9]+$. It matches last sequence of digits.

Upvotes: 6

Related Questions