in reply to Re: how to extract string by possible groupings?
in thread how to extract string by possible groupings?
It's possibly that a capture group was missed in your explanation:
/((.*\.c\s)|(.*\.h\s)|(.*\.cpp\s))|(\s+(.*)\%\s+(of+)\s+\d+\s)|(\bNone +\b)/g #01 2 3 4 5 6 7
If you lay that out using the /x modifier it becomes more obvious:
/ ( # 1 (.*\.c\s) # 2 | (.*\.h\s) # 3 | (.*\.cpp\s) # 4 ) | (\s+ # 5 (.*)\%\s+ # 6 (of+)\s+\d+\s # 7 ) | (\bNone\b) # 8 /gx
My preference would be to first reduce the capturing to just those parts that are needed. For example, it's unlikely that one would want both "1" and "2", "3", and "4". Likewise, it's unlikely that someone would care about "5" while also caring about "6", and "7".
Second, resort to named captures: (?<somename>...). And third, to look at breaking it up into smaller problems with /g and \G
I think, in particular, that named captures and (?:...) grouping where capturing isn't needed would make this easier to use.
Dave
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^3: how to extract string by possible groupings?
by AnomalousMonk (Archbishop) on Jun 02, 2014 at 23:43 UTC | |
Re^3: how to extract string by possible groupings?
by LanX (Saint) on Jun 02, 2014 at 23:26 UTC | |
by davido (Cardinal) on Jun 02, 2014 at 23:57 UTC |