user3649739 user3649739 - 1 month ago 6x
Python Question

String after last hypen with 1-N Hypens in Regex (Python)

Given a pattern ( which can have N # of hypens where I want the text after the last one, how would I request that as I always get the first:

<details>Fiction - Mystery - Duvall</details>

<details>Fiction - Mystery - Horror - Duvall</details>

Where I want Duvall in each case.

Disclaimer: for anyone following my questions, I realize this looks a lot like

Finding the last specific character type in a string

but I tried to apply that solution to no avail. Possibly not totally understanding it as a relative Regex newbie, just didn't want the person who did answer that to think I ignored them and was asking for duplicate work.


I think what you're looking for is this:

<details>(?:\w+ - *)*(\w+)<\/details>

The idea is to match as much as possible inside the (?: ) group, which doesn't cause a backreference to be made, then match the thing you actually care about - the last token. The example below should give a bit more insight into what the syntax means.