Core Function RegexMatch

From Sputnik Wiki
Revision as of 16:27, 12 September 2013 by UberFoX (Talk | contribs)
Jump to: navigation, search
RegexMatch( <expression>, <pattern>, <matches>, <offset> )

Contents

Description

Perform a regular expression match.

Parameters

expression

The input string.

pattern

The pattern to search for, as a string.

See <Expression> =~ m/pattern/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 "m" in this pattern so '//' is just as valid as m// would normally be.

The pattern can be written as:

"m/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.

matches

Optional; If matches is provided, then it is filled with the results of search.

For example if matches is set to $matches then

$matches[0] will contain the text that matched the full pattern.

$matches[1] will have the text that matched the first captured parenthesized subpattern, and so on.

Default: $_rg

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> =~ m/pattern/flags regex operator =~ so you should take a look at that and see if it suits your needs.

Example

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

Simple match check

if(RegexMatch('abcdef', '/^abc/'))
{
	printr "true";
}
else
{
	printr "false";
}
// Prints true

Another simple match check

if(RegexMatch('abcdef', '/^def/'))
{
	printr "true";
}
else
{
	printr "false";
}
// Prints false

Returning captured group

my $ret;
if(RegexMatch('111777', '/^(\d{3})(\d{3})/', $ret))
{
	printr $ret;
}
// Prints
//ARRAY
//{
//        [0] => 111777
//        [1] => 111
//        [2] => 777
//}

Returning captured group using offset parameter

my $ret;
if(RegexMatch('ddd111777', '/^(\d{3})(\d{3})/', $ret, 3))
{
	printr $ret;
}
// Prints
//ARRAY
//{
//        [0] => 111777
//        [1] => 111
//        [2] => 777
//}
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox