Regex giving wrong result?

I'm trying to parse this input:

wc -w -l -c text.txt
using this expression:
^wc (?:(-. )*)([a-zA-Z0-9_\\. \\-]+)$

but the expression won't compile with the "?:" tag, it kinda works using this tool http://regexr.com/ but it doesn't group the flags as it should...

instead of giving me 4 groups with -w, -l, -c and text.txt, I only get 2 groups; -c and text.txt

if I remove "?:" I get 3 groups, one with all the flags (-w -l -c) and then -c and text.txt

what am I doing wrong? my code http://pastebin.com/xPNddWXk
The problem is that you still only have two capturing groups because you only have two capturing parentheses in the expression. Just because your first group matches three things doesn't give you three groups. You get one group that ends up with the last thing that it matched.

Maybe make a second regular expression to match the different flags? Or they could be extracted by other means.
Last edited on
how should I do it then?

I tried to just place (-. ) into 3 parentheses instead of 1 but all it did was that it give me -c into 3 groups, also as mentioned above, when I use the "?:" flag in the code I posted below it won't compile the expression at all (iRes on line 13 isn't 0)

I have basically none experience with regex ...
I would make two regular expressions. The first one to capture all the switches as one group and the second one to match them individually.

I am not that familiar with the regular expression engine you are using so I can't provide a proper code example. Is there any reason you are not using the standard library version <regex>? The posix library is quite difficult to work with.
that kind of is a C code :/ I'm forced to use C for this
Topic archived. No new replies allowed.