You must supply extra pointer arguments to extract matched subpieces. Example: extracts "ruby" into "s" and 1234 into "i" int i; string s; pcrecpp::RE re("(\e\ew+):(\e\ed+)"); re.FullMatch("ruby:1234", &s, &i); Example: does not try to extract any extra sub-patterns re.FullMatch("ruby:1234", &s); Example: does not try to extract into NULL re.FullMatch("ruby:1234", NULL, &i); Example: integer overflow causes failure !re.FullMatch("ruby:1234567891234", NULL, &i); Example: fails because there aren't enough sub-patterns: !pcrecpp::RE("\e\ew+:\e\ed+").FullMatch("ruby:1234", &s); Example: fails because string cannot be stored in integer !pcrecpp::RE("(.*)").FullMatch("ruby", &i); The provided pointer arguments can be pointers to any scalar numeric type, or one of: string (matched piece is copied to string) StringPiece (StringPiece is mutated to point to matched piece) T (where "bool T::ParseFrom(const char*, int)" exists) NULL (the corresponding matched sub-pattern is not copied) The function returns true iff all of the following conditions are satisfied: a. "text" matches "pattern" exactly; b. The number of matched sub-patterns is >= number of supplied pointers; c. The "i"th argument has a suitable type for holding the string captured as the "i"th sub-pattern. If you pass in void * NULL for the "i"th argument, or a non-void * NULL of the correct type, or pass fewer arguments than the number of sub-patterns, "i"th captured sub-pattern is ignored. CAVEAT: An optional sub-pattern that does not exist in the matched string is assigned the empty string. Therefore, the following will return false (because the empty string is not a valid number): int number; pcrecpp::RE::FullMatch("abc", "[a-z]+(\e\ed+)?", &number); The matching interface supports at most 16 arguments per call. If you need more, consider using the more general interface pcrecpp::RE::DoMatch. See pcrecpp.h for the signature for DoMatch.
NOTE: Do not use no_arg, which is used internally to mark the end of a list of optional arguments, as a placeholder for missing arguments, as this can lead to segfaults. . .
For a full account on how each modifier works, please check the PCRE API reference page.
For each modifier, there are two member functions whose name is made out of the modifier in lowercase, without the "PCRE_" prefix. For instance, PCRE_CASELESS is handled by bool caseless() which returns true if the modifier is set, and RE_Options & set_caseless(bool) which sets or unsets the modifier. Moreover, PCRE_EXTRA_MATCH_LIMIT can be accessed through the set_match_limit() and match_limit() member functions. Setting match_limit to a non-zero value will limit the execution of pcre to keep it from doing bad things like blowing the stack or taking an eternity to return a result. A value of 5000 is good enough to stop stack blowup in a 2MB thread stack. Setting match_limit to zero disables match limiting. Alternatively, you can call match_limit_recursion() which uses PCRE_EXTRA_MATCH_LIMIT_RECURSION to limit how much PCRE recurses. match_limit() limits the number of matches PCRE does; match_limit_recursion() limits the depth of internal recursion, and therefore the amount of stack that is used.
Normally, to pass one or more modifiers to a RE class, you declare a RE_Options object, set the appropriate options, and pass this object to a RE constructor. Example: RE_Options opt; opt.set_caseless(true); if (RE("HELLO", opt).PartialMatch("hello world")) ... RE_options has two constructors. The default constructor takes no arguments and creates a set of flags that are off by default. The optional parameter option_flags is to facilitate transfer of legacy code from C programs. This lets you do RE(pattern, RE_Options(PCRE_CASELESS|PCRE_MULTILINE)).PartialMatch(str); However, new code is better off doing RE(pattern, RE_Options().set_caseless(true).set_multiline(true)) .PartialMatch(str); If you are going to pass one of the most used modifiers, there are some convenience functions that return a RE_Options class with the appropriate modifier already set: CASELESS(), UTF8(), MULTILINE(), DOTALL(), and EXTENDED().
If you need to set several options at once, and you don't want to go through the pains of declaring a RE_Options object and setting several options, there is a parallel method that give you such ability on the fly. You can concatenate several set_xxxxx() member functions, since each of them returns a reference to its class object. For example, to pass PCRE_CASELESS, PCRE_EXTENDED, and PCRE_MULTILINE to a RE with one statement, you may write: RE(" ^ xyz \e\es+ .* blah$", RE_Options() .set_caseless(true) .set_extended(true) .set_multiline(true)).PartialMatch(sometext); . .
The "FindAndConsume" operation is similar to "Consume" but does not anchor your match at the beginning of the string. For example, you could extract all words from a string by repeatedly calling pcrecpp::RE("(\e\ew+)").FindAndConsume(&input, &word) . .
.rs By default, if you pass a pointer to a numeric value, the corresponding text is interpreted as a base-10 number. You can instead wrap the pointer with a call to one of the operators Hex(), Octal(), or CRadix() to interpret the text in another base. The CRadix operator interprets C-style "0" (base-8) and "0x" (base-16) prefixes, but defaults to base-10. Example: int a, b, c, d; pcrecpp::RE re("(.*) (.*) (.*) (.*)"); re.FullMatch("100 40 0100 0x40", pcrecpp::Octal(&a), pcrecpp::Hex(&b), pcrecpp::CRadix(&c), pcrecpp::CRadix(&d)); will leave 64 in a, b, c, and d. . .GlobalReplace is like Replace except that it replaces all occurrences of the pattern in the string with the rewrite. Replacements are not subject to re-matching. For example: string s = "yabba dabba doo"; pcrecpp::RE("b+").GlobalReplace("d", &s); will leave "s" containing "yada dada doo". It returns the number of replacements made.
Extract is like Replace, except that if the pattern matches, "rewrite" is copied into "out" (an additional argument) with substitutions. The non-matching portions of "text" are ignored. Returns true iff a match occurred and the extraction happened successfully; if no match occurs, the string is left unaffected. . .
The C++ wrapper was contributed by Google Inc. Copyright (c) 2007 Google Inc.. .
Last updated: 08 January 2012