Regex Class Documentation
The Regex class provides a set of static methods for working with regular expressions, including escaping/unescaping strings, counting matches, checking for matches, and replacing text based on patterns. These methods facilitate working with regular expressions in a convenient and efficient manner.
Methods
escape
public static escape(str: string): stringDescription:
Escapes a string to safely use it within a regular expression.
Parameters:
str(string): The string to escape.
Returns:
- (string): The escaped string.
Example:
auto escapedStr = Regex.escape("special*characters?");
Out.println(escapedStr);unescape
public static unescape(str: string): stringDescription:
Unescapes a string from a regular expression.
Parameters:
str(string): The string to unescape.
Returns:
- (string): The unescaped string.
Example:
auto unescapedStr = Regex.unescape("special\\*characters\\?");
Out.println(unescapedStr);count
public static count(pattern: string, value: string): i32Description:
Counts the number of matches of the specified pattern in the given value.
Parameters:
pattern(string): The regular expression pattern to match.value(string): The string to search for matches.
Returns:
- (i32): The number of matches found.
Example:
auto matchCount = Regex.count("\\d+", "There are 24 apples and 42 oranges.");
Out.println(matchCount);isMatch
public static isMatch(pattern: string, value: string): boolDescription:
Checks if the specified pattern matches any part of the given value.
Parameters:
pattern(string): The regular expression pattern to match.value(string): The string to search for matches.
Returns:
- (bool):
trueif a match is found,falseotherwise.
Example:
auto isMatching = Regex.isMatch("\\d+", "There are 24 apples and 42 oranges.");
Out.println(isMatching); // truereplace
public static replace(pattern: string, value: string, replacement: string): boolDescription:
Replaces all occurrences of the specified pattern in the given value with the replacement string.
Parameters:
pattern(string): The regular expression pattern to match.value(string): The string in which to replace matches.replacement(string): The replacement string.
Returns:
- (bool):
trueif replacements were made,falseotherwise.
Example:
auto result = Regex.replace("\\d+", "There are 24 apples and 42 oranges.", "many");
Out.println(result); // "There are many apples and many oranges."Example Usage
Escape and Unescape Strings
auto specialPattern = ".*+?^${}()|[]\\";
auto escapedPattern = Regex.escape(specialPattern);
Out.println(escapedPattern);
auto unescapedPattern = Regex.unescape(escapedPattern);
Out.println(unescapedPattern);Counting Matches
auto text = "The rain in Spain falls mainly in the plain.";
auto wordCount = Regex.count("\\b\\w+\\b", text);
Out.println(wordCount); // Number of wordsChecking for Matches
auto text = "Sample text with numbers 1234 and 5678.";
auto hasNumbers = Regex.isMatch("\\d+", text);
Out.println(hasNumbers); // trueReplacing Matches
auto text = "Apples are $1, oranges are $2.";
auto newText = Regex.replace("\\$\\d+", text, "$3");
Out.println(newText); // "Apples are $3, oranges are $3."