tools/pcre/doc/pcrecpp.3
author Nicholas Hastings <skamonkey@gmail.com>
Sun Jul 15 12:31:06 2012 -0400 (2012-07-15)
changeset 3579 6d730190a3e4
parent 2762 d2d467bf1b5a
child 3831 babd0eee3c5e
permissions -rw-r--r--
Added missing files to each core and logic makefiles (NPOTB).
dvander@2762
     1
.TH PCRECPP 3
dvander@2762
     2
.SH NAME
dvander@2762
     3
PCRE - Perl-compatible regular expressions.
dvander@2762
     4
.SH "SYNOPSIS OF C++ WRAPPER"
dvander@2762
     5
.rs
dvander@2762
     6
.sp
dvander@2762
     7
.B #include <pcrecpp.h>
dvander@2762
     8
.
dvander@2762
     9
.SH DESCRIPTION
dvander@2762
    10
.rs
dvander@2762
    11
.sp
dvander@2762
    12
The C++ wrapper for PCRE was provided by Google Inc. Some additional
dvander@2762
    13
functionality was added by Giuseppe Maxia. This brief man page was constructed
dvander@2762
    14
from the notes in the \fIpcrecpp.h\fP file, which should be consulted for
dvander@2762
    15
further details.
dvander@2762
    16
.
dvander@2762
    17
.
dvander@2762
    18
.SH "MATCHING INTERFACE"
dvander@2762
    19
.rs
dvander@2762
    20
.sp
dvander@2762
    21
The "FullMatch" operation checks that supplied text matches a supplied pattern
dvander@2762
    22
exactly. If pointer arguments are supplied, it copies matched sub-strings that
dvander@2762
    23
match sub-patterns into them.
dvander@2762
    24
.sp
dvander@2762
    25
  Example: successful match
dvander@2762
    26
     pcrecpp::RE re("h.*o");
dvander@2762
    27
     re.FullMatch("hello");
dvander@2762
    28
.sp
dvander@2762
    29
  Example: unsuccessful match (requires full match):
dvander@2762
    30
     pcrecpp::RE re("e");
dvander@2762
    31
     !re.FullMatch("hello");
dvander@2762
    32
.sp
dvander@2762
    33
  Example: creating a temporary RE object:
dvander@2762
    34
     pcrecpp::RE("h.*o").FullMatch("hello");
dvander@2762
    35
.sp
dvander@2762
    36
You can pass in a "const char*" or a "string" for "text". The examples below
dvander@2762
    37
tend to use a const char*. You can, as in the different examples above, store
dvander@2762
    38
the RE object explicitly in a variable or use a temporary RE object. The
dvander@2762
    39
examples below use one mode or the other arbitrarily. Either could correctly be
dvander@2762
    40
used for any of these examples.
dvander@2762
    41
.P
dvander@2762
    42
You must supply extra pointer arguments to extract matched subpieces.
dvander@2762
    43
.sp
dvander@2762
    44
  Example: extracts "ruby" into "s" and 1234 into "i"
dvander@2762
    45
     int i;
dvander@2762
    46
     string s;
dvander@2762
    47
     pcrecpp::RE re("(\e\ew+):(\e\ed+)");
dvander@2762
    48
     re.FullMatch("ruby:1234", &s, &i);
dvander@2762
    49
.sp
dvander@2762
    50
  Example: does not try to extract any extra sub-patterns
dvander@2762
    51
     re.FullMatch("ruby:1234", &s);
dvander@2762
    52
.sp
dvander@2762
    53
  Example: does not try to extract into NULL
dvander@2762
    54
     re.FullMatch("ruby:1234", NULL, &i);
dvander@2762
    55
.sp
dvander@2762
    56
  Example: integer overflow causes failure
dvander@2762
    57
     !re.FullMatch("ruby:1234567891234", NULL, &i);
dvander@2762
    58
.sp
dvander@2762
    59
  Example: fails because there aren't enough sub-patterns:
dvander@2762
    60
     !pcrecpp::RE("\e\ew+:\e\ed+").FullMatch("ruby:1234", &s);
dvander@2762
    61
.sp
dvander@2762
    62
  Example: fails because string cannot be stored in integer
dvander@2762
    63
     !pcrecpp::RE("(.*)").FullMatch("ruby", &i);
dvander@2762
    64
.sp
dvander@2762
    65
The provided pointer arguments can be pointers to any scalar numeric
dvander@2762
    66
type, or one of:
dvander@2762
    67
.sp
dvander@2762
    68
   string        (matched piece is copied to string)
dvander@2762
    69
   StringPiece   (StringPiece is mutated to point to matched piece)
dvander@2762
    70
   T             (where "bool T::ParseFrom(const char*, int)" exists)
dvander@2762
    71
   NULL          (the corresponding matched sub-pattern is not copied)
dvander@2762
    72
.sp
dvander@2762
    73
The function returns true iff all of the following conditions are satisfied:
dvander@2762
    74
.sp
dvander@2762
    75
  a. "text" matches "pattern" exactly;
dvander@2762
    76
.sp
dvander@2762
    77
  b. The number of matched sub-patterns is >= number of supplied
dvander@2762
    78
     pointers;
dvander@2762
    79
.sp
dvander@2762
    80
  c. The "i"th argument has a suitable type for holding the
dvander@2762
    81
     string captured as the "i"th sub-pattern. If you pass in
dvander@2762
    82
     void * NULL for the "i"th argument, or a non-void * NULL
dvander@2762
    83
     of the correct type, or pass fewer arguments than the
dvander@2762
    84
     number of sub-patterns, "i"th captured sub-pattern is
dvander@2762
    85
     ignored.
dvander@2762
    86
.sp
dvander@2762
    87
CAVEAT: An optional sub-pattern that does not exist in the matched
dvander@2762
    88
string is assigned the empty string. Therefore, the following will
dvander@2762
    89
return false (because the empty string is not a valid number):
dvander@2762
    90
.sp
dvander@2762
    91
   int number;
dvander@2762
    92
   pcrecpp::RE::FullMatch("abc", "[a-z]+(\e\ed+)?", &number);
dvander@2762
    93
.sp
dvander@2762
    94
The matching interface supports at most 16 arguments per call.
dvander@2762
    95
If you need more, consider using the more general interface
dvander@2762
    96
\fBpcrecpp::RE::DoMatch\fP. See \fBpcrecpp.h\fP for the signature for
dvander@2762
    97
\fBDoMatch\fP.
dvander@2762
    98
.P
dvander@2762
    99
NOTE: Do not use \fBno_arg\fP, which is used internally to mark the end of a
dvander@2762
   100
list of optional arguments, as a placeholder for missing arguments, as this can
dvander@2762
   101
lead to segfaults.
dvander@2762
   102
.
dvander@2762
   103
.
dvander@2762
   104
.SH "QUOTING METACHARACTERS"
dvander@2762
   105
.rs
dvander@2762
   106
.sp
dvander@2762
   107
You can use the "QuoteMeta" operation to insert backslashes before all
dvander@2762
   108
potentially meaningful characters in a string. The returned string, used as a
dvander@2762
   109
regular expression, will exactly match the original string.
dvander@2762
   110
.sp
dvander@2762
   111
  Example:
dvander@2762
   112
     string quoted = RE::QuoteMeta(unquoted);
dvander@2762
   113
.sp
dvander@2762
   114
Note that it's legal to escape a character even if it has no special meaning in
dvander@2762
   115
a regular expression -- so this function does that. (This also makes it
dvander@2762
   116
identical to the perl function of the same name; see "perldoc -f quotemeta".)
dvander@2762
   117
For example, "1.5-2.0?" becomes "1\e.5\e-2\e.0\e?".
dvander@2762
   118
.
dvander@2762
   119
.SH "PARTIAL MATCHES"
dvander@2762
   120
.rs
dvander@2762
   121
.sp
dvander@2762
   122
You can use the "PartialMatch" operation when you want the pattern
dvander@2762
   123
to match any substring of the text.
dvander@2762
   124
.sp
dvander@2762
   125
  Example: simple search for a string:
dvander@2762
   126
     pcrecpp::RE("ell").PartialMatch("hello");
dvander@2762
   127
.sp
dvander@2762
   128
  Example: find first number in a string:
dvander@2762
   129
     int number;
dvander@2762
   130
     pcrecpp::RE re("(\e\ed+)");
dvander@2762
   131
     re.PartialMatch("x*100 + 20", &number);
dvander@2762
   132
     assert(number == 100);
dvander@2762
   133
.
dvander@2762
   134
.
dvander@2762
   135
.SH "UTF-8 AND THE MATCHING INTERFACE"
dvander@2762
   136
.rs
dvander@2762
   137
.sp
dvander@2762
   138
By default, pattern and text are plain text, one byte per character. The UTF8
dvander@2762
   139
flag, passed to the constructor, causes both pattern and string to be treated
dvander@2762
   140
as UTF-8 text, still a byte stream but potentially multiple bytes per
dvander@2762
   141
character. In practice, the text is likelier to be UTF-8 than the pattern, but
dvander@2762
   142
the match returned may depend on the UTF8 flag, so always use it when matching
dvander@2762
   143
UTF8 text. For example, "." will match one byte normally but with UTF8 set may
dvander@2762
   144
match up to three bytes of a multi-byte character.
dvander@2762
   145
.sp
dvander@2762
   146
  Example:
dvander@2762
   147
     pcrecpp::RE_Options options;
dvander@2762
   148
     options.set_utf8();
dvander@2762
   149
     pcrecpp::RE re(utf8_pattern, options);
dvander@2762
   150
     re.FullMatch(utf8_string);
dvander@2762
   151
.sp
dvander@2762
   152
  Example: using the convenience function UTF8():
dvander@2762
   153
     pcrecpp::RE re(utf8_pattern, pcrecpp::UTF8());
dvander@2762
   154
     re.FullMatch(utf8_string);
dvander@2762
   155
.sp
dvander@2762
   156
NOTE: The UTF8 flag is ignored if pcre was not configured with the
dvander@2762
   157
      --enable-utf8 flag.
dvander@2762
   158
.
dvander@2762
   159
.
dvander@2762
   160
.SH "PASSING MODIFIERS TO THE REGULAR EXPRESSION ENGINE"
dvander@2762
   161
.rs
dvander@2762
   162
.sp
dvander@2762
   163
PCRE defines some modifiers to change the behavior of the regular expression
dvander@2762
   164
engine. The C++ wrapper defines an auxiliary class, RE_Options, as a vehicle to
dvander@2762
   165
pass such modifiers to a RE class. Currently, the following modifiers are
dvander@2762
   166
supported:
dvander@2762
   167
.sp
dvander@2762
   168
   modifier              description               Perl corresponding
dvander@2762
   169
.sp
dvander@2762
   170
   PCRE_CASELESS         case insensitive match      /i
dvander@2762
   171
   PCRE_MULTILINE        multiple lines match        /m
dvander@2762
   172
   PCRE_DOTALL           dot matches newlines        /s
dvander@2762
   173
   PCRE_DOLLAR_ENDONLY   $ matches only at end       N/A
dvander@2762
   174
   PCRE_EXTRA            strict escape parsing       N/A
dvander@2762
   175
   PCRE_EXTENDED         ignore whitespaces          /x
dvander@2762
   176
   PCRE_UTF8             handles UTF8 chars          built-in
dvander@2762
   177
   PCRE_UNGREEDY         reverses * and *?           N/A
dvander@2762
   178
   PCRE_NO_AUTO_CAPTURE  disables capturing parens   N/A (*)
dvander@2762
   179
.sp
dvander@2762
   180
(*) Both Perl and PCRE allow non capturing parentheses by means of the
dvander@2762
   181
"?:" modifier within the pattern itself. e.g. (?:ab|cd) does not
dvander@2762
   182
capture, while (ab|cd) does.
dvander@2762
   183
.P
dvander@2762
   184
For a full account on how each modifier works, please check the
dvander@2762
   185
PCRE API reference page.
dvander@2762
   186
.P
dvander@2762
   187
For each modifier, there are two member functions whose name is made
dvander@2762
   188
out of the modifier in lowercase, without the "PCRE_" prefix. For
dvander@2762
   189
instance, PCRE_CASELESS is handled by
dvander@2762
   190
.sp
dvander@2762
   191
  bool caseless()
dvander@2762
   192
.sp
dvander@2762
   193
which returns true if the modifier is set, and
dvander@2762
   194
.sp
dvander@2762
   195
  RE_Options & set_caseless(bool)
dvander@2762
   196
.sp
dvander@2762
   197
which sets or unsets the modifier. Moreover, PCRE_EXTRA_MATCH_LIMIT can be
dvander@2762
   198
accessed through the \fBset_match_limit()\fR and \fBmatch_limit()\fR member
dvander@2762
   199
functions. Setting \fImatch_limit\fR to a non-zero value will limit the
dvander@2762
   200
execution of pcre to keep it from doing bad things like blowing the stack or
dvander@2762
   201
taking an eternity to return a result. A value of 5000 is good enough to stop
dvander@2762
   202
stack blowup in a 2MB thread stack. Setting \fImatch_limit\fR to zero disables
dvander@2762
   203
match limiting. Alternatively, you can call \fBmatch_limit_recursion()\fP
dvander@2762
   204
which uses PCRE_EXTRA_MATCH_LIMIT_RECURSION to limit how much PCRE
dvander@2762
   205
recurses. \fBmatch_limit()\fP limits the number of matches PCRE does;
dvander@2762
   206
\fBmatch_limit_recursion()\fP limits the depth of internal recursion, and
dvander@2762
   207
therefore the amount of stack that is used.
dvander@2762
   208
.P
dvander@2762
   209
Normally, to pass one or more modifiers to a RE class, you declare
dvander@2762
   210
a \fIRE_Options\fR object, set the appropriate options, and pass this
dvander@2762
   211
object to a RE constructor. Example:
dvander@2762
   212
.sp
dvander@2762
   213
   RE_options opt;
dvander@2762
   214
   opt.set_caseless(true);
dvander@2762
   215
   if (RE("HELLO", opt).PartialMatch("hello world")) ...
dvander@2762
   216
.sp
dvander@2762
   217
RE_options has two constructors. The default constructor takes no arguments and
dvander@2762
   218
creates a set of flags that are off by default. The optional parameter
dvander@2762
   219
\fIoption_flags\fR is to facilitate transfer of legacy code from C programs.
dvander@2762
   220
This lets you do
dvander@2762
   221
.sp
dvander@2762
   222
   RE(pattern,
dvander@2762
   223
     RE_Options(PCRE_CASELESS|PCRE_MULTILINE)).PartialMatch(str);
dvander@2762
   224
.sp
dvander@2762
   225
However, new code is better off doing
dvander@2762
   226
.sp
dvander@2762
   227
   RE(pattern,
dvander@2762
   228
     RE_Options().set_caseless(true).set_multiline(true))
dvander@2762
   229
       .PartialMatch(str);
dvander@2762
   230
.sp
dvander@2762
   231
If you are going to pass one of the most used modifiers, there are some
dvander@2762
   232
convenience functions that return a RE_Options class with the
dvander@2762
   233
appropriate modifier already set: \fBCASELESS()\fR, \fBUTF8()\fR,
dvander@2762
   234
\fBMULTILINE()\fR, \fBDOTALL\fR(), and \fBEXTENDED()\fR.
dvander@2762
   235
.P
dvander@2762
   236
If you need to set several options at once, and you don't want to go through
dvander@2762
   237
the pains of declaring a RE_Options object and setting several options, there
dvander@2762
   238
is a parallel method that give you such ability on the fly. You can concatenate
dvander@2762
   239
several \fBset_xxxxx()\fR member functions, since each of them returns a
dvander@2762
   240
reference to its class object. For example, to pass PCRE_CASELESS,
dvander@2762
   241
PCRE_EXTENDED, and PCRE_MULTILINE to a RE with one statement, you may write:
dvander@2762
   242
.sp
dvander@2762
   243
   RE(" ^ xyz \e\es+ .* blah$",
dvander@2762
   244
     RE_Options()
dvander@2762
   245
       .set_caseless(true)
dvander@2762
   246
       .set_extended(true)
dvander@2762
   247
       .set_multiline(true)).PartialMatch(sometext);
dvander@2762
   248
.sp
dvander@2762
   249
.
dvander@2762
   250
.
dvander@2762
   251
.SH "SCANNING TEXT INCREMENTALLY"
dvander@2762
   252
.rs
dvander@2762
   253
.sp
dvander@2762
   254
The "Consume" operation may be useful if you want to repeatedly
dvander@2762
   255
match regular expressions at the front of a string and skip over
dvander@2762
   256
them as they match. This requires use of the "StringPiece" type,
dvander@2762
   257
which represents a sub-range of a real string. Like RE, StringPiece
dvander@2762
   258
is defined in the pcrecpp namespace.
dvander@2762
   259
.sp
dvander@2762
   260
  Example: read lines of the form "var = value" from a string.
dvander@2762
   261
     string contents = ...;                 // Fill string somehow
dvander@2762
   262
     pcrecpp::StringPiece input(contents);  // Wrap in a StringPiece
dvander@2762
   263
dvander@2762
   264
     string var;
dvander@2762
   265
     int value;
dvander@2762
   266
     pcrecpp::RE re("(\e\ew+) = (\e\ed+)\en");
dvander@2762
   267
     while (re.Consume(&input, &var, &value)) {
dvander@2762
   268
       ...;
dvander@2762
   269
     }
dvander@2762
   270
.sp
dvander@2762
   271
Each successful call to "Consume" will set "var/value", and also
dvander@2762
   272
advance "input" so it points past the matched text.
dvander@2762
   273
.P
dvander@2762
   274
The "FindAndConsume" operation is similar to "Consume" but does not
dvander@2762
   275
anchor your match at the beginning of the string. For example, you
dvander@2762
   276
could extract all words from a string by repeatedly calling
dvander@2762
   277
.sp
dvander@2762
   278
  pcrecpp::RE("(\e\ew+)").FindAndConsume(&input, &word)
dvander@2762
   279
.
dvander@2762
   280
.
dvander@2762
   281
.SH "PARSING HEX/OCTAL/C-RADIX NUMBERS"
dvander@2762
   282
.rs
dvander@2762
   283
.sp
dvander@2762
   284
By default, if you pass a pointer to a numeric value, the
dvander@2762
   285
corresponding text is interpreted as a base-10 number. You can
dvander@2762
   286
instead wrap the pointer with a call to one of the operators Hex(),
dvander@2762
   287
Octal(), or CRadix() to interpret the text in another base. The
dvander@2762
   288
CRadix operator interprets C-style "0" (base-8) and "0x" (base-16)
dvander@2762
   289
prefixes, but defaults to base-10.
dvander@2762
   290
.sp
dvander@2762
   291
  Example:
dvander@2762
   292
    int a, b, c, d;
dvander@2762
   293
    pcrecpp::RE re("(.*) (.*) (.*) (.*)");
dvander@2762
   294
    re.FullMatch("100 40 0100 0x40",
dvander@2762
   295
                 pcrecpp::Octal(&a), pcrecpp::Hex(&b),
dvander@2762
   296
                 pcrecpp::CRadix(&c), pcrecpp::CRadix(&d));
dvander@2762
   297
.sp
dvander@2762
   298
will leave 64 in a, b, c, and d.
dvander@2762
   299
.
dvander@2762
   300
.
dvander@2762
   301
.SH "REPLACING PARTS OF STRINGS"
dvander@2762
   302
.rs
dvander@2762
   303
.sp
dvander@2762
   304
You can replace the first match of "pattern" in "str" with "rewrite".
dvander@2762
   305
Within "rewrite", backslash-escaped digits (\e1 to \e9) can be
dvander@2762
   306
used to insert text matching corresponding parenthesized group
dvander@2762
   307
from the pattern. \e0 in "rewrite" refers to the entire matching
dvander@2762
   308
text. For example:
dvander@2762
   309
.sp
dvander@2762
   310
  string s = "yabba dabba doo";
dvander@2762
   311
  pcrecpp::RE("b+").Replace("d", &s);
dvander@2762
   312
.sp
dvander@2762
   313
will leave "s" containing "yada dabba doo". The result is true if the pattern
dvander@2762
   314
matches and a replacement occurs, false otherwise.
dvander@2762
   315
.P
dvander@2762
   316
\fBGlobalReplace\fP is like \fBReplace\fP except that it replaces all
dvander@2762
   317
occurrences of the pattern in the string with the rewrite. Replacements are
dvander@2762
   318
not subject to re-matching. For example:
dvander@2762
   319
.sp
dvander@2762
   320
  string s = "yabba dabba doo";
dvander@2762
   321
  pcrecpp::RE("b+").GlobalReplace("d", &s);
dvander@2762
   322
.sp
dvander@2762
   323
will leave "s" containing "yada dada doo". It returns the number of
dvander@2762
   324
replacements made.
dvander@2762
   325
.P
dvander@2762
   326
\fBExtract\fP is like \fBReplace\fP, except that if the pattern matches,
dvander@2762
   327
"rewrite" is copied into "out" (an additional argument) with substitutions.
dvander@2762
   328
The non-matching portions of "text" are ignored. Returns true iff a match
dvander@2762
   329
occurred and the extraction happened successfully;  if no match occurs, the
dvander@2762
   330
string is left unaffected.
dvander@2762
   331
.
dvander@2762
   332
.
dvander@2762
   333
.SH AUTHOR
dvander@2762
   334
.rs
dvander@2762
   335
.sp
dvander@2762
   336
.nf
dvander@2762
   337
The C++ wrapper was contributed by Google Inc.
dvander@2762
   338
Copyright (c) 2007 Google Inc.
dvander@2762
   339
.fi
dvander@2762
   340
.
dvander@2762
   341
.
dvander@2762
   342
.SH REVISION
dvander@2762
   343
.rs
dvander@2762
   344
.sp
dvander@2762
   345
.nf
dvander@2762
   346
Last updated: 17 March 2009
dvander@2762
   347
.fi