c - Working example of substitution using PCRS -
i need to substitution in string in c. recommended in 1 of answers here how regex string replacements in pure c? use pcrs library. downloaded pcrs here ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/contrib/ i'm confused how use it. below code (taken se post)
const char *error; int erroffset; pcre *re; int rc; int i; int ovector[100]; char *regex = "from:([^@]+).*"; char str[] = "from:regular.expressions@example.com\r\n"; char stringtobesubstituted[] = "gmail.com"; re = pcre_compile (regex, /* pattern */ pcre_multiline, &error, /* error message */ &erroffset, /* error offset */ 0); /* use default character tables */ if (!re) { printf("pcre_compile failed (offset: %d), %s\n", erroffset, error); return -1; } unsigned int offset = 0; unsigned int len = strlen(str); while (offset < len && (rc = pcre_exec(re, 0, str, len, offset, 0, ovector, sizeof(ovector))) >= 0) { for(int = 0; < rc; ++i) { printf("%2d: %.*s\n", i, ovector[2*i+1] - ovector[2*i], str + ovector[2*i]); } offset = ovector[1]; }
as opposed 'pcre_compile' , 'pcre_exec' functions need use pcrs?
thanks.
simply follow instructions in install
file:
to build pcrs, need pcre 3.0 or later , gcc.
installation easy: ./configure && make && make install debug mode can enabled --enable-debug.
there simple demo application (pcrsed) included.
pcrs provides following functions documented in man page pcrs.3
:
pcrs_compile
pcrs_compile_command
pcrs_execute
pcrs_execute_list
pcrs_free_job
pcrs_free_joblist
pcrs_strerror
here's online version of man page. use these functions, include header file pcrs.h
, link program against pcrs library using linker flag -lpcrs
.
Comments
Post a Comment