Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdint.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <regex.h>
- #include <libc.h>
- int replaceInText(const char *pattern, char **str, const char *replace)
- {
- char *s = *str;
- regex_t reg;
- char *new;
- const char *p;
- if (regcomp(®, pattern, REG_EXTENDED))
- return -1;
- size_t nm = reg.re_nsub;
- regmatch_t pmatch[nm + 1];
- while (!regexec(®, s, nm + 1, pmatch, REG_NOTBOL))
- {
- new = (char *) malloc(strlen(*str) + strlen(replace));
- strncat(new, *str, s - *str);
- p = replace;
- strncat(new, s, pmatch[0].rm_so);
- strcat(new, p);
- unsigned int off = strlen(new);
- strcat(new, s + pmatch[0].rm_eo);
- *str = (char *) malloc(strlen(new) + 1);
- strcpy(*str, new);
- s = *str + off;
- free(new);
- }
- regfree(®);
- *str = (char *) realloc(*str, strlen(*str) + 1);
- return 0;
- }
- int main(int argc, char **argv)
- {
- char *regex = argv[1];
- char *text = argv[2];
- char *replacement = argv[3];
- replaceInText(regex, &text, replacement);
- printf("%s", text);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement