Regex - How to capture all iterations of a repeating pattern?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


Regex - How to capture all iterations of a repeating pattern?



Take a look at this example, I want to capture not just the last iteration (which is 5), but also 2, 3 and 4. It says here:



A repeated capturing group will only capture the last iteration. Put a capturing group around the repeated group to capture all iterations...



but I don't know how to do this. Since I need this in C++, I was looking for a way to get all iterations of capturing group using some regex functions in C++, but I always end up with same groups which the website finds.





How about instruction d((,d)*)? (IMHO, this is meant in the cited text.) Though, this leaves still the task to separate the inner matches...
– Scheff
17 mins ago




instruction d((,d)*)





Though I like reg. expressions in general, I'm not so enthusiastic about usage in C++. This language has enough power to write any parsers in "readable" code with any context dependency beyond what reg. ex. can do (though the code size will surely become a bit larger). ;-) (It's just an opinion.)
– Scheff
12 mins ago







I love regexp, but I hate hardcoded regular expressions - IMO this bad practice. RE should be used as configuration option or as user input. IMO problem here is trying match everything at once instead do constitutive searches.
– Marek R
11 mins ago





I also understood that is what the comment reffered to, but nothing changes, see regex101.com/r/DO1hQx/5
– Vladimir
10 mins ago





but nothing changes? Now, you get two groups: ,2,3,4,5 and ,5. Isn't it what you were looking for?
– Scheff
8 mins ago


,2,3,4,5


,5




1 Answer
1



If you're only interested in the numbers, you can use a positive Lookbehind combined with G to match each individual number. Something like this should work:


G


(?<=instructions|(?!^)G,)d



Explanation of the regex:


(?<= # Start of a positive Lookbehind.
instruction # Matches the word 'instruction' literally.
s # Matches a whitespace.
| # Alternation (OR).
(?! # Start of a negative Lookahead.
^ # Asserts position at the beginning of the string.
) # End of the negative Lookahead.
G # Asserts position at the end of the previous match.
, # Matches the character ',' literally.
) # End of the positive Lookbehind.
d # Matches a digit character.



Here's a demo.





To the downvoter, can you explain what's wrong with the answer?
– Ahmed Abdelhameed
1 min ago






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

C# - How to create a semi transparent or blurred backcolor on windows form

Swipe gestures in WKWebView

How to populate data on nav-tab in partial View in MVC?