#include <stdio.h>
#include <string.h>
#include <regex.h>
int main ( )
{
char * source = "* * * * 00 00 00 ; reboot" ;
char * regexString = "^(\\ *|[[:digit:]]{1,9})[[:blank:]](\\ *|[[:digit:]]{2})[[:blank:]](\\ *|[[:digit:]]{2})[[:blank:]](\\ *|[[:alpha:]]{3})[[:blank:]](\\ *|[[:digit:]]{2})[[:blank:]](\\ *|[[:digit:]]{2})[[:blank:]](\\ *|[[:digit:]]{2})[[:blank:]];[[:blank:]]([[:print:]]*)" ;
size_t maxMatches = 3 ; //I've tried for sevrals values, 2, 3 ... same Output
size_t maxGroups = 9 ; // 8 groups + 1 for whole match
regex_t regexCompiled;
regmatch_t groupArray[ maxGroups] ;
unsigned int m;
char * cursor;
if ( regcomp( & regexCompiled, regexString, REG_EXTENDED) )
{
printf ( "Could not compile regular expression.\n " ) ; return 1 ;
} ;
m = 0 ;
cursor = source;
for ( m = 0 ; m < maxMatches; m ++ )
{
if ( regexec( & regexCompiled, cursor, maxGroups, groupArray, 0 ) )
break ; // No more matches
unsigned int g = 0 ;
unsigned int offset = 0 ;
for ( g = 0 ; g < maxGroups; g++ )
{
if ( groupArray[ g] .rm_so == ( size_t ) - 1 )
break ; // No more groups
if ( g == 0 )
offset = groupArray[ g] .rm_eo ;
char cursorCopy
[ strlen ( cursor
) + 1 ] ; cursorCopy[ groupArray[ g] .rm_eo ] = 0 ;
printf ( "Match %u, Group %u: [%2u-%2u]: %s\n " , m, g, groupArray[ g] .rm_so , groupArray[ g] .rm_eo ,
cursorCopy + groupArray[ g] .rm_so ) ;
}
cursor += offset;
}
regfree( & regexCompiled) ;
return 0 ;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdHJpbmcuaD4KI2luY2x1ZGUgPHJlZ2V4Lmg+CgppbnQgbWFpbiAoKQp7CiAgICBjaGFyICogc291cmNlID0gIiogKiAqICogMDAgMDAgMDAgOyByZWJvb3QiOwogICAgY2hhciAqcmVnZXhTdHJpbmcgPSAiXih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lIG1hdGNoZXMKCiAgICAgICAgdW5zaWduZWQgaW50IGcgPSAwOwogICAgICAgIHVuc2lnbmVkIGludCBvZmZzZXQgPSAwOwogICAgICAgIGZvciAoZyA9IDA7IGcgPCBtYXhHcm91cHM7IGcrKykKICAgICAgICB7CiAgICAgICAgICAgIGlmIChncm91cEFycmF5W2ddLnJtX3NvID09IChzaXplX3QpLTEpCiAgICAgICAgICAgICAgICBicmVhazsgIC8vIE5vIG1vcmUgZ3JvdXBzCgogICAgICAgICAgICBpZiAoZyA9PSAwKQogICAgICAgICAgICAgICAgb2Zmc2V0ID0gZ3JvdXBBcnJheVtnXS5ybV9lbzsKCiAgICAgICAgICAgIGNoYXIgY3Vyc29yQ29weVtzdHJsZW4oY3Vyc29yKSArIDFdOwogICAgICAgICAgICBzdHJjcHkoY3Vyc29yQ29weSwgY3Vyc29yKTsKICAgICAgICAgICAgY3Vyc29yQ29weVtncm91cEFycmF5W2ddLnJtX2VvXSA9IDA7CiAgICAgICAgICAgIHByaW50ZigiTWF0Y2ggJXUsIEdyb3VwICV1OiBbJTJ1LSUydV06ICVzXG4iLAogICAgICAgICAgICAgICAgICAgbSwgZywgZ3JvdXBBcnJheVtnXS5ybV9zbywgZ3JvdXBBcnJheVtnXS5ybV9lbywKICAgICAgICAgICAgICAgICAgIGN1cnNvckNvcHkgKyBncm91cEFycmF5W2ddLnJtX3NvKTsKICAgICAgICB9CiAgICAgICAgY3Vyc29yICs9IG9mZnNldDsKICAgIH0KCiAgICByZWdmcmVlKCZyZWdleENvbXBpbGVkKTsKCiAgICByZXR1cm4gMDsKfQ==
stdout
Match 0, Group 0: [ 0-25]: * * * * 00 00 00 ; reboot
Match 0, Group 1: [ 0- 1]: *
Match 0, Group 2: [ 2- 3]: *
Match 0, Group 3: [ 4- 5]: *
Match 0, Group 4: [ 6- 7]: *
Match 0, Group 5: [ 8-10]: 00
Match 0, Group 6: [11-13]: 00
Match 0, Group 7: [14-16]: 00
Match 0, Group 8: [19-25]: reboot