C++ Subpattern Matching -



C++ Subpattern Matching -

can please show me illustration using regex (regex.h) in c/c++ search and/or extracting subpattern in regex.

in javascript, this,

var str = "the string contains 123 dots , 344 chars"; var r = /the string contains ([0-9].*?) dots , ([0-9].*?) chars/; var res = r.exec(str); var dots = res[1]; var chars = res[2]; alert('dots ' + dots + ' , chars ' + chars);

how can using regex.h in c/c++ (not boost or other libraries) ??

thanks,

there no regex.h in standard c or standard c++, i'm assuming mean posix regular look library. c example:

char const *str = "the string contains 123 dots , 344 chars"; char const *re_str = "the string contains ([0-9].*?) dots , ([0-9].*?) chars"; regex_t compiled; regmatch_t *groups; regcomp(&compiled, re_str, reg_extended); ngroups = compiled.re_nsub + 1; groups = malloc(ngroups * sizeof(regmatch_t)); regexec(&compiled, str, ngroups, groups, 0); (size_t = 0; < ngroups; i++) { if (groups[i].rm_so == (size_t)(-1)) break; else { size_t len = groups[i].rm_eo - groups[i].rm_so; char buf[len + 1]; memcpy(buf, str + groups[i].rm_so, len); buf[len] = '\0'; puts(buf); } } free(groups);

(add own error checking. more details, see this answer.)

c++ c regex

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

c# - Can ProtoBuf-Net deserialize to a flat class? -

javascript - Change element in each JQuery tab to dynamically generated colors -