Regex, group, find the last match

I was hoping for this regex

([A-Z]+)$ 

will select the last event in this example:

AB.012.00.022ABC-1
AB.013.00.022AB-1
AB.014.00.022ABAB-1

But I have no matches. If I remove the '$', I get:

AB and ABC
AB and AB
AB and ABAB

I need only the last case (ABC / AB / ABAB). "AB". should not return the match. How to ignore it? Something like (^ ??.)

+4
source share
3 answers

To get the last occurrence, simply use a positive forecast that will make the regex engine match the last characters at the end:

([A-Z]+)(?=-\d+$)

, AB.012.00.022ABC-1, AB.013.00.022AB-1, AB.014.00.022ABAB-1 ( -1). -1 , (?=-1$) look-ahead.

+3

-, lookahead, , . [A-Z]+(?!.*[A-Z]), [A-Z] , .

+1

\Z, .

, ABC/AB/ABAB, :

^((?:ABC|AB|ABAB)\.\S+)\Z

, :

((?:ABC|AB|ABAB)-\d+)\Z

, :

(ABC|AB|ABAB)-\d+$    # with the M flag

+1
source

All Articles