Core Function RegexEscape

From Sputnik Wiki
(Difference between revisions)
Jump to: navigation, search
(Created page with "<pre> RegexEscape( <expression> ) </pre> === Description === Escape regular expression characters. RegexEscape() takes a string and puts a backslash in front of every characte...")
 
m (1 revision)
 
(3 intermediate revisions by one user not shown)
Line 1: Line 1:
 
<pre>
 
<pre>
RegexEscape( <expression> )
+
RegexEscape( <expression>, <delimiter> )
 
</pre>
 
</pre>
  
Line 20: Line 20:
  
 
The string to use.
 
The string to use.
 +
 +
==== delimiter ====
 +
 +
Optional; A string containing extra characters to also add escapes to.
 +
 +
The most common would be '/'
  
 
=== Return Value ===
 
=== Return Value ===
Line 38: Line 44:
 
say RegexEscape(@"(\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)
 
say RegexUnEscape(RegexEscape(@"(\w+)\s(\1)")); // Prints: (\w+)\s(\1)
 +
</syntaxhighlight>
 +
 +
Example of using the delim
 +
 +
<syntaxhighlight lang="sputnik">
 +
$keywords = '$40 for a g3/400';
 +
$keywords = RegexEscape($keywords, '/');
 +
say $keywords;
 +
// Prints: \$40\ for\ a\ g3\/400
 +
</syntaxhighlight>
 +
 +
Another example of using the delim
 +
 +
<syntaxhighlight lang="sputnik">
 +
$keywords = '$40 for a g3/400';
 +
$keywords = RegexEscape($keywords, '/0');
 +
say $keywords;
 +
// Prints: \$4\0\ for\ a\ g3\/4\0\0
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
[[Category:Core Function]]
 
[[Category:Core Function]]

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
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox