-
Notifications
You must be signed in to change notification settings - Fork 1.2k
CplusplusAPI
[walaloo](https://www.youtube.com/watch?v=8EGLJ5876ZE&t=31s)# Syntax
In POSIX mode, RE2 accepts standard POSIX (egrep) syntax regular expressions. In Perl mode, RE2 accepts most Perl operators. The only excluded ones are those that require backtracking (and its potential for exponential runtime) to implement. These include backreferences (submatching is still okay) and generalized assertions. The Syntax page documents the supported Perl-mode syntax in detail. The default is Perl mode.
- Matching Interface
Examples:
assert(RE2::FullMatch("hello", "h.*o"))
assert(!RE2::FullMatch("hello", "e"))
assert(RE2::PartialMatch("hello", "h.*o"))
assert(RE2::PartialMatch("hello", "e"))
- Submatch Extraction
Examples:
// Successful parsing.
int i;
string s;
assert(RE2::FullMatch("ruby:1234", "(\\w+):(\\d+)", &s, &i));
assert(s == "ruby");
assert(i == 1234);
// Fails: "ruby" cannot be parsed as an integer.
assert(!RE2::FullMatch("ruby", "(.+)", &i));
// Success; does not extract the number.
assert(RE2::FullMatch("ruby:1234", "(\\w+):(\\d+)", &s));
// Success; skips NULL argument.
assert(RE2::FullMatch("ruby:1234", "(\\w+):(\\d+)", (void*)NULL, &i));
// Fails: integer overflow keeps value from being stored in i.
assert(!RE2::FullMatch("ruby:123456789123", "(\\w+):(\\d+)", &s, &i));
- Pre-Compiled Regular Expressions
Example:
RE2 re("(\\w+):(\\d+)");
assert(re.ok()); // compiled; if not, see re.error();
assert(RE2::FullMatch("ruby:1234", re, &s, &i));
assert(RE2::FullMatch("ruby:1234", re, &s));
assert(RE2::FullMatch("ruby:1234", re, (void*)NULL, &i));
assert(!RE2::FullMatch("ruby:123456789123", re, &s, &i));
- Options
RE2 re("(ab", RE2::Quiet); // don't write to stderr for parser failure
assert(!re.ok()); // can check re.error() for details
Other useful predefined options are ` Latin1 ` (disable UTF-8) and ` POSIX ` (use POSIX syntax and leftmost longest matching).
You can also declare your own RE2::Options object and then configure it as you like. See the [header](https://github.com/google/re2/blob/master/re2/re2.h) for the full set of options.
- Unicode Normalization
- Additional Tips and Tricks