Elliott Hughes | 378b175 | 2021-06-08 13:42:40 -0700 | [diff] [blame] | 1 | .TH PCRE2POSIX 3 "26 April 2021" "PCRE2 10.37" |
Nick Kralevich | f73ff17 | 2014-09-27 12:41:49 -0700 | [diff] [blame] | 2 | .SH NAME |
Janis Danisevskis | 53e448c | 2016-03-31 13:35:25 +0100 | [diff] [blame] | 3 | PCRE2 - Perl-compatible regular expressions (revised API) |
Nick Kralevich | f73ff17 | 2014-09-27 12:41:49 -0700 | [diff] [blame] | 4 | .SH "SYNOPSIS" |
| 5 | .rs |
| 6 | .sp |
Janis Danisevskis | 53e448c | 2016-03-31 13:35:25 +0100 | [diff] [blame] | 7 | .B #include <pcre2posix.h> |
Nick Kralevich | f73ff17 | 2014-09-27 12:41:49 -0700 | [diff] [blame] | 8 | .PP |
| 9 | .nf |
Elliott Hughes | 0c26e19 | 2019-08-07 12:24:46 -0700 | [diff] [blame] | 10 | .B int pcre2_regcomp(regex_t *\fIpreg\fP, const char *\fIpattern\fP, |
Nick Kralevich | f73ff17 | 2014-09-27 12:41:49 -0700 | [diff] [blame] | 11 | .B " int \fIcflags\fP);" |
| 12 | .sp |
Elliott Hughes | 0c26e19 | 2019-08-07 12:24:46 -0700 | [diff] [blame] | 13 | .B int pcre2_regexec(const regex_t *\fIpreg\fP, const char *\fIstring\fP, |
Nick Kralevich | f73ff17 | 2014-09-27 12:41:49 -0700 | [diff] [blame] | 14 | .B " size_t \fInmatch\fP, regmatch_t \fIpmatch\fP[], int \fIeflags\fP);" |
Janis Danisevskis | 53e448c | 2016-03-31 13:35:25 +0100 | [diff] [blame] | 15 | .sp |
Elliott Hughes | 0c26e19 | 2019-08-07 12:24:46 -0700 | [diff] [blame] | 16 | .B "size_t pcre2_regerror(int \fIerrcode\fP, const regex_t *\fIpreg\fP," |
Nick Kralevich | f73ff17 | 2014-09-27 12:41:49 -0700 | [diff] [blame] | 17 | .B " char *\fIerrbuf\fP, size_t \fIerrbuf_size\fP);" |
| 18 | .sp |
Elliott Hughes | 0c26e19 | 2019-08-07 12:24:46 -0700 | [diff] [blame] | 19 | .B void pcre2_regfree(regex_t *\fIpreg\fP); |
Nick Kralevich | f73ff17 | 2014-09-27 12:41:49 -0700 | [diff] [blame] | 20 | .fi |
| 21 | . |
| 22 | .SH DESCRIPTION |
| 23 | .rs |
| 24 | .sp |
Janis Danisevskis | 53e448c | 2016-03-31 13:35:25 +0100 | [diff] [blame] | 25 | This set of functions provides a POSIX-style API for the PCRE2 regular |
Elliott Hughes | 0c26e19 | 2019-08-07 12:24:46 -0700 | [diff] [blame] | 26 | expression 8-bit library. There are no POSIX-style wrappers for PCRE2's 16-bit |
| 27 | and 32-bit libraries. See the |
Nick Kralevich | f73ff17 | 2014-09-27 12:41:49 -0700 | [diff] [blame] | 28 | .\" HREF |
Janis Danisevskis | 53e448c | 2016-03-31 13:35:25 +0100 | [diff] [blame] | 29 | \fBpcre2api\fP |
Nick Kralevich | f73ff17 | 2014-09-27 12:41:49 -0700 | [diff] [blame] | 30 | .\" |
Janis Danisevskis | 53e448c | 2016-03-31 13:35:25 +0100 | [diff] [blame] | 31 | documentation for a description of PCRE2's native API, which contains much |
Elliott Hughes | 0c26e19 | 2019-08-07 12:24:46 -0700 | [diff] [blame] | 32 | additional functionality. |
Nick Kralevich | f73ff17 | 2014-09-27 12:41:49 -0700 | [diff] [blame] | 33 | .P |
Elliott Hughes | 0c26e19 | 2019-08-07 12:24:46 -0700 | [diff] [blame] | 34 | The functions described here are wrapper functions that ultimately call the |
| 35 | PCRE2 native API. Their prototypes are defined in the \fBpcre2posix.h\fP header |
| 36 | file, and they all have unique names starting with \fBpcre2_\fP. However, the |
| 37 | \fBpcre2posix.h\fP header also contains macro definitions that convert the |
| 38 | standard POSIX names such \fBregcomp()\fP into \fBpcre2_regcomp()\fP etc. This |
| 39 | means that a program can use the usual POSIX names without running the risk of |
| 40 | accidentally linking with POSIX functions from a different library. |
Nick Kralevich | f73ff17 | 2014-09-27 12:41:49 -0700 | [diff] [blame] | 41 | .P |
Elliott Hughes | 0c26e19 | 2019-08-07 12:24:46 -0700 | [diff] [blame] | 42 | On Unix-like systems the PCRE2 POSIX library is called \fBlibpcre2-posix\fP, so |
| 43 | can be accessed by adding \fB-lpcre2-posix\fP to the command for linking an |
| 44 | application. Because the POSIX functions call the native ones, it is also |
| 45 | necessary to add \fB-lpcre2-8\fP. |
| 46 | .P |
Elliott Hughes | 378b175 | 2021-06-08 13:42:40 -0700 | [diff] [blame] | 47 | Although they were not defined as protypes in \fBpcre2posix.h\fP, releases |
| 48 | 10.33 to 10.36 of the library contained functions with the POSIX names |
| 49 | \fBregcomp()\fP etc. These simply passed their arguments to the PCRE2 |
| 50 | functions. These functions were provided for backwards compatibility with |
| 51 | earlier versions of PCRE2, which had only POSIX names. However, this has proved |
| 52 | troublesome in situations where a program links with several libraries, some of |
| 53 | which use PCRE2's POSIX interface while others use the real POSIX functions. |
| 54 | For this reason, the POSIX names have been removed since release 10.37. |
Elliott Hughes | 0c26e19 | 2019-08-07 12:24:46 -0700 | [diff] [blame] | 55 | .P |
| 56 | Calling the header file \fBpcre2posix.h\fP avoids any conflict with other POSIX |
| 57 | libraries. It can, of course, be renamed or aliased as \fBregex.h\fP, which is |
| 58 | the "correct" name, if there is no clash. It provides two structure types, |
| 59 | \fIregex_t\fP for compiled internal forms, and \fIregmatch_t\fP for returning |
| 60 | captured substrings. It also defines some constants whose names start with |
| 61 | "REG_"; these are used for setting options and identifying error codes. |
| 62 | . |
| 63 | . |
| 64 | .SH "USING THE POSIX FUNCTIONS" |
| 65 | .rs |
| 66 | .sp |
Janis Danisevskis | 53e448c | 2016-03-31 13:35:25 +0100 | [diff] [blame] | 67 | Those POSIX option bits that can reasonably be mapped to PCRE2 native options |
| 68 | have been implemented. In addition, the option REG_EXTENDED is defined with the |
| 69 | value zero. This has no effect, but since programs that are written to the |
| 70 | POSIX interface often use it, this makes it easier to slot in PCRE2 as a |
Nick Kralevich | f73ff17 | 2014-09-27 12:41:49 -0700 | [diff] [blame] | 71 | replacement library. Other POSIX options are not even defined. |
| 72 | .P |
Janis Danisevskis | 8b979b2 | 2016-08-15 16:09:16 +0100 | [diff] [blame] | 73 | There are also some options that are not defined by POSIX. These have been |
| 74 | added at the request of users who want to make use of certain PCRE2-specific |
Elliott Hughes | 9bc971b | 2018-07-27 13:23:14 -0700 | [diff] [blame] | 75 | features via the POSIX calling interface or to add BSD or GNU functionality. |
Nick Kralevich | f73ff17 | 2014-09-27 12:41:49 -0700 | [diff] [blame] | 76 | .P |
Janis Danisevskis | 53e448c | 2016-03-31 13:35:25 +0100 | [diff] [blame] | 77 | When PCRE2 is called via these functions, it is only the API that is POSIX-like |
Nick Kralevich | f73ff17 | 2014-09-27 12:41:49 -0700 | [diff] [blame] | 78 | in style. The syntax and semantics of the regular expressions themselves are |
Janis Danisevskis | 53e448c | 2016-03-31 13:35:25 +0100 | [diff] [blame] | 79 | still those of Perl, subject to the setting of various PCRE2 options, as |
Nick Kralevich | f73ff17 | 2014-09-27 12:41:49 -0700 | [diff] [blame] | 80 | described below. "POSIX-like in style" means that the API approximates to the |
Janis Danisevskis | 53e448c | 2016-03-31 13:35:25 +0100 | [diff] [blame] | 81 | POSIX definition; it is not fully POSIX-compatible, and in multi-unit encoding |
Nick Kralevich | f73ff17 | 2014-09-27 12:41:49 -0700 | [diff] [blame] | 82 | domains it is probably even less compatible. |
| 83 | .P |
Elliott Hughes | 0c26e19 | 2019-08-07 12:24:46 -0700 | [diff] [blame] | 84 | The descriptions below use the actual names of the functions, but, as described |
| 85 | above, the standard POSIX names (without the \fBpcre2_\fP prefix) may also be |
| 86 | used. |
Nick Kralevich | f73ff17 | 2014-09-27 12:41:49 -0700 | [diff] [blame] | 87 | . |
| 88 | . |
| 89 | .SH "COMPILING A PATTERN" |
| 90 | .rs |
| 91 | .sp |
Elliott Hughes | 0c26e19 | 2019-08-07 12:24:46 -0700 | [diff] [blame] | 92 | The function \fBpcre2_regcomp()\fP is called to compile a pattern into an |
Elliott Hughes | 9bc971b | 2018-07-27 13:23:14 -0700 | [diff] [blame] | 93 | internal form. By default, the pattern is a C string terminated by a binary |
| 94 | zero (but see REG_PEND below). The \fIpreg\fP argument is a pointer to a |
| 95 | \fBregex_t\fP structure that is used as a base for storing information about |
| 96 | the compiled regular expression. (It is also used for input when REG_PEND is |
| 97 | set.) |
Nick Kralevich | f73ff17 | 2014-09-27 12:41:49 -0700 | [diff] [blame] | 98 | .P |
| 99 | The argument \fIcflags\fP is either zero, or contains one or more of the bits |
| 100 | defined by the following macros: |
| 101 | .sp |
| 102 | REG_DOTALL |
| 103 | .sp |
Janis Danisevskis | 53e448c | 2016-03-31 13:35:25 +0100 | [diff] [blame] | 104 | The PCRE2_DOTALL option is set when the regular expression is passed for |
Nick Kralevich | f73ff17 | 2014-09-27 12:41:49 -0700 | [diff] [blame] | 105 | compilation to the native function. Note that REG_DOTALL is not part of the |
| 106 | POSIX standard. |
| 107 | .sp |
| 108 | REG_ICASE |
| 109 | .sp |
Janis Danisevskis | 53e448c | 2016-03-31 13:35:25 +0100 | [diff] [blame] | 110 | The PCRE2_CASELESS option is set when the regular expression is passed for |
Nick Kralevich | f73ff17 | 2014-09-27 12:41:49 -0700 | [diff] [blame] | 111 | compilation to the native function. |
| 112 | .sp |
| 113 | REG_NEWLINE |
| 114 | .sp |
Janis Danisevskis | 53e448c | 2016-03-31 13:35:25 +0100 | [diff] [blame] | 115 | The PCRE2_MULTILINE option is set when the regular expression is passed for |
Nick Kralevich | f73ff17 | 2014-09-27 12:41:49 -0700 | [diff] [blame] | 116 | compilation to the native function. Note that this does \fInot\fP mimic the |
| 117 | defined POSIX behaviour for REG_NEWLINE (see the following section). |
| 118 | .sp |
Elliott Hughes | 9bc971b | 2018-07-27 13:23:14 -0700 | [diff] [blame] | 119 | REG_NOSPEC |
| 120 | .sp |
| 121 | The PCRE2_LITERAL option is set when the regular expression is passed for |
| 122 | compilation to the native function. This disables all meta characters in the |
| 123 | pattern, causing it to be treated as a literal string. The only other options |
| 124 | that are allowed with REG_NOSPEC are REG_ICASE, REG_NOSUB, REG_PEND, and |
| 125 | REG_UTF. Note that REG_NOSPEC is not part of the POSIX standard. |
| 126 | .sp |
Nick Kralevich | f73ff17 | 2014-09-27 12:41:49 -0700 | [diff] [blame] | 127 | REG_NOSUB |
| 128 | .sp |
Elliott Hughes | 0c26e19 | 2019-08-07 12:24:46 -0700 | [diff] [blame] | 129 | When a pattern that is compiled with this flag is passed to |
| 130 | \fBpcre2_regexec()\fP for matching, the \fInmatch\fP and \fIpmatch\fP arguments |
| 131 | are ignored, and no captured strings are returned. Versions of the PCRE library |
| 132 | prior to 10.22 used to set the PCRE2_NO_AUTO_CAPTURE compile option, but this |
| 133 | no longer happens because it disables the use of backreferences. |
Nick Kralevich | f73ff17 | 2014-09-27 12:41:49 -0700 | [diff] [blame] | 134 | .sp |
Elliott Hughes | 9bc971b | 2018-07-27 13:23:14 -0700 | [diff] [blame] | 135 | REG_PEND |
| 136 | .sp |
| 137 | If this option is set, the \fBreg_endp\fP field in the \fIpreg\fP structure |
| 138 | (which has the type const char *) must be set to point to the character beyond |
Elliott Hughes | 0c26e19 | 2019-08-07 12:24:46 -0700 | [diff] [blame] | 139 | the end of the pattern before calling \fBpcre2_regcomp()\fP. The pattern itself |
| 140 | may now contain binary zeros, which are treated as data characters. Without |
Elliott Hughes | 9bc971b | 2018-07-27 13:23:14 -0700 | [diff] [blame] | 141 | REG_PEND, a binary zero terminates the pattern and the \fBre_endp\fP field is |
| 142 | ignored. This is a GNU extension to the POSIX standard and should be used with |
| 143 | caution in software intended to be portable to other systems. |
| 144 | .sp |
Nick Kralevich | f73ff17 | 2014-09-27 12:41:49 -0700 | [diff] [blame] | 145 | REG_UCP |
| 146 | .sp |
Janis Danisevskis | 53e448c | 2016-03-31 13:35:25 +0100 | [diff] [blame] | 147 | The PCRE2_UCP option is set when the regular expression is passed for |
| 148 | compilation to the native function. This causes PCRE2 to use Unicode properties |
Nick Kralevich | f73ff17 | 2014-09-27 12:41:49 -0700 | [diff] [blame] | 149 | when matchine \ed, \ew, etc., instead of just recognizing ASCII values. Note |
Janis Danisevskis | 53e448c | 2016-03-31 13:35:25 +0100 | [diff] [blame] | 150 | that REG_UCP is not part of the POSIX standard. |
Nick Kralevich | f73ff17 | 2014-09-27 12:41:49 -0700 | [diff] [blame] | 151 | .sp |
| 152 | REG_UNGREEDY |
| 153 | .sp |
Janis Danisevskis | 53e448c | 2016-03-31 13:35:25 +0100 | [diff] [blame] | 154 | The PCRE2_UNGREEDY option is set when the regular expression is passed for |
Nick Kralevich | f73ff17 | 2014-09-27 12:41:49 -0700 | [diff] [blame] | 155 | compilation to the native function. Note that REG_UNGREEDY is not part of the |
| 156 | POSIX standard. |
| 157 | .sp |
Janis Danisevskis | 53e448c | 2016-03-31 13:35:25 +0100 | [diff] [blame] | 158 | REG_UTF |
Nick Kralevich | f73ff17 | 2014-09-27 12:41:49 -0700 | [diff] [blame] | 159 | .sp |
Janis Danisevskis | 53e448c | 2016-03-31 13:35:25 +0100 | [diff] [blame] | 160 | The PCRE2_UTF option is set when the regular expression is passed for |
Nick Kralevich | f73ff17 | 2014-09-27 12:41:49 -0700 | [diff] [blame] | 161 | compilation to the native function. This causes the pattern itself and all data |
Janis Danisevskis | 53e448c | 2016-03-31 13:35:25 +0100 | [diff] [blame] | 162 | strings used for matching it to be treated as UTF-8 strings. Note that REG_UTF |
Nick Kralevich | f73ff17 | 2014-09-27 12:41:49 -0700 | [diff] [blame] | 163 | is not part of the POSIX standard. |
| 164 | .P |
| 165 | In the absence of these flags, no options are passed to the native function. |
Janis Danisevskis | 53e448c | 2016-03-31 13:35:25 +0100 | [diff] [blame] | 166 | This means the the regex is compiled with PCRE2 default semantics. In |
Nick Kralevich | f73ff17 | 2014-09-27 12:41:49 -0700 | [diff] [blame] | 167 | particular, the way it handles newline characters in the subject string is the |
Janis Danisevskis | 53e448c | 2016-03-31 13:35:25 +0100 | [diff] [blame] | 168 | Perl way, not the POSIX way. Note that setting PCRE2_MULTILINE has only |
Nick Kralevich | f73ff17 | 2014-09-27 12:41:49 -0700 | [diff] [blame] | 169 | \fIsome\fP of the effects specified for REG_NEWLINE. It does not affect the way |
Janis Danisevskis | 53e448c | 2016-03-31 13:35:25 +0100 | [diff] [blame] | 170 | newlines are matched by the dot metacharacter (they are not) or by a negative |
| 171 | class such as [^a] (they are). |
Nick Kralevich | f73ff17 | 2014-09-27 12:41:49 -0700 | [diff] [blame] | 172 | .P |
Elliott Hughes | 0c26e19 | 2019-08-07 12:24:46 -0700 | [diff] [blame] | 173 | The yield of \fBpcre2_regcomp()\fP is zero on success, and non-zero otherwise. |
| 174 | The \fIpreg\fP structure is filled in on success, and one other member of the |
Elliott Hughes | 9bc971b | 2018-07-27 13:23:14 -0700 | [diff] [blame] | 175 | structure (as well as \fIre_endp\fP) is public: \fIre_nsub\fP contains the |
| 176 | number of capturing subpatterns in the regular expression. Various error codes |
| 177 | are defined in the header file. |
Nick Kralevich | f73ff17 | 2014-09-27 12:41:49 -0700 | [diff] [blame] | 178 | .P |
Elliott Hughes | 0c26e19 | 2019-08-07 12:24:46 -0700 | [diff] [blame] | 179 | NOTE: If the yield of \fBpcre2_regcomp()\fP is non-zero, you must not attempt |
| 180 | to use the contents of the \fIpreg\fP structure. If, for example, you pass it |
| 181 | to \fBpcre2_regexec()\fP, the result is undefined and your program is likely to |
| 182 | crash. |
Nick Kralevich | f73ff17 | 2014-09-27 12:41:49 -0700 | [diff] [blame] | 183 | . |
| 184 | . |
| 185 | .SH "MATCHING NEWLINE CHARACTERS" |
| 186 | .rs |
| 187 | .sp |
| 188 | This area is not simple, because POSIX and Perl take different views of things. |
Janis Danisevskis | 53e448c | 2016-03-31 13:35:25 +0100 | [diff] [blame] | 189 | It is not possible to get PCRE2 to obey POSIX semantics, but then PCRE2 was |
| 190 | never intended to be a POSIX engine. The following table lists the different |
| 191 | possibilities for matching newline characters in Perl and PCRE2: |
Nick Kralevich | f73ff17 | 2014-09-27 12:41:49 -0700 | [diff] [blame] | 192 | .sp |
| 193 | Default Change with |
| 194 | .sp |
Janis Danisevskis | 53e448c | 2016-03-31 13:35:25 +0100 | [diff] [blame] | 195 | . matches newline no PCRE2_DOTALL |
Nick Kralevich | f73ff17 | 2014-09-27 12:41:49 -0700 | [diff] [blame] | 196 | newline matches [^a] yes not changeable |
Janis Danisevskis | 53e448c | 2016-03-31 13:35:25 +0100 | [diff] [blame] | 197 | $ matches \en at end yes PCRE2_DOLLAR_ENDONLY |
| 198 | $ matches \en in middle no PCRE2_MULTILINE |
| 199 | ^ matches \en in middle no PCRE2_MULTILINE |
Nick Kralevich | f73ff17 | 2014-09-27 12:41:49 -0700 | [diff] [blame] | 200 | .sp |
Janis Danisevskis | 53e448c | 2016-03-31 13:35:25 +0100 | [diff] [blame] | 201 | This is the equivalent table for a POSIX-compatible pattern matcher: |
Nick Kralevich | f73ff17 | 2014-09-27 12:41:49 -0700 | [diff] [blame] | 202 | .sp |
| 203 | Default Change with |
| 204 | .sp |
| 205 | . matches newline yes REG_NEWLINE |
| 206 | newline matches [^a] yes REG_NEWLINE |
| 207 | $ matches \en at end no REG_NEWLINE |
| 208 | $ matches \en in middle no REG_NEWLINE |
| 209 | ^ matches \en in middle no REG_NEWLINE |
| 210 | .sp |
Janis Danisevskis | 53e448c | 2016-03-31 13:35:25 +0100 | [diff] [blame] | 211 | This behaviour is not what happens when PCRE2 is called via its POSIX |
| 212 | API. By default, PCRE2's behaviour is the same as Perl's, except that there is |
| 213 | no equivalent for PCRE2_DOLLAR_ENDONLY in Perl. In both PCRE2 and Perl, there |
| 214 | is no way to stop newline from matching [^a]. |
Nick Kralevich | f73ff17 | 2014-09-27 12:41:49 -0700 | [diff] [blame] | 215 | .P |
Janis Danisevskis | 53e448c | 2016-03-31 13:35:25 +0100 | [diff] [blame] | 216 | Default POSIX newline handling can be obtained by setting PCRE2_DOTALL and |
| 217 | PCRE2_DOLLAR_ENDONLY when calling \fBpcre2_compile()\fP directly, but there is |
| 218 | no way to make PCRE2 behave exactly as for the REG_NEWLINE action. When using |
Elliott Hughes | 0c26e19 | 2019-08-07 12:24:46 -0700 | [diff] [blame] | 219 | the POSIX API, passing REG_NEWLINE to PCRE2's \fBpcre2_regcomp()\fP function |
Janis Danisevskis | 53e448c | 2016-03-31 13:35:25 +0100 | [diff] [blame] | 220 | causes PCRE2_MULTILINE to be passed to \fBpcre2_compile()\fP, and REG_DOTALL |
| 221 | passes PCRE2_DOTALL. There is no way to pass PCRE2_DOLLAR_ENDONLY. |
Nick Kralevich | f73ff17 | 2014-09-27 12:41:49 -0700 | [diff] [blame] | 222 | . |
| 223 | . |
| 224 | .SH "MATCHING A PATTERN" |
| 225 | .rs |
| 226 | .sp |
Elliott Hughes | 0c26e19 | 2019-08-07 12:24:46 -0700 | [diff] [blame] | 227 | The function \fBpcre2_regexec()\fP is called to match a compiled pattern |
| 228 | \fIpreg\fP against a given \fIstring\fP, which is by default terminated by a |
| 229 | zero byte (but see REG_STARTEND below), subject to the options in \fIeflags\fP. |
| 230 | These can be: |
Nick Kralevich | f73ff17 | 2014-09-27 12:41:49 -0700 | [diff] [blame] | 231 | .sp |
| 232 | REG_NOTBOL |
| 233 | .sp |
Janis Danisevskis | 53e448c | 2016-03-31 13:35:25 +0100 | [diff] [blame] | 234 | The PCRE2_NOTBOL option is set when calling the underlying PCRE2 matching |
Nick Kralevich | f73ff17 | 2014-09-27 12:41:49 -0700 | [diff] [blame] | 235 | function. |
| 236 | .sp |
| 237 | REG_NOTEMPTY |
| 238 | .sp |
Janis Danisevskis | 53e448c | 2016-03-31 13:35:25 +0100 | [diff] [blame] | 239 | The PCRE2_NOTEMPTY option is set when calling the underlying PCRE2 matching |
Nick Kralevich | f73ff17 | 2014-09-27 12:41:49 -0700 | [diff] [blame] | 240 | function. Note that REG_NOTEMPTY is not part of the POSIX standard. However, |
| 241 | setting this option can give more POSIX-like behaviour in some situations. |
| 242 | .sp |
| 243 | REG_NOTEOL |
| 244 | .sp |
Janis Danisevskis | 53e448c | 2016-03-31 13:35:25 +0100 | [diff] [blame] | 245 | The PCRE2_NOTEOL option is set when calling the underlying PCRE2 matching |
Nick Kralevich | f73ff17 | 2014-09-27 12:41:49 -0700 | [diff] [blame] | 246 | function. |
| 247 | .sp |
| 248 | REG_STARTEND |
| 249 | .sp |
Elliott Hughes | 653c210 | 2019-01-09 15:41:36 -0800 | [diff] [blame] | 250 | When this option is set, the subject string starts at \fIstring\fP + |
Elliott Hughes | 9bc971b | 2018-07-27 13:23:14 -0700 | [diff] [blame] | 251 | \fIpmatch[0].rm_so\fP and ends at \fIstring\fP + \fIpmatch[0].rm_eo\fP, which |
| 252 | should point to the first character beyond the string. There may be binary |
Elliott Hughes | 653c210 | 2019-01-09 15:41:36 -0800 | [diff] [blame] | 253 | zeros within the subject string, and indeed, using REG_STARTEND is the only |
Elliott Hughes | 9bc971b | 2018-07-27 13:23:14 -0700 | [diff] [blame] | 254 | way to pass a subject string that contains a binary zero. |
| 255 | .P |
| 256 | Whatever the value of \fIpmatch[0].rm_so\fP, the offsets of the matched string |
| 257 | and any captured substrings are still given relative to the start of |
| 258 | \fIstring\fP itself. (Before PCRE2 release 10.30 these were given relative to |
| 259 | \fIstring\fP + \fIpmatch[0].rm_so\fP, but this differs from other |
| 260 | implementations.) |
| 261 | .P |
| 262 | This is a BSD extension, compatible with but not specified by IEEE Standard |
| 263 | 1003.2 (POSIX.2), and should be used with caution in software intended to be |
| 264 | portable to other systems. Note that a non-zero \fIrm_so\fP does not imply |
| 265 | REG_NOTBOL; REG_STARTEND affects only the location and length of the string, |
| 266 | not how it is matched. Setting REG_STARTEND and passing \fIpmatch\fP as NULL |
| 267 | are mutually exclusive; the error REG_INVARG is returned. |
Nick Kralevich | f73ff17 | 2014-09-27 12:41:49 -0700 | [diff] [blame] | 268 | .P |
| 269 | If the pattern was compiled with the REG_NOSUB flag, no data about any matched |
| 270 | strings is returned. The \fInmatch\fP and \fIpmatch\fP arguments of |
Elliott Hughes | 0c26e19 | 2019-08-07 12:24:46 -0700 | [diff] [blame] | 271 | \fBpcre2_regexec()\fP are ignored (except possibly as input for REG_STARTEND). |
Nick Kralevich | f73ff17 | 2014-09-27 12:41:49 -0700 | [diff] [blame] | 272 | .P |
Janis Danisevskis | 8b979b2 | 2016-08-15 16:09:16 +0100 | [diff] [blame] | 273 | The value of \fInmatch\fP may be zero, and the value \fIpmatch\fP may be NULL |
| 274 | (unless REG_STARTEND is set); in both these cases no data about any matched |
| 275 | strings is returned. |
Nick Kralevich | f73ff17 | 2014-09-27 12:41:49 -0700 | [diff] [blame] | 276 | .P |
Janis Danisevskis | 8b979b2 | 2016-08-15 16:09:16 +0100 | [diff] [blame] | 277 | Otherwise, the portion of the string that was matched, and also any captured |
Nick Kralevich | f73ff17 | 2014-09-27 12:41:49 -0700 | [diff] [blame] | 278 | substrings, are returned via the \fIpmatch\fP argument, which points to an |
| 279 | array of \fInmatch\fP structures of type \fIregmatch_t\fP, containing the |
Janis Danisevskis | 53e448c | 2016-03-31 13:35:25 +0100 | [diff] [blame] | 280 | members \fIrm_so\fP and \fIrm_eo\fP. These contain the byte offset to the first |
Nick Kralevich | f73ff17 | 2014-09-27 12:41:49 -0700 | [diff] [blame] | 281 | character of each substring and the offset to the first character after the end |
| 282 | of each substring, respectively. The 0th element of the vector relates to the |
| 283 | entire portion of \fIstring\fP that was matched; subsequent elements relate to |
| 284 | the capturing subpatterns of the regular expression. Unused entries in the |
| 285 | array have both structure members set to -1. |
| 286 | .P |
| 287 | A successful match yields a zero return; various error codes are defined in the |
| 288 | header file, of which REG_NOMATCH is the "expected" failure code. |
| 289 | . |
| 290 | . |
| 291 | .SH "ERROR MESSAGES" |
| 292 | .rs |
| 293 | .sp |
Elliott Hughes | 0c26e19 | 2019-08-07 12:24:46 -0700 | [diff] [blame] | 294 | The \fBpcre2_regerror()\fP function maps a non-zero errorcode from either |
| 295 | \fBpcre2_regcomp()\fP or \fBpcre2_regexec()\fP to a printable message. If |
| 296 | \fIpreg\fP is not NULL, the error should have arisen from the use of that |
| 297 | structure. A message terminated by a binary zero is placed in \fIerrbuf\fP. If |
| 298 | the buffer is too short, only the first \fIerrbuf_size\fP - 1 characters of the |
| 299 | error message are used. The yield of the function is the size of buffer needed |
| 300 | to hold the whole message, including the terminating zero. This value is |
| 301 | greater than \fIerrbuf_size\fP if the message was truncated. |
Nick Kralevich | f73ff17 | 2014-09-27 12:41:49 -0700 | [diff] [blame] | 302 | . |
| 303 | . |
| 304 | .SH MEMORY USAGE |
| 305 | .rs |
| 306 | .sp |
| 307 | Compiling a regular expression causes memory to be allocated and associated |
Elliott Hughes | 0c26e19 | 2019-08-07 12:24:46 -0700 | [diff] [blame] | 308 | with the \fIpreg\fP structure. The function \fBpcre2_regfree()\fP frees all |
| 309 | such memory, after which \fIpreg\fP may no longer be used as a compiled |
| 310 | expression. |
Nick Kralevich | f73ff17 | 2014-09-27 12:41:49 -0700 | [diff] [blame] | 311 | . |
| 312 | . |
| 313 | .SH AUTHOR |
| 314 | .rs |
| 315 | .sp |
| 316 | .nf |
| 317 | Philip Hazel |
| 318 | University Computing Service |
Janis Danisevskis | 53e448c | 2016-03-31 13:35:25 +0100 | [diff] [blame] | 319 | Cambridge, England. |
Nick Kralevich | f73ff17 | 2014-09-27 12:41:49 -0700 | [diff] [blame] | 320 | .fi |
| 321 | . |
| 322 | . |
| 323 | .SH REVISION |
| 324 | .rs |
| 325 | .sp |
| 326 | .nf |
Elliott Hughes | 378b175 | 2021-06-08 13:42:40 -0700 | [diff] [blame] | 327 | Last updated: 26 April 2021 |
| 328 | Copyright (c) 1997-2021 University of Cambridge. |
Nick Kralevich | f73ff17 | 2014-09-27 12:41:49 -0700 | [diff] [blame] | 329 | .fi |