简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM