#include <userver/utils/regex.hpp>
A drop-in replacement for std::regex
without huge includes and with better performance characteristics.
utils::regex is currently implemented using re2.
- See also
- utils::regex_match
-
utils::regex_search
-
utils::regex_replace
Read re2 documentation on the limitations of re2 engine. Notably, it does not support:
- lookahead and lookbehind;
- quantifiers over 1000, regexes with large repetition counts consume more memory;
- spaces in quantifiers like
\w{1, 5}
;
- possessive quantifiers.
An example of complex string parsing using <tt>utils::regex</tt>
std::vector<std::string_view>
words;
throw std::invalid_argument(fmt::format(
"Invalid characters '{}'",
punctuation));
}
throw std::invalid_argument(fmt::format(
"Word '{}' should be capitalized",
word));
}
}
throw std::invalid_argument(fmt::format(
"Invalid characters '{}'",
remaining));
}
}
SplitTextIntoWords(
"Foo bar. Baz, qux quux."), testing::ElementsAre(
"Foo",
"bar",
"Baz",
"qux",
"quux")
);
}
Definition at line 44 of file regex.hpp.
◆ regex()
utils::regex::regex |
( |
std::string_view |
pattern | ) |
|
|
explicit |
Compiles regex from pattern, always valid on construction.
- Exceptions
-
◆ GetPatternView()
std::string_view utils::regex::GetPatternView |
( |
| ) |
const |
- Returns
- a view to the original pattern stored inside.
◆ operator==()
- Returns
true
if the patterns are equal.
- Note
- May also return
true
if the patterns are not equal, but are equivalent.
◆ str()
std::string utils::regex::str |
( |
| ) |
const |
- Returns
- the original pattern.
◆ match_results
◆ regex_match
Returns true if the specified regular expression matches the whole of the input. Fills in what matched in m.
- Note
- m may be clobbered on failure.
◆ regex_replace [1/2]
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
- See also
- utils::Re2Replacement
◆ regex_replace [2/2]
std::string regex_replace |
( |
std::string_view |
str, |
|
|
const regex & |
pattern, |
|
|
std::string_view |
repl |
|
) |
| |
|
friend |
Create a new string where all regular expression matches replaced with repl.
Interprets repl as a literal, does not support substitutions.
- See also
- utils::Re2Replacement
◆ regex_search
Determines whether the regular expression matches anywhere in the target character sequence. Fills in what matched in m.
- Note
- m may be clobbered on failure.
The documentation for this class was generated from the following file: