#include <stdio.h>
#include <regex.h>
int main()
{
regex_t r;
regmatch_t match;
char * s="aaaabbbccc", *t=s;
char * replace="1";
char result[100];//must big enough
size_t pos=0,len=strlen(replace);
memset(result,0,100);
regcomp(&r,"a+",REG_EXTENDED);
while(regexec(&r,t,1,&match,0)==0)
{
printf("start:%lld,end:%lld\n",t-s+match.rm_so,t-s+match.rm_eo);//absolute position in string s
memcpy(result+pos,t,match.rm_so);//first copy the string that doesn't match
pos+=match.rm_so;//change pos
memcpy(result+pos,replace,len);//then replace
pos+=len;
t+=match.rm_eo;//for another match
}
memcpy(result+pos,t,strlen(t));//don't forget the last unmatch string
printf("%s",result);//that's all.
regfree(&r);
return 0;
}
#include <regex.h>
int main()
{
regex_t r;
regmatch_t match;
char * s="aaaabbbccc", *t=s;
char * replace="1";
char result[100];//must big enough
size_t pos=0,len=strlen(replace);
memset(result,0,100);
regcomp(&r,"a+",REG_EXTENDED);
while(regexec(&r,t,1,&match,0)==0)
{
printf("start:%lld,end:%lld\n",t-s+match.rm_so,t-s+match.rm_eo);//absolute position in string s
memcpy(result+pos,t,match.rm_so);//first copy the string that doesn't match
pos+=match.rm_so;//change pos
memcpy(result+pos,replace,len);//then replace
pos+=len;
t+=match.rm_eo;//for another match
}
memcpy(result+pos,t,strlen(t));//don't forget the last unmatch string
printf("%s",result);//that's all.
regfree(&r);
return 0;
}