0

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.

Vladimir
  • 1,193
  • 5
  • 17
  • 23
  • 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's Cat Aug 01 '18 at 15:40
  • 1
    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's Cat Aug 01 '18 at 15:45
  • 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 Aug 01 '18 at 15:46
  • I also understood that is what the comment reffered to, but nothing changes, see https://regex101.com/r/DO1hQx/5 – Vladimir Aug 01 '18 at 15:47
  • _but nothing changes_? Now, you get two groups: `,2,3,4,5` and `,5`. Isn't it what you were looking for? – Scheff's Cat Aug 01 '18 at 15:49
  • Actually, I made a regex to match .char(or .byte) directive in assembly, it looks like this ^.char\s*(-?\d+|".*"|'.'|\w+)(\s*,\s*(-?\d+|".*"|'.'|\w+))*$, and I need to catch all those arguments split by a comma, (although a comma might appear in one of arguments like "ad,c"). So, what do you guys suggest I should do? – Vladimir Aug 01 '18 at 15:51
  • @Scheff I want to extract 1, 2, 3, 4 and 5 but each of them separately. The example catches only the last iteration (,5), I would like to catch (,2), (,3) and (,4) as well – Vladimir Aug 01 '18 at 15:54
  • How about [`std::regex_iterator`](https://en.cppreference.com/w/cpp/regex/regex_iterator)? – ProXicT Aug 01 '18 at 15:54
  • In such situations, I prefer pure C++ code: [Appetizer](https://stackoverflow.com/a/50021308/7478597). An alternative could be something based on `lex` and `yacc` (or `flex` and `bison`). I must admit that `lex` uses reg. expressions as well but they are translated into a state machine implemented in C (and output as source code). – Scheff's Cat Aug 01 '18 at 15:55
  • @Vladimir Is it important to match the numbers only after `instruction`? – Wiktor Stribiżew Aug 01 '18 at 16:03
  • @WiktorStribiżew yeah, I don't need commas. Actually this answer which was posted looks to solve the problem, don't you guys agree? https://regex101.com/r/svPWt9/2 – Vladimir Aug 01 '18 at 16:06
  • @Vladimir It is a PCRE pattern. Are you using PCRE? All you indicated is that you want to use built-in libraries, right? Please add the regex flavor/library you are using. With PCRE, you can use [`(?:instruction\s|(?!^)\G,)\K\d+`](https://regex101.com/r/svPWt9/4), too, but is it helpful *for you*? – Wiktor Stribiżew Aug 01 '18 at 16:06
  • @WiktorStribiżew I need it to work in C++, and it looks like that regex I posted doesn't work in C++ – Vladimir Aug 01 '18 at 16:21
  • See [this post](https://stackoverflow.com/a/7257415/3832970) that explains what to do. – Wiktor Stribiżew Aug 01 '18 at 16:26
  • IMO closing this question was premature. Especially duplicate doesn't have a good answer, here is [my solution of the problem](https://wandbox.org/permlink/Bgs6YAKv0lee4vyU). – Marek R Aug 01 '18 at 20:32
  • I've provided an [answer under a duplicate](https://stackoverflow.com/a/51641925/1387438). – Marek R Aug 01 '18 at 20:40

0 Answers0