-1

I'm trying to match and keep all the following text (including the numbers) until next set of numbers

string = "100 - Generic - Applicable (Prepaid leg) 101 - TFM - N/A 102 - TOPUP - Applicable (Prepaid leg) 103 - Staff - Applicable (Prepaid leg)"

my regex below grabs the next set of digits too and breaks the rest as you can see

\b(\d{3}.*?\s\d{3})

essentially the result should look like this

list = ['100 - Generic - Applicable (Prepaid leg)','101 - TFM - N/A','etc','etc']

https://regex101.com/r/2QOMzy/1

I'm struggling to tell it to exclude\stop at the last pattern. I've looked at these, with out success obviously:

https://stackoverflow.com/questions/43899304/pcre-regex-matching-patterns-until-next-pattern-is-found

https://stackoverflow.com/questions/49487978/regex-to-match-pattern-until-next-occurence-of-it

https://stackoverflow.com/questions/33249035/regex-match-from-pattern-until-another-pattern

imp
  • 435
  • 6
  • 20

1 Answers1

1

You can omit the capture group and use a positive lookahead instead.

\b\d{3}\b.*?(?=\s+\d{3}\b)

Regex demo

If you also want to include the last match, you can add an alternation to assert the end of the string.

\b\d{3}\b.*?(?=\s+\d{3}\b|$)

Regex demo

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