#include <lib_std_regex.h>
Realizes a regular expression object to carry out matching, searching, spliting, or replacement operations with.
std::regex
, maxon::regex::Regex
does not support named capturing groups. This regex feature is simply not available, and one must use group indices instead to access the captured groups. Public Member Functions | |
MAXON_METHOD String | GetString () const |
MAXON_METHOD REGEX_SYNTAX_FLAGS | GetFlags () const |
MAXON_METHOD Result< Bool > | Match (const String &string, const ValueReceiver< const RegexMatch & > &receiver, REGEX_MATCH_FLAGS flags=REGEX_MATCH_FLAGS::DEFAULT) const |
MAXON_METHOD Result< Bool > | Search (const String &string, const ValueReceiver< const String & > &receiver, REGEX_MATCH_FLAGS flags=REGEX_MATCH_FLAGS::DEFAULT|REGEX_MATCH_FLAGS::MATCH_ALL) const |
MAXON_METHOD Result< Bool > | Split (const String &string, const ValueReceiver< const String & > &receiver, REGEX_MATCH_FLAGS flags=REGEX_MATCH_FLAGS::DEFAULT) const |
MAXON_METHOD Result< String > | Replace (const String &string, const String &replacement, REGEX_MATCH_FLAGS flags=REGEX_MATCH_FLAGS::DEFAULT) const |
Static Public Member Functions | |
static MAXON_METHOD Result< Regex > | Create (const String &string, REGEX_SYNTAX_FLAGS flags=REGEX_SYNTAX_FLAGS::ECMAScript) |
Private Member Functions | |
MAXON_INTERFACE (RegexInterface, MAXON_REFERENCE_COPY_ON_WRITE, "net.maxon.interface.regex") | |
|
private |
|
static |
Constructs an expression with the given string and syntax flags.
[in] | string | The expression string. |
[in] | flags | The syntax flags for the expression. Defaults to REGEX_SYNTAX_FLAGS::ECMAScript . |
MAXON_METHOD String GetString | ( | ) | const |
Returns the string of the expression.
MAXON_METHOD REGEX_SYNTAX_FLAGS GetFlags | ( | ) | const |
Returns the syntax flags used by the expression.
MAXON_METHOD Result<Bool> Match | ( | const String & | string, |
const ValueReceiver< const RegexMatch & > & | receiver, | ||
REGEX_MATCH_FLAGS | flags = REGEX_MATCH_FLAGS::DEFAULT |
||
) | const |
Evaluates if the given string
matches the regex in its entirety.
As always in regular expressions, matching requires the full string to match the expression. The 0th element in the receiver
for a valid match will always be the full match, and following elements are sub-matches for potentially present groups in the regex
. Optional groups will evaluate to the empty string when not present in the input string
, due to the full match nature of Match()
. E.g., matching the string "hello world"_s
against the expression "(hello)\\s*(world)?(!)?"_s
would return four matches and not three, even though there is no exclamation mark at the end of the string. The matches would be: ("hello world", "hello", "world", "")
. Analogously, matching the string "hello"_s
with that expression would return ("hello", "hello", "", "")
.
[in] | string | The string to match. |
[out] | receiver | The receiver to store the matches for regex in string in. |
[in] | flags | The flags for the match operation. |
true
if string
fully matches expression
, false
otherwise. MAXON_METHOD Result<Bool> Search | ( | const String & | string, |
const ValueReceiver< const String & > & | receiver, | ||
REGEX_MATCH_FLAGS | flags = REGEX_MATCH_FLAGS::DEFAULT|REGEX_MATCH_FLAGS::MATCH_ALL |
||
) | const |
Finds all occurrences of the regex in the given string
.
Other than Match()
, Search()
does not require the full string to match the expression, and will return all the partial matches in the given string. But while Match()
does support capture groups in its expressions, it will not decompose them in its output, i.e., it only yields the full match and not the capture groups for each partial match in the string.
[in] | string | The string to search. |
[out] | receiver | The receiver to store the matches for regex in string in. |
[in] | flags | The flags for the search operation. |
true
if regex
matches in string
, false
otherwise. MAXON_METHOD Result<Bool> Split | ( | const String & | string, |
const ValueReceiver< const String & > & | receiver, | ||
REGEX_MATCH_FLAGS | flags = REGEX_MATCH_FLAGS::DEFAULT |
||
) | const |
Splits the given string
into parts using the regex as a separator.
The regex
is not included in the result. The string
is split at the positions where the regex
matches.
[in] | string | The string to split. |
[out] | receiver | The receiver to store the parts of the split string in. |
[in] | flags | The flags for the split operation. |
true
if string
was split, false
otherwise. MAXON_METHOD Result<String> Replace | ( | const String & | string, |
const String & | replacement, | ||
REGEX_MATCH_FLAGS | flags = REGEX_MATCH_FLAGS::DEFAULT |
||
) | const |
Replaces all occurrences of the regex in the given string
with the given replacement
.
Supports replacement format strings based on the chosen regex syntax. Commonly supported symbols are $&
for the full match and $1
, $2
, ... for the first, second, ... capture group in the regex.
[in] | string | The string to replace content in. |
[in] | replacement | The regex formating syntax replacement string to use. |
[in] | flags | The flags for the replace operation. |