Skip to main content
13 events
when toggle format what by license comment
Dec 8, 2023 at 15:46 comment added Pierre @ssent1 FYI regular-expressions.info/captureall.html does not do what you are doing here at all. They capture all the matches at once. The OP wants to capture them one by one, which is impossible with a once-running regex.
Dec 8, 2023 at 15:32 comment added Pierre @ssent1 please fix your answer. Replace ((?:\w+)+),? with (\w+),? which is strictly equivalent, since the anonymous group is never repeated. Your general solution ((?:{{RE}})+) is absolutely wrong too obviously. THIS. DOES. NOT. WORK. AS. INTENDED. This is missleading. How can you say I'm correct and yet not fix your answer?
Oct 31, 2023 at 10:32 comment added philk And how would that pattern work, if the source string must also contain a certain start word? Say "keyword some other text 123,345,356,678" and the numbers should be captured all?
Oct 3, 2023 at 18:08 history edited InSync CC BY-SA 4.0
edited body
Feb 17, 2022 at 16:45 comment added ssent1 @Pierre You're correct. And yet it seems like there is still a distinction to be made between [Repeating a Capturing Group vs. Capturing a Repeated Group])(regular-expressions.info/captureall.html). On a practical level, it could be part of a functional solution. Ultimately, if a 'bulletproof' solution is needed, it's probably better to do it programmatically.
Jan 9, 2022 at 17:48 comment added Pierre (Note that in my first answer there is a typo in the regex I suggested, should read: ((?:\w+,?)+))
Jan 9, 2022 at 17:38 comment added Pierre @ssent1 Your ((?:\w+)+),? is equivalent to (\w+),?. Your enclosing anonymous group is never repeated. This misleading, there is nothing like "capturing a repetated group [in multiple matches]". Unfortunately, nothing in regexp can match multiple times the same group. There is only the g flag and preg_match_all that executes the regexp iteratively on the remaining unmatched string.
Jan 8, 2022 at 16:53 comment added ssent1 @Pierre Thanks for your clarification. Based on the original question(s), we have to make an assumption about what is needed. First, he said, "I want to capture every word, so that Group 1 is: HELLO…Group 3 is WORLD…" Your distinction is important because unique backreference groups are necessary for this case. The table above shows all matchs assigned to Group 1. As a result, ((?:\w+)+),? does not work. Going on to sum up, he said, "I need to capture all the groups that match the pattern, not only the last one." ((?:\w+)+),? accomplishes this with the g flag enabled.
Dec 23, 2021 at 14:36 comment added Pierre This is so wrong. "Capturing a repeated group captures all iterations": yes but it will capture ALL of them in only ONE match (containing them all). Your example should be ((?:\w,?)+) . You have multiple matches here only because of the g flag as @thomas-laurent stated. There is no way to have multiple matches from one capturing group. You have to extract and preg_match_all (or equivalent function) the repeating group.
Jan 22, 2021 at 11:21 comment added Thomas LAURENT "Capturing a repeated group captures all iterations." In your regex101 try to replace your regex with (\w+),? and it will give you the same result. The key here is the g flag which repeats your pattern to match into multiple groups.
Dec 11, 2020 at 2:49 review Late answers
Dec 11, 2020 at 3:34
Dec 11, 2020 at 2:33 history edited ssent1 CC BY-SA 4.0
Added link to an example at regex101.
Dec 11, 2020 at 2:27 history answered ssent1 CC BY-SA 4.0