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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
.TH PCRE 3
.SH NAME
pcre_subst - Perl-compatible regular expression subsitution.
.SH SYNOPSIS
.B #include <pcre.h>
.br
.B #include <pcre_subst.h>
.PP
.SM
.br
int pcre_exec(const pcre *\fIcode\fR, const pcre_extra *\fIextra\fR,
.ti +5n
const char *\fIsubject\fR, int \fIlength\fR, int \fIstartoffset\fR,
.ti +5n
int \fIoptions\fR, char *\fIreplacement\fR);
.SH DESCRIPTION
\fBpcre_subst\fR is a convenience routine that calls \fIpcre_exec\fR,
and returns a freshly allocated string based on the \fIsubject\fR with
the \fIreplacement\fR action applied. Unlike \fIsubject\fR, whics is
passed as a byte array with a length, \fIreplacement\fR is expected to
be a zero terminated string (most users will just pass \fIstrlen(subject)\fR
as the \fIlength\fR).
.br
If no match is found, pcre_subst returns NULL. The returned string is zero
terminated (note that \fIsubject\fR doesn't have to be). For information
on the \fIcode\fR, \fIextra\fR, \fIsubject\fR, \fIlength\fR,
\fIstartoffset\fR and \fIoptions\fR parameters, please see \fBpcre(3)\fR.
.SH 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).
.SH EXAMPLE
.Bd -literal -compact
#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;
}
.Ed
.SH SEE ALSO
pcre(3)
|