By registering with us, you'll be able to discuss, share and private message with other members of our community.
SignUp Now!How am I to interpret the return value of @REGEX[] below? It doesn't seem to be the (documented) "number of matching groups".
Code:e:\logs\mercury> echo %@REGEX["(efused)|(uthor)|(known)",refused] 4
UChar *mstart=(UChar*)szString,
*mend=(UChar*)szString + 2 * lstrlen(szString);
OnigRegion *region = onig_region_new();
// here's the interesting stuff
INT matches = 0, i;
while ( onig_search(regex, mstart, mend, mstart, mend, region, 0) GE 0 )
{
matches += 1;
// find the match and move past it
// first see if the match was a group
for ( i=1; i < region-GTnum_regs; i++ )
{
if ( region->beg[i] GE 0 ) // match was a group
{
mstart += region-GTend[i];
break; // keep looking (continue the while)
}
}
if ( i == region-GTnum_regs ) // match was not a group (region 0)
{
mstart += region-GTend[0];
}
// keep looking (continue the while)
}
Sprintf(psz, L"%d", matches);
g:\projects\4utils\release> echo %@regex[o|g,doggiepoo]
5
g:\projects\4utils\release> echo %@regex[(oo)|(g),doggiepoo]
3
g:\projects\4utils\release> echo %@regex[(oo)|(gg),doggiepoo]
2
g:\projects\4utils\release> echo %@regex[(o)|(g),doggie]
3
g:\projects\4utils\release> echo %@regex[(o)|g,doggie]
3
g:\projects\4utils\release> echo %@regex[o|g,doggie]
3
g:\projects\4utils\release> echo %@regex[(s)|f,doggie]
0
g:\projects\4utils\release> echo %@regex[o|h,dog]
1
g:\projects\4utils\release> echo %@regex[(foo),foozzz]
1
g:\projects\4utils\release> echo %@regex[(foo),foozzzfoo]
2
INT matches = 0;
while ( onig_search(regex, mstart, mend, mstart, mend, region, 0) GE 0 )
{
matches += 1;
for ( INT i = region-GTnum_regs-1; i GE 0; i-- )
{
if ( region->beg[i] GE 0 )
{
mstart += region-GTend[i];
break;
}
}
}
Sprintf(psz, L"%d", matches);
> How am I to interpret the return value of @REGEX[] below? It doesn't
> seem to be the (documented) "number of matching groups".
>
>
> Code:
> ---------
> e:\logs\mercury> echo %@REGEX["(efused)|(uthor)|(known)",refused]
> 4
> ---------
v:\> echo %@regex[(a)|(b)|(c),cat]
4
v:\> echo %@regex[(a)|(b)|(c),ccaat]
4
v:\> echo %@regex[(a)|(b)|(c),cccaaat]
4
v:\> echo %@regex[(a)|(b)|(c)|(d),cccaaat]
5
v:\> echo %@regex[(a)|(b)|(c)|(d),ccaat]
5
v:\> echo %@regex[(a)|(b)|(c)|(d),cat]
5
On Sun, 11 Jul 2010 22:25:42 -0400, rconn <>
wrote:
|So -- what are you trying to do
I was just pointing out that, contrary to the help, @REGEX[] doesn't
return the number of matching groups. The code I posted (and the
complete version I emailed you) simply always returns the number of
matches. As far as counting matches is concerned, groups are not
significant; there are 3 matches here [a|b|c,cab] as well as here
[(a)|(b)|(c),cab] ... also here [(a|b|c),cab]. I'm not even sure
whether there's any point in using groups in a simple "find_a_match"
or "count_the_matches" function.
UChar *at = (UChar*) pString,
*mend=(UChar*)pString + lstrlen(pString) * sizeof(WCHAR);
INT matches = 0,
matchlen;
while ( at < mend )
{
matchlen = onig_match(regex, (UChar*) pString, mend, at, NULL, option);
if ( matchlen >= 0 )
{
matches += 1;
at += matchlen;
}
else
{
at += 2;
}
}
Sprintf(psz, L"%d", matches);