PCRE(3)

NAME
pcre_subst - Perl-compatible regular expression subsitution.

SYNOPSIS
#include <pcre.h>
#include <pcre_subst.h>

int pcre_exec(const pcre *code, const pcre_extra *extra,
const char *subject, int length, int startoffset,
int options, char *replacement);

DESCRIPTION
pcre_subst is a convenience routine that calls pcre_exec, and returns a freshly allocated string based on the subject with the replacement action applied. Unlike subject, whics is passed as a byte array with a length, replacement is expected to be a zero terminated string (most users will just pass strlen(subject) as the length).
If no match is found, pcre_subst returns NULL. The returned string is zero terminated (note that subject doesn't have to be). For information on the code, extra, subject, length, startoffset and options parameters, please see pcre(3).

REPLACEMENT STRING
The replacement string supports a subset of the PERL replacement string. In particular, \1 style escapes are not supported (actually, only the $1 style is handled).

EXAMPLE
#include <stdio.h> #include <pcre.h> #include "pcre_subst.h" int main() {
    char *pat = "quick\\s(\\w+)\\s(fox)";
    char *rep = "$1ish $2";
    char *str = "The quick brown foxy";
    char *newstr;
    const char *err;
    int erroff;
    pcre_extra *extra;
    pcre *p = pcre_compile(pat, 0, &err, &erroff, NULL);
    if (p == NULL) {
        fprintf(stderr, "%s at %d\n", err, erroff);
        exit(1);
    }
    extra = pcre_study(p, 0, &err);
    if (err != NULL)
        fprintf(stderr, "Study %s: %s\n", pat, err);
    newstr = pcre_subst(ppat, extra, str, strlen(str),
                        0, 0, rep);
    if (newstr) {
        printf("New string: %s\n", newstr);
        pcre_free(newstr);
    };
    return 0; }

SEE ALSO
pcre(3)