RegexInterface Class Reference

#include <lib_std_regex.h>

Inheritance diagram for RegexInterface:

Detailed Description

Realizes a regular expression object to carry out matching, searching, spliting, or replacement operations with.

Warning
Just as 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< BoolMatch (const String &string, const ValueReceiver< const RegexMatch & > &receiver, REGEX_MATCH_FLAGS flags=REGEX_MATCH_FLAGS::DEFAULT) const
 
MAXON_METHOD Result< BoolSearch (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< BoolSplit (const String &string, const ValueReceiver< const String & > &receiver, REGEX_MATCH_FLAGS flags=REGEX_MATCH_FLAGS::DEFAULT) const
 
MAXON_METHOD Result< StringReplace (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")
 

Member Function Documentation

◆ MAXON_INTERFACE()

MAXON_INTERFACE ( RegexInterface  ,
MAXON_REFERENCE_COPY_ON_WRITE  ,
"net.maxon.interface.regex"   
)
private

◆ Create()

static MAXON_METHOD Result<Regex> Create ( const String string,
REGEX_SYNTAX_FLAGS  flags = REGEX_SYNTAX_FLAGS::ECMAScript 
)
static

Constructs an expression with the given string and syntax flags.

Parameters
[in]stringThe expression string.
[in]flagsThe syntax flags for the expression. Defaults to REGEX_SYNTAX_FLAGS::ECMAScript.

◆ GetString()

MAXON_METHOD String GetString ( ) const

Returns the string of the expression.

◆ GetFlags()

MAXON_METHOD REGEX_SYNTAX_FLAGS GetFlags ( ) const

Returns the syntax flags used by the expression.

◆ Match()

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", "", "").

Parameters
[in]stringThe string to match.
[out]receiverThe receiver to store the matches for regex in string in.
[in]flagsThe flags for the match operation.
Returns
true if string fully matches expression, false otherwise.

◆ Search()

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.

Parameters
[in]stringThe string to search.
[out]receiverThe receiver to store the matches for regex in string in.
[in]flagsThe flags for the search operation.
Returns
true if regex matches in string, false otherwise.

◆ Split()

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.

Parameters
[in]stringThe string to split.
[out]receiverThe receiver to store the parts of the split string in.
[in]flagsThe flags for the split operation.
Returns
true if string was split, false otherwise.

◆ Replace()

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.

Parameters
[in]stringThe string to replace content in.
[in]replacementThe regex formating syntax replacement string to use.
[in]flagsThe flags for the replace operation.
Returns
The string with the replacements or the original string if no matches were found.