Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Regex: Match a capturing group repeatedly

I just encountered a problem. I am trying to match a capturing group repeatedly, but it apparently only matches the last found
Example of what i want to do:

Input: 1:100,2:50,3:40 ENDLEVEL
Used: ([0-9]+:[0-9]+)+ ENDLEVEL
Expected matched groups: 1:100, 2:50, 3:40
Matched groups: 3:40

I have, prior to this, seen a question about this problem, where the answer was to use the {num}, but I am unsure of how I would use it in my case. Please excuse my dumb yet learning ass and help me if you can, thanks in advance

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

P.S. From Regex101: A repeated capturing group will only capture the last iteration. Put a capturing group around the repeated group to capture all iterations or use a non-capturing group instead if you're not interested in the data.

>Solution :

If you want to match your three values separately, you can use a positive lookaround, which will attempt to see whether the match ([0-9]+:[0-9]+) is followed by:

  • an optional comma,
  • any character,
  • a space
  • the ENDLEVEL string

Here’s the final regex:

[0-9]+:[0-9]+(?=,?.* ENDLEVEL)

Check the demo here.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading