Home > Enterprise >  Regex captures unwanted characters on repeated group
Regex captures unwanted characters on repeated group

Time:12-02

Subsequent captures for a group are including the comma or the closing parenthesis.

The regex:

(?<name1>\?\?\$.*)\s\((?<type1>\w )\s(?<conv>\w )\s(?<name2>\w :?:?) <(?<type2>[\w\s] )>\((?<args>. ?(?=[,)])) \)

The input:

??$PlotBarGroups@C@ImPlot@@YAXQBQBDPBCHHNNH@Z (void __cdecl ImPlot::PlotBarGroups<signed char>(char const * const * const,signed char const *,int,int,double,double,int))
??$PlotBarGroups@E@ImPlot@@YAXQBQBDPBEHHNNH@Z (void __cdecl ImPlot::PlotBarGroups<unsigned char>(char const * const * const,unsigned char const *,int,int,double,double,int))
??$PlotBarGroups@F@ImPlot@@YAXQBQBDPBFHHNNH@Z (void __cdecl ImPlot::PlotBarGroups<short>(char const * const * const,short const *,int,int,double,double,int))
??$PlotBarGroups@G@ImPlot@@YAXQBQBDPBGHHNNH@Z (void __cdecl ImPlot::PlotBarGroups<unsigned short>(char const * const * const,unsigned short const *,int,int,double,double,int))
??$PlotBarGroups@H@ImPlot@@YAXQBQBDPBHHHNNH@Z (void __cdecl ImPlot::PlotBarGroups<int>(char const * const * const,int const *,int,int,double,double,int))
??$PlotBarGroups@I@ImPlot@@YAXQBQBDPBIHHNNH@Z (void __cdecl ImPlot::PlotBarGroups<unsigned int>(char const * const * const,unsigned int const *,int,int,double,double,int))
??$PlotBarGroups@M@ImPlot@@YAXQBQBDPBMHHNNH@Z (void __cdecl ImPlot::PlotBarGroups<float>(char const * const * const,float const *,int,int,double,double,int))
??$PlotBarGroups@N@ImPlot@@YAXQBQBDPBNHHNNH@Z (void __cdecl ImPlot::PlotBarGroups<double>(char const * const * const,double const *,int,int,double,double,int))
??$PlotBarGroups@_J@ImPlot@@YAXQBQBDPB_JHHNNH@Z (void __cdecl ImPlot::PlotBarGroups<__int64>(char const * const * const,__int64 const *,int,int,double,double,int))
??$PlotBarGroups@_K@ImPlot@@YAXQBQBDPB_KHHNNH@Z (void __cdecl ImPlot::PlotBarGroups<unsigned __int64>(char const * const * const,unsigned __int64 const *,int,int,double,double,int))

Actual result:

signed char
char const * const * const
,signed char const *
,int
,int
,double
,double
,int
)

Expected result:

signed char
char const * const * const
signed char const *
int
int
double
double
int

Online sample:

https://regex101.com/r/Fkyk4P/1

Question:

How can I not capture these commas and closing parenthesis?

CodePudding user response:

You may try this regex:

(?<name1>\?{0,2}\$.*)\s\((?<type1>\w )\s(?<conv>\w )\s(?<name2>\w :{0,2}) <(?<type2>[\w\s] )>\((?: *(?<args>[^,)] )[,)]) 

Updated RegEx Demo

Take note of last repeating capture group:

(?: *(?<args>[^,)] )[,)]) 

That matches 1 of non-comma, non-) characters which must be followed by , or ).

Or if we want to make this regex more strict then use:

(?<name1>\?{0,2}\$.*)\s\((?<type1>\w )\s(?<conv>\w )\s(?<name2>\w :{0,2}) <(?<type2>[\w\s] )>\( *(?<args>[^,)] )(?:, *(?<args>[^,)] ))*\)

Here last subpattern is:

\( *(?<args>[^,)] )(?:, *(?<args>[^,)] ))*\)

RegEx Demo 2

  • Related