-
Notifications
You must be signed in to change notification settings - Fork 0
/
subst_test.c
39 lines (32 loc) · 1.08 KB
/
subst_test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <stdio.h>
#include <string.h>
#include <pcre.h>
#include "pcre_subst.h"
int main(int argc, char *argv[]) {
if (argc != 4) {
printf("Syntax: subst_test <string> <re> <replacement>\n");
exit(1);
}
const char *error_msg;
int error_off;
const unsigned char *pcre_tables = pcre_maketables();
pcre *re;
int matches;
#define MAX_RE_CAPTURES 5
int re_ret[3 * MAX_RE_CAPTURES];
int options = PCRE_SUBST_DEFAULT;
options |= PCRE_SUBST_NO_SPECIAL_CHARS;
options |= PCRE_SUBST_SHELL_ESCAPE_SUBJ;
re = pcre_compile(argv[2], 0, &error_msg, &error_off, pcre_tables);
pcre_subst_data *subst_data = pcre_subst_study(argv[3], options);
matches = pcre_exec(re, NULL, argv[1], strlen(argv[1]), 0, 0, re_ret, 3*MAX_RE_CAPTURES);
printf("pcre_exec returned: %d\n", matches);
if (matches > 0) {
char *s = pcre_subst_replace(argv[1], subst_data, re_ret, 3*MAX_RE_CAPTURES, matches, options);
printf("->%s<-\n", s);
pcre_subst_free(subst_data);
free(s);
} else {
printf("no match\n");
}
}