Baked Inhalf
Baked Inhalf

Reputation: 3735

Regex, grouping, find last match

I was hoping this regex

([A-Z]+)$ 

would pick the last occurance in this example:

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

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

AB and ABC
AB and AB
AB and ABAB

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

Upvotes: 4

Views: 107

Answers (3)

dawg
dawg

Reputation: 103744

You can use \Z which is the absolute end of string meta character.

To get the final line starting with ABC / AB / ABAB, you can do:

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

Demo

For the final part of the string that starts with those letters:

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

Demo

If you want each group of at the end of each line:

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

Demo

Upvotes: 1

Pluto
Pluto

Reputation: 3026

To match the last occurrence of anything, you can use a negative lookahead that matches what you want later on in the string. This would look like [A-Z]+(?!.*[A-Z]) where [A-Z] is replaced with anything you want to match.

Upvotes: 1

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626699

To get the last occurrence, just use a positive look-ahead that will tell the regex engine to match the final symbols at the end:

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

Your regex just looks for the capital letters from A to Z at the end of a string, but there are no uppercase letters at the end of neither AB.012.00.022ABC-1, nor AB.013.00.022AB-1, nor AB.014.00.022ABAB-1 (they all end with -1). If your strings all have -1 at the end, you can use (?=-1$) look-ahead.

See demo

Upvotes: 3

Related Questions