-1

I have a below pattern string.

'GP0|#92ca7467-4c0d-461a-aac4-2bc8fd9ee16a;L0|#092ca7467-4c0d-461a-aac4-2bc8fd9ee16a|Analysts;GTSet|#7fa22453-62b1-4bec-b73d-01ccf115d558;GPP|#fd613617-ba9d-43e5-9990-95f96f94af2a;GPP|#ba395283-6169-4c6d-84d5-1cecb3c2a73b;GP0|#a547b95c-0dfa-4f42-b540-e55872fb2e81;L0|#0a547b95c-0dfa-4f42-b540-e55872fb2e81|Awards;GP0|#c4363ae8-8608-4309-92f0-5079c69b47e4;L0|#0c4363ae8-8608-4309-92f0-5079c69b47e4|Digital Workplace;GP0|#1976b988-a993-4f13-a1e8-d847138eebc6;L0|#01976b988-a993-4f13-a1e8-d847138eebc6|Intranet;GP0|#a68218df-b9e8-4f07-bfff-22cab83bbc0d;L0|#0a68218df-b9e8-4f07-bfff-22cab83bbc0d|Microsoft;GP0|#57737444-1a1b-4c87-a479-1548b58e44e3;L0|#057737444-1a1b-4c87-a479-1548b58e44e3|Research;'

I want to get last occurrence of the pattern which starts with GPP and ends with ;

expected output: GPP|#ba395283-6169-4c6d-84d5-1cecb3c2a73b;

I tried this regex /GPP\|.+?;/i but it gives the first occurrence i.e. GPP|#fd613617-ba9d-43e5-9990-95f96f94af2a;

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Dave Smith
  • 69
  • 2
  • 7

1 Answers1

1

As they come after each other, you can match the pattern and assert that there is no following occurrence starting with the same pattern:

As the match ends on a single character, you can match any character except ; instead using a negated character class.

\bGPP\|[^;]+;(?!GPP\|[^;]+;)

Regex demo

The fourth bird
  • 154,723
  • 16
  • 55
  • 70