class RE2
Defined at line 239 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
void RE2 (const char * pattern)
Need to have the const char* and const std::string
&
forms for implicit
conversions when passing string literals to FullMatch and PartialMatch.
Otherwise the absl::string_view form would be sufficient.
void RE2 (const std::string & pattern)
void RE2 (absl::string_view pattern)
void RE2 (absl::string_view 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.
bool FindAndConsumeN (absl::string_view *input,const RE2 &re,const Arg *const[]args,intn)
void RE2 (const RE2 & )
Not copyable.
RE2 objects are expensive. You should probably use std::shared_ptr
<RE2
>
instead. If you really must copy, RE2(first.pattern(), first.options())
effectively does so: it produces a second object that mimics the first.
Defined at line 296 of file ../../third_party/re2/src/re2/re2.h
template <typename... A>
bool FullMatch (absl::string_viewtext,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)
absl::string_view (string_view is mutated to point to matched piece)
std::optional
<T
> (T is a supported numeric or string type as above)
T ("bool T::ParseFrom(const char*, size_t)" must exist)
(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 null string. Therefore, the
following returns false because the null string - absence of
a string (not even the empty string) - is not a valid number:
int number;
RE2::FullMatch("abc", "[a-z]+(\\d+)?",
&number
);
Use std::optional
<int
> instead to handle this case correctly.
Defined at line 411 of file ../../third_party/re2/src/re2/re2.h
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.
bool FullMatchN (absl::string_viewtext,const RE2 &re,const Arg *const[]args,intn)
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 (absl::string_viewtext,const RE2 &re,const Arg *const[]args,intn)
bool ConsumeN (absl::string_view *input,const RE2 &re,const Arg *const[]args,intn)
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 (absl::string_viewtext,size_tstartpos,size_tendpos,Anchorre_anchor,absl::string_view *submatch,intnsubmatch)
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 == absl::string_view() 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 (absl::string_view 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 (absl::string_view rewrite)
Returns the maximum submatch needed for the rewrite to be done by
Replace(). E.g. if rewrite == "foo \\2,\\1", returns 2.
RE2 & operator= (const RE2 & )
Defined at line 297 of file ../../third_party/re2/src/re2/re2.h
void RE2 (RE2 && )
Not movable.
RE2 objects are thread-safe and logically immutable. You should probably
use std::unique_ptr
<RE2
> instead. Otherwise, consider std::deque
<RE2
> if
direct emplacement into a container is desired. If you really must move,
be prepared to submit a design document along with your feature request.
Defined at line 303 of file ../../third_party/re2/src/re2/re2.h
RE2 & operator= (RE2 && )
Defined at line 304 of file ../../third_party/re2/src/re2/re2.h
bool ok ()
Returns whether RE2 was created properly.
Defined at line 307 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 312 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 316 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 320 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 324 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 340 of file ../../third_party/re2/src/re2/re2.h
template <typename... A>
bool PartialMatch (absl::string_viewtext,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 427 of file ../../third_party/re2/src/re2/re2.h
template <typename... A>
bool Consume (absl::string_view *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 445 of file ../../third_party/re2/src/re2/re2.h
template <typename... A>
bool FindAndConsume (absl::string_view *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 463 of file ../../third_party/re2/src/re2/re2.h
int NumberOfCapturingGroups ()
Return the number of capturing sub-patterns, 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 551 of file ../../third_party/re2/src/re2/re2.h
void ~RE2 ()
int ReverseProgramFanout (std::vector<int> * histogram)
bool Replace (std::string *str,const RE2 &re,absl::string_viewrewrite)
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";
ABSL_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,absl::string_viewrewrite)
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";
ABSL_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 (absl::string_viewtext,const RE2 &re,absl::string_viewrewrite,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 (absl::string_view 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,intmaxlen)
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.
bool Rewrite (std::string *out,absl::string_viewrewrite,const absl::string_view *vec,intveclen)
Append the "rewrite" string, with backslash substitutions 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.
const Options & options ()
Returns the options set in the constructor.
Defined at line 758 of file ../../third_party/re2/src/re2/re2.h
template <typename T>
Arg CRadix (T * ptr)
Argument converters; see below.
Defined at line 950 of file ../../third_party/re2/src/re2/re2.h
template <typename T>
Arg Hex (T * ptr)
Defined at line 957 of file ../../third_party/re2/src/re2/re2.h
template <typename T>
Arg Octal (T * ptr)
Defined at line 964 of file ../../third_party/re2/src/re2/re2.h
void FUZZING_ONLY_set_maximum_global_replace_count (int i)
Controls the maximum count permitted by GlobalReplace(); -1 is unlimited.
FOR FUZZING ONLY.
Enumerations
enum ErrorCode
| Name | Value | Comments |
|---|---|---|
| NoError | 0 | -- |
| ErrorInternal | 1 |
Unexpected error |
| ErrorBadEscape | 2 |
bad escape sequence |
| ErrorBadCharClass | 3 |
bad character class |
| ErrorBadCharRange | 4 |
bad character class range |
| ErrorMissingBracket | 5 |
missing closing ] |
| ErrorMissingParen | 6 |
missing closing ) |
| ErrorUnexpectedParen | 7 |
unexpected closing ) |
| ErrorTrailingBackslash | 8 |
trailing |
| ErrorRepeatArgument | 9 |
repeat argument missing, e.g. "*" |
| ErrorRepeatSize | 10 |
bad repetition argument |
| ErrorRepeatOp | 11 |
bad repetition operator |
| ErrorBadPerlOp | 12 |
bad perl operator |
| ErrorBadUTF8 | 13 |
invalid UTF-8 in regexp |
| ErrorBadNamedCapture | 14 |
bad named capture group |
| ErrorPatternTooLarge | 15 |
pattern too large (compile failed) |
Defined at line 248 of file ../../third_party/re2/src/re2/re2.h
enum CannedOptions
| Name | Value | Comments |
|---|---|---|
| DefaultOptions | 0 | -- |
| Latin1 | 1 |
treat input as Latin-1 (default UTF-8) |
| POSIX | 2 |
POSIX syntax, leftmost-longest match |
| Quiet | 3 |
do not log about regexp parse errors |
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 276 of file ../../third_party/re2/src/re2/re2.h
enum Anchor
| Name | Value | Comments |
|---|---|---|
| UNANCHORED | 0 |
No anchoring |
| ANCHOR_START | 1 |
Anchor at start only |
| ANCHOR_BOTH | 2 |
Anchor at start and end |
Type of match.
Defined at line 542 of file ../../third_party/re2/src/re2/re2.h