开发者

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

开发者 https://www.devze.com 2023-03-20 01:27 出处:网络
I have a string, for example: black-guitar-12-strings-7584 And I am trying to match t开发者_如何学Pythonhe set of digits at the end (not always 4 in length).

I have a string, for example:

black-guitar-12-strings-7584

And I am trying to match t开发者_如何学Pythonhe 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.


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


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.


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

[^-]*$


(\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.

0

精彩评论

暂无评论...
验证码 换一张
取 消