- May
- 13,141
- 180
The help says "Returns the number of matching groups in the string" (if groups were specified). I may have misinterpreted that (probably did) to mean the number of matches. Whereas I suppose you meant the number of groups for which matches were found. [Recommend better wording]
In any case, @REGEX does not live up to its documentation, as this example shows.
One group was matched, and there were two matches.
I have some code that does both (it could do either, I suppose) ... I'd be glad to share it with you. Some examples far below.
It doesn't seem to make much sense to count groups that were matched unless the regex is a disjunction of groups. For example
will only match if both groups match. Even with a disjunction of groups, the results may not be as expected. For example, in
(2) will never be matches because each onig_search or onig_match will match (\d) first (and stop). [But all that's the user's problem.]
Here's an example of what I've got. %XMATCH[] returns a pair: number of matches,number of matched groups.
8 matches, 2 groups matched
I don't know what sort of thing to test here. Folks, please make suggestions.
Here's the working part of my code (it's quite fast; handles up to 63 groups).
P.S. Did you fix vbulletin's mishandling of less/greater symbols inside code tags? There doesn't seem to be a problem any more.
In any case, @REGEX does not live up to its documentation, as this example shows.
Code:
echo %@regex[(a)|(b)|(c),maam]
4
I have some code that does both (it could do either, I suppose) ... I'd be glad to share it with you. Some examples far below.
It doesn't seem to make much sense to count groups that were matched unless the regex is a disjunction of groups. For example
Code:
%@REGEX[(a)(b),...]
Code:
%@REGEX[(\d)|(2),...]
Here's an example of what I've got. %XMATCH[] returns a pair: number of matches,number of matched groups.
Code:
v:\> echo %@xmatch[(i)|(s)|(q),Mississippi]
8,2
I don't know what sort of thing to test here. Folks, please make suggestions.
Here's the working part of my code (it's quite fast; handles up to 63 groups).
P.S. Did you fix vbulletin's mishandling of less/greater symbols inside code tags? There doesn't seem to be a problem any more.
Code:
OnigRegion *region = onig_region_new();
UChar *at = (UChar*) pString,
*mend = (UChar*) strend(pString);
INT matches = 0,
rmatches = 0,
matchlen;
ULONGLONG regionmap = 0;
while ( at < mend )
{
matchlen = onig_match(regex, (UChar*) pString, mend, at, region, option);
if ( matchlen >= 0 )
{
matches += 1;
at += matchlen;
for ( INT i = 1; i < region->num_regs; i += 1 )
{
if ( region->beg[i] >= 0 )
{
if ( !(regionmap & (1I64 << i)) )
{
rmatches += 1;
regionmap |= (1I64 << i);
}
}
}
}
else
{
at += 2;
}
}
Sprintf(psz, L"%d,%d", matches, rmatches);