Core Function RegexReplace

From Sputnik Wiki
Jump to: navigation, search
RegexReplace( <expression>, <pattern>, <replacement>, <limit>, <count>, <offset> )

Contents

Description

Perform a regular expression search and replace.

Parameters

expression

The input string.

pattern

NOTE - It highly recommend you use @"" strings for Regexp patterns etc so you can do @"\nTest\\Hello\t" instead of "\\nTest\\\\Hello\\t".

The pattern to search for, as a string.

See <Expression> =~ s/pattern/replacement/flags for information on how to construct the pattern or see the examples below.

Note - Unlike in normal =~ regex you do not need to specify the "s" in this pattern so '//' is just as valid as s// would normally be.

The pattern can be written as:

"s/Cat/"

OR

"/Cat/"

OR

"Cat"

The missing peices will be filled in automatically however if you don't include the / / you will be unable to use special flags such as "/Cat/i" for case insensitive match.

replacement

The string to replace.

Can also be a function etc if using the /e flag.

limit

Optional; The maximum possible replacements for each pattern in each subject string.

Default: -1 (no limit)

Note - You should note that -1 does not automatically make it replace all... You still need the /g flag (although /g flag is added automatically is the limit is set beyond 1).

offset

Optional; Normally, the search starts from the beginning of the subject string.

The optional parameter offset can be used to specify the alternate place from which to start the search.

It works exactly the same as SubStr() start position (so it can be negative etc).

Default: 0

Return Value

Success: Returns true.

Failure: Returns false.

Remarks

This function is a wrapper for the <Expression> =~ s/pattern/replacement/flags regex operator =~ so you should take a look at that and see if it suits your needs.

Example

See <Expression> =~ s/pattern/replacement/flags for more examples.

Finding and replacing the order of things in a string

$string = 'April 15, 2003';
$pattern = '/(\w+) (\d+), (\d+)/i';
$replacement = '$3 $2 $1';
echo RegexReplace($string, $pattern, $replacement);
// 2003 15 April

Example of using the $2 to get a char out of it

$string = 'April 15, 2003';
$pattern = '/(\w+) (\d+), (\d+)/i';
$replacement = '$1$2[0],$3';
echo RegexReplace($string, $pattern, $replacement);
// April1,2003

This example strips excess whitespace from a string.

$str = 'foo   o';
$str = RegexReplace($str, '/\s\s+/', ' ');
// This will be 'foo o' now
echo $str;

Replacing using the offset parameter

// Case insensitive replace all with no count variable and
// start matching from position 3
say RegexReplace('CatCatFox', 's/(cat)/i', 'Dog', -1, null, 3);
// Prints: CatDogFox

Same as above but this time using a function to decide the replacement string

// Case insensitive replace all with no count variable and
// start matching from position 3
say RegexReplace('CatCatFox', 's/(cat)/ie', 'Tee($1)', -1, null, 3);
Function Tee( $a )
{
	return "#$a#";
}
// Prints: Cat#Cat#Fox
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox