---
name: "pcre_subst"
section: "3"
platform: "linux"
package: "rate-engine"
version: "0.5.4-1"
release: "Debian 3.1 (Sarge)"
platforms: ["linux"]
url: "https://new.carta.tech/pcre_subst"
---

# pcre\_subst(3)

Perl-compatible regular expression subsitution.

## 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)](/pcre/3.md)**.

## 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)](/exit/1.md);\
     }\
     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)](/pcre/3.md)
