pcrecpp.html 14 KB

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