Core Function RegexEscape
(→Example) |
m (1 revision) |
(One intermediate revision by one user not shown) |
Latest revision as of 12:37, 14 June 2015
RegexEscape( <expression>, <delimiter> )
Contents |
Description
Escape regular expression characters.
RegexEscape() takes a string and puts a backslash in front of every character that is part of the regular expression syntax.
This is useful if you have a run-time string that you need to match in some text and the string may contain special Regexp characters.
Escapes a minimal set of characters (\, *, +, ?, |, {, [, (,), ^, $,., #, and white space) by replacing them with their escape codes.
This instructs the regular expression engine to interpret these characters literally rather than as metacharacters.
Parameters
expression
The string to use.
delimiter
Optional; A string containing extra characters to also add escapes to.
The most common would be '/'
Return Value
Success: Returns the escaped string.
Failure: Returns empty string.
Remarks
None.
Example
// Obviously using @"" is the best way use Regexp strings say @"(\w+)\s(\1)"; // Prints: (\w+)\s(\1) say RegexEscape(@"(\w+)\s(\1)"); // Prints: \(\\w\+\)\\s\(\\1\) say RegexUnEscape(RegexEscape(@"(\w+)\s(\1)")); // Prints: (\w+)\s(\1)
Example of using the delim
$keywords = '$40 for a g3/400'; $keywords = RegexEscape($keywords, '/'); say $keywords; // Prints: \$40\ for\ a\ g3\/400
Another example of using the delim
$keywords = '$40 for a g3/400'; $keywords = RegexEscape($keywords, '/0'); say $keywords; // Prints: \$4\0\ for\ a\ g3\/4\0\0