class RE2

Defined at line 230 of file ../../third_party/re2/src/re2/re2.h

Interface for regular expression matching. Also corresponds to a

pre-compiled regular expression. An "RE2" object is safe for

concurrent use by multiple threads.

Public Methods

template <typename... A>
bool FullMatch (const StringPiece & text, const RE2 & re, A &&... a)

Matches "text" against "re". If pointer arguments are

supplied, copies matched sub-patterns into them.

You can pass in a "const char*" or a "std::string" for "text".

You can pass in a "const char*" or a "std::string" or a "RE2" for "re".

The provided pointer arguments can be pointers to any scalar numeric

type, or one of:

std::string (matched piece is copied to string)

StringPiece (StringPiece is mutated to point to matched piece)

T (where "bool T::ParseFrom(const char*, size_t)" exists)

(void*)NULL (the corresponding matched sub-pattern is not copied)

Returns true iff all of the following conditions are satisfied:

a. "text" matches "re" fully - from the beginning to the end of "text".

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

NULL for the "i"th argument, or pass fewer arguments than

number of sub-patterns, the "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;

RE2::FullMatch("abc", "[a-z]+(\\d+)?",

&number

);

Defined at line 387 of file ../../third_party/re2/src/re2/re2.h

template <typename... A>
bool PartialMatch (const StringPiece & text, const RE2 & re, A &&... a)

Like FullMatch(), except that "re" is allowed to match a substring

of "text".

Returns true iff all of the following conditions are satisfied:

a. "text" matches "re" partially - for some substring of "text".

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

NULL for the "i"th argument, or pass fewer arguments than

number of sub-patterns, the "i"th captured sub-pattern is

ignored.

Defined at line 403 of file ../../third_party/re2/src/re2/re2.h

template <typename... A>
bool Consume (StringPiece * input, const RE2 & re, A &&... a)

Like FullMatch() and PartialMatch(), except that "re" has to match

a prefix of the text, and "input" is advanced past the matched

text. Note: "input" is modified iff this routine returns true

and "re" matched a non-empty substring of "input".

Returns true iff all of the following conditions are satisfied:

a. "input" matches "re" partially - for some prefix of "input".

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

NULL for the "i"th argument, or pass fewer arguments than

number of sub-patterns, the "i"th captured sub-pattern is

ignored.

Defined at line 421 of file ../../third_party/re2/src/re2/re2.h

template <typename... A>
bool FindAndConsume (StringPiece * input, const RE2 & re, A &&... a)

Like Consume(), but does not anchor the match at the beginning of

the text. That is, "re" need not start its match at the beginning

of "input". For example, "FindAndConsume(s, "(

\

w+)", &word)" finds

the next word in "s" and stores it in "word".

Returns true iff all of the following conditions are satisfied:

a. "input" matches "re" partially - for some substring of "input".

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

NULL for the "i"th argument, or pass fewer arguments than

number of sub-patterns, the "i"th captured sub-pattern is

ignored.

Defined at line 439 of file ../../third_party/re2/src/re2/re2.h

int NumberOfCapturingGroups ()

Return the number of capturing subpatterns, or -1 if the

regexp wasn't valid on construction. The overall match ($0)

does not count: if the regexp is "(a)(b)", returns 2.

Defined at line 528 of file ../../third_party/re2/src/re2/re2.h

void RE2 (const char * pattern)
void RE2 (const std::string & pattern)
void RE2 (const StringPiece & pattern)
void RE2 (const StringPiece & pattern, const Options & options)
int ProgramSize ()

Returns the program size, a very approximate measure of a regexp's "cost".

Larger numbers are more expensive than smaller numbers.

int ReverseProgramSize ()
int ProgramFanout (std::vector<int> * histogram)

If histogram is not null, outputs the program fanout

as a histogram bucketed by powers of 2.

Returns the number of the largest non-empty bucket.

int ReverseProgramFanout (std::vector<int> * histogram)
bool FullMatchN (const StringPiece & text, const RE2 & re, const Arg *const[] args, int n)

The functions here have names ending in 'N' and are used to implement

the functions whose names are the prefix before the 'N'. It is sometimes

useful to invoke them directly, but the syntax is awkward, so the 'N'-less

versions should be preferred.

bool PartialMatchN (const StringPiece & text, const RE2 & re, const Arg *const[] args, int n)
bool ConsumeN (StringPiece * input, const RE2 & re, const Arg *const[] args, int n)
bool FindAndConsumeN (StringPiece * input, const RE2 & re, const Arg *const[] args, int n)
bool Replace (std::string * str, const RE2 & re, const StringPiece & rewrite)

Replace the first match of "re" in "str" with "rewrite".

Within "rewrite", backslash-escaped digits (

\

1 to

\

9) can be

used to insert text matching corresponding parenthesized group

from the pattern.

\

0 in "rewrite" refers to the entire matching

text. E.g.,

std::string s = "yabba dabba doo";

CHECK(RE2::Replace(

&s

, "b+", "d"));

will leave "s" containing "yada dabba doo"

Returns true if the pattern matches and a replacement occurs,

false otherwise.

int GlobalReplace (std::string * str, const RE2 & re, const StringPiece & rewrite)

Like Replace(), except replaces successive non-overlapping occurrences

of the pattern in the string with the rewrite. E.g.

std::string s = "yabba dabba doo";

CHECK(RE2::GlobalReplace(

&s

, "b+", "d"));

will leave "s" containing "yada dada doo"

Replacements are not subject to re-matching.

Because GlobalReplace only replaces non-overlapping matches,

replacing "ana" within "banana" makes only one replacement, not two.

Returns the number of replacements made.

bool Extract (const StringPiece & text, const RE2 & re, const StringPiece & rewrite, std::string * out)

Like Replace, except that if the pattern matches, "rewrite"

is copied into "out" 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.

REQUIRES: "text" must not alias any part of "*out".

std::string QuoteMeta (const StringPiece & unquoted)

Escapes all potentially meaningful regexp characters in

'unquoted'. The returned string, used as a regular expression,

will match exactly the original string. For example,

1.5-2.0?

may become:

1

.

5

\

-2

.

0

\

?

bool PossibleMatchRange (std::string * min, std::string * max, int maxlen)

Computes range for any strings matching regexp. The min and max can in

some cases be arbitrarily precise, so the caller gets to specify the

maximum desired length of string returned.

Assuming PossibleMatchRange(

&min

,

&max

, N) returns successfully, any

string s that is an anchored match for this regexp satisfies

min

<

= s

&

&

s

<

= max.

Note that PossibleMatchRange() will only consider the first copy of an

infinitely repeated element (i.e., any regexp element followed by a '*' or

'+' operator). Regexps with "{N}" constructions are not affected, as those

do not compile down to infinite repetitions.

Returns true on success, false on error.

const std::map<std::string, int> & NamedCapturingGroups ()

Return a map from names to capturing indices.

The map records the index of the leftmost group

with the given name.

Only valid until the re is deleted.

const std::map<int, std::string> & CapturingGroupNames ()

Return a map from capturing indices to names.

The map has no entries for unnamed groups.

Only valid until the re is deleted.

bool Match (const StringPiece & text, size_t startpos, size_t endpos, Anchor re_anchor, StringPiece * submatch, int nsubmatch)

General matching routine.

Match against text starting at offset startpos

and stopping the search at offset endpos.

Returns true if match found, false if not.

On a successful match, fills in submatch[] (up to nsubmatch entries)

with information about submatches.

I.e. matching RE2("(foo)|(bar)baz") on "barbazbla" will return true, with

submatch[0] = "barbaz", submatch[1].data() = NULL, submatch[2] = "bar",

submatch[3].data() = NULL, ..., up to submatch[nsubmatch-1].data() = NULL.

Caveat: submatch[] may be clobbered even on match failure.

Don't ask for more match information than you will use:

runs much faster with nsubmatch == 1 than nsubmatch > 1, and

runs even faster if nsubmatch == 0.

Doesn't make sense to use nsubmatch > 1 + NumberOfCapturingGroups(),

but will be handled correctly.

Passing text == StringPiece(NULL, 0) will be handled like any other

empty string, but note that on return, it will not be possible to tell

whether submatch i matched the empty string or did not match:

either way, submatch[i].data() == NULL.

bool CheckRewriteString (const StringPiece & rewrite, std::string * error)

Check that the given rewrite string is suitable for use with this

regular expression. It checks that:

* The regular expression has enough parenthesized subexpressions

to satisfy all of the

tokens in rewrite

* The rewrite string doesn't have any syntax errors. E.g.,

'

\

' followed by anything other than a digit or '

\

'.

A true return value guarantees that Replace() and Extract() won't

fail because of a bad rewrite string.

int MaxSubmatch (const StringPiece & rewrite)

Returns the maximum submatch needed for the rewrite to be done by

Replace(). E.g. if rewrite == "foo \\2,\\1", returns 2.

bool Rewrite (std::string * out, const StringPiece & rewrite, const StringPiece * vec, int veclen)

Append the "rewrite" string, with backslash subsitutions from "vec",

to string "out".

Returns true on success. This method can fail because of a malformed

rewrite string. CheckRewriteString guarantees that the rewrite will

be sucessful.

bool ok ()

Returns whether RE2 was created properly.

Defined at line 286 of file ../../third_party/re2/src/re2/re2.h

const std::string & pattern ()

The string specification for this RE2. E.g.

RE2 re("ab*c?d+");

re.pattern(); // "ab*c?d+"

Defined at line 291 of file ../../third_party/re2/src/re2/re2.h

const std::string & error ()

If RE2 could not be created properly, returns an error string.

Else returns the empty string.

Defined at line 295 of file ../../third_party/re2/src/re2/re2.h

ErrorCode error_code ()

If RE2 could not be created properly, returns an error code.

Else returns RE2::NoError (== 0).

Defined at line 299 of file ../../third_party/re2/src/re2/re2.h

const std::string & error_arg ()

If RE2 could not be created properly, returns the offending

portion of the regexp.

Defined at line 303 of file ../../third_party/re2/src/re2/re2.h

re2::Regexp * Regexp ()

Returns the underlying Regexp; not for general use.

Returns entire_regexp_ so that callers don't need

to know about prefix_ and prefix_foldcase_.

Defined at line 319 of file ../../third_party/re2/src/re2/re2.h

const Options & options ()

Returns the options set in the constructor.

Defined at line 735 of file ../../third_party/re2/src/re2/re2.h

void ~RE2 ()
template <typename T>
Arg CRadix (T * ptr)

Argument converters; see below.

Defined at line 891 of file ../../third_party/re2/src/re2/re2.h

template <typename T>
Arg Hex (T * ptr)

Defined at line 898 of file ../../third_party/re2/src/re2/re2.h

template <typename T>
Arg Octal (T * ptr)

Defined at line 905 of file ../../third_party/re2/src/re2/re2.h

Enumerations

enum ErrorCode
Name Value
NoError 0
ErrorInternal 1
ErrorBadEscape 2
ErrorBadCharClass 3
ErrorBadCharRange 4
ErrorMissingBracket 5
ErrorMissingParen 6
ErrorUnexpectedParen 7
ErrorTrailingBackslash 8
ErrorRepeatArgument 9
ErrorRepeatSize 10
ErrorRepeatOp 11
ErrorBadPerlOp 12
ErrorBadUTF8 13
ErrorBadNamedCapture 14
ErrorPatternTooLarge 15

Defined at line 239 of file ../../third_party/re2/src/re2/re2.h

enum CannedOptions
Name Value
DefaultOptions 0
Latin1 1
POSIX 2
Quiet 3

Predefined common options.

If you need more complicated things, instantiate

an Option class, possibly passing one of these to

the Option constructor, change the settings, and pass that

Option class to the RE2 constructor.

Defined at line 267 of file ../../third_party/re2/src/re2/re2.h

enum Anchor
Name Value
UNANCHORED 0
ANCHOR_START 1
ANCHOR_BOTH 2

Type of match.

Defined at line 519 of file ../../third_party/re2/src/re2/re2.h

Records