1PCREPOSIX(3)               Library Functions Manual               PCREPOSIX(3)
2
3
4

NAME

6       PCRE - Perl-compatible regular expressions.
7

SYNOPSIS OF POSIX API

9
10       #include <pcreposix.h>
11
12       int regcomp(regex_t *preg, const char *pattern,
13            int cflags);
14
15       int regexec(regex_t *preg, const char *string,
16            size_t nmatch, regmatch_t pmatch[], int eflags);
17
18       size_t regerror(int errcode, const regex_t *preg,
19            char *errbuf, size_t errbuf_size);
20
21       void regfree(regex_t *preg);
22

DESCRIPTION

24
25       This  set  of  functions provides a POSIX-style API to the PCRE regular
26       expression package. See the pcreapi documentation for a description  of
27       PCRE's native API, which contains much additional functionality.
28
29       The functions described here are just wrapper functions that ultimately
30       call  the  PCRE  native  API.  Their  prototypes  are  defined  in  the
31       pcreposix.h  header  file,  and  on  Unix systems the library itself is
32       called pcreposix.a, so can be accessed by  adding  -lpcreposix  to  the
33       command  for  linking  an application that uses them. Because the POSIX
34       functions call the native ones, it is also necessary to add -lpcre.
35
36       I have implemented only those option bits that can be reasonably mapped
37       to PCRE native options. In addition, the option REG_EXTENDED is defined
38       with the value zero. This has no effect, but since  programs  that  are
39       written  to  the  POSIX interface often use it, this makes it easier to
40       slot in PCRE as a replacement library. Other POSIX options are not even
41       defined.
42
43       When  PCRE  is  called  via these functions, it is only the API that is
44       POSIX-like in style. The syntax and semantics of  the  regular  expres‐
45       sions  themselves  are  still  those of Perl, subject to the setting of
46       various PCRE options, as described below. "POSIX-like in  style"  means
47       that  the  API  approximates  to  the POSIX definition; it is not fully
48       POSIX-compatible, and in multi-byte encoding  domains  it  is  probably
49       even less compatible.
50
51       The  header for these functions is supplied as pcreposix.h to avoid any
52       potential clash with other POSIX  libraries.  It  can,  of  course,  be
53       renamed or aliased as regex.h, which is the "correct" name. It provides
54       two structure types, regex_t for  compiled  internal  forms,  and  reg‐
55       match_t  for  returning  captured substrings. It also defines some con‐
56       stants whose names start  with  "REG_";  these  are  used  for  setting
57       options and identifying error codes.
58

COMPILING A PATTERN

60
61       The  function regcomp() is called to compile a pattern into an internal
62       form. The pattern is a C string terminated by a  binary  zero,  and  is
63       passed  in  the  argument  pattern. The preg argument is a pointer to a
64       regex_t structure that is used as a base for storing information  about
65       the compiled regular expression.
66
67       The argument cflags is either zero, or contains one or more of the bits
68       defined by the following macros:
69
70         REG_DOTALL
71
72       The PCRE_DOTALL option is set when the regular expression is passed for
73       compilation to the native function. Note that REG_DOTALL is not part of
74       the POSIX standard.
75
76         REG_ICASE
77
78       The PCRE_CASELESS option is set when the regular expression  is  passed
79       for compilation to the native function.
80
81         REG_NEWLINE
82
83       The  PCRE_MULTILINE option is set when the regular expression is passed
84       for compilation to the native function. Note that this does  not  mimic
85       the  defined  POSIX  behaviour  for REG_NEWLINE (see the following sec‐
86       tion).
87
88         REG_NOSUB
89
90       The PCRE_NO_AUTO_CAPTURE option is set when the regular  expression  is
91       passed for compilation to the native function. In addition, when a pat‐
92       tern that is compiled with this flag is passed to regexec() for  match‐
93       ing,  the  nmatch  and  pmatch  arguments  are ignored, and no captured
94       strings are returned.
95
96         REG_UTF8
97
98       The PCRE_UTF8 option is set when the regular expression is  passed  for
99       compilation  to the native function. This causes the pattern itself and
100       all data strings used for matching it to be treated as  UTF-8  strings.
101       Note that REG_UTF8 is not part of the POSIX standard.
102
103       In  the  absence  of  these  flags, no options are passed to the native
104       function.  This means the the  regex  is  compiled  with  PCRE  default
105       semantics.  In particular, the way it handles newline characters in the
106       subject string is the Perl way, not the POSIX way.  Note  that  setting
107       PCRE_MULTILINE  has only some of the effects specified for REG_NEWLINE.
108       It does not affect the way newlines are matched by . (they  aren't)  or
109       by a negative class such as [^a] (they are).
110
111       The  yield of regcomp() is zero on success, and non-zero otherwise. The
112       preg structure is filled in on success, and one member of the structure
113       is  public: re_nsub contains the number of capturing subpatterns in the
114       regular expression. Various error codes are defined in the header file.
115

MATCHING NEWLINE CHARACTERS

117
118       This area is not simple, because POSIX and Perl take different views of
119       things.   It  is  not possible to get PCRE to obey POSIX semantics, but
120       then PCRE was never intended to be a POSIX engine. The following  table
121       lists  the  different  possibilities for matching newline characters in
122       PCRE:
123
124                                 Default   Change with
125
126         . matches newline          no     PCRE_DOTALL
127         newline matches [^a]       yes    not changeable
128         $ matches \n at end        yes    PCRE_DOLLARENDONLY
129         $ matches \n in middle     no     PCRE_MULTILINE
130         ^ matches \n in middle     no     PCRE_MULTILINE
131
132       This is the equivalent table for POSIX:
133
134                                 Default   Change with
135
136         . matches newline          yes    REG_NEWLINE
137         newline matches [^a]       yes    REG_NEWLINE
138         $ matches \n at end        no     REG_NEWLINE
139         $ matches \n in middle     no     REG_NEWLINE
140         ^ matches \n in middle     no     REG_NEWLINE
141
142       PCRE's behaviour is the same as Perl's, except that there is no equiva‐
143       lent  for  PCRE_DOLLAR_ENDONLY in Perl. In both PCRE and Perl, there is
144       no way to stop newline from matching [^a].
145
146       The  default  POSIX  newline  handling  can  be  obtained  by   setting
147       PCRE_DOTALL  and  PCRE_DOLLAR_ENDONLY, but there is no way to make PCRE
148       behave exactly as for the REG_NEWLINE action.
149

MATCHING A PATTERN

151
152       The function regexec() is called  to  match  a  compiled  pattern  preg
153       against  a given string, which is terminated by a zero byte, subject to
154       the options in eflags. These can be:
155
156         REG_NOTBOL
157
158       The PCRE_NOTBOL option is set when calling the underlying PCRE matching
159       function.
160
161         REG_NOTEOL
162
163       The PCRE_NOTEOL option is set when calling the underlying PCRE matching
164       function.
165
166       If the pattern was compiled with the REG_NOSUB flag, no data about  any
167       matched  strings  is  returned.  The  nmatch  and  pmatch  arguments of
168       regexec() are ignored.
169
170       Otherwise,the portion of the string that was matched, and also any cap‐
171       tured substrings, are returned via the pmatch argument, which points to
172       an array of nmatch structures of type regmatch_t, containing  the  mem‐
173       bers  rm_so  and rm_eo. These contain the offset to the first character
174       of each substring and the offset to the first character after  the  end
175       of  each substring, respectively. The 0th element of the vector relates
176       to the entire portion of string that was matched;  subsequent  elements
177       relate  to  the capturing subpatterns of the regular expression. Unused
178       entries in the array have both structure members set to -1.
179
180       A successful match yields  a  zero  return;  various  error  codes  are
181       defined  in  the  header  file,  of which REG_NOMATCH is the "expected"
182       failure code.
183

ERROR MESSAGES

185
186       The regerror() function maps a non-zero errorcode from either regcomp()
187       or  regexec()  to  a  printable message. If preg is not NULL, the error
188       should have arisen from the use of that structure. A message terminated
189       by  a  binary  zero  is  placed  in  errbuf. The length of the message,
190       including the zero, is limited to errbuf_size. The yield of  the  func‐
191       tion is the size of buffer needed to hold the whole message.
192

MEMORY USAGE

194
195       Compiling  a regular expression causes memory to be allocated and asso‐
196       ciated with the preg structure. The function regfree() frees  all  such
197       memory,  after  which  preg may no longer be used as a compiled expres‐
198       sion.
199

AUTHOR

201
202       Philip Hazel
203       University Computing Service
204       Cambridge CB2 3QH, England.
205

REVISION

207
208       Last updated: 06 March 2007
209       Copyright (c) 1997-2007 University of Cambridge.
210
211
212
213                                                                  PCREPOSIX(3)
Impressum