Core Function GMatch

From Sputnik Wiki
Jump to: navigation, search
GMatch( <string>, <pattern>, <asKeypair>, <offset> )

Contents

Description

Extract the patterns from a string and return as array or associative array .

Parameters

string

The string to evaluate.

pattern

Go read the Patterns section on the Find( ) function to learn how to create and use patterns then come back here to put it to use on this function.

asKeypair

Optional; If true the return will be an associative array where the first capture is the key and the second capture is the value (regardless of how many captures there are) if there are more than two captures then the third one will also become a key and forth one a value and so on.

offset

Optional; Specifies where to start the search; its default value is 0 and can be negative.

Return Value

Returns an array of all captures or optionally places the captures into key/value pairs if asKeypair is set to true.

Remarks

This function is pretty much the same a the LUA String.GMatch() however this one returns start position starting at 0 (LUA's starts at 1) and lowers the end position by 1, Also the Offset begins at 0 here where as in LUA it begins at 1.

This is because in Sputnik chars in a string start at 0 not 1.

Example

Basic example

$t = array();
$s = "Hello world from Sputnik!";
foreach(gmatch($s, "(%w+)") as $v)
{
    $t[] = $v;
}
printr $t;
// Prints
// Array
// (
//     [0] => Hello
//     [1] => world
//     [2] => from
//     [3] => Sputnik
// )

Example of using asKeypair in a Foreach loop

$t = array();
$s = "from=world, to=Sputnik";
foreach(gmatch($s, "(%w+)=(%w+)", true) as $k => $v)
{
    $t[$k] = $v;
}
printr $t;
// Prints
// Array
// (
//     [from] => world
//     [to] => Sputnik
// )

Using GMatch() to split strings at a separator

Function mysplit($inputstr, $sep = '%s')
{
	my $t = array();
	foreach(gmatch($inputstr, "([^" . $sep . "]+)") as $str)
		$t[] = $str;
	return $t;
}
// Split a string
$ret = mysplit("The,quick,brown,fox", ",");
// Print result
printr $ret;
// Prints:
// Array
// (
//     [0] => The
//     [1] => quick
//     [2] => brown
//     [3] => fox
// )

Advanced example

$stuff = "to iterate through this, we match at ... punctuation!";
 
say "\niterating through the words in a string";
foreach(gmatch($stuff,'%w+') as $k)
{
	say $k;
}
 
say "\nAlternative - capturing non-spaces";
foreach(gmatch($stuff,'%S+') as $k)
{
	say $k;
}
 
say "\nCapture a char, then greedy to last repeat of it";
foreach(gmatch($stuff,'(.)(.*)(%1)') as list($k, $j, $i))
{
	say "$k    $j    $i";
}
 
say "\nCapture a char, then sparse to last repeat of it";
foreach(gmatch($stuff,'(.)(.-)(%1)') as list($k, $j, $i))
{
	say "$k    $j    $i";
}
say "\n6 chars at a time, 2 to one variable and 4 to another";
foreach(gmatch($stuff,'(..)(....)') as list($k, $j))
{
	say "$k    $j";
}
 
say "\niterating through the parameters to a CGI script";
$cgistring = 'this=17&that=24&somemore=extra text';
foreach(gmatch($cgistring,'([^=&]+)=([^&]+)') as list($k, $j))
{
	say "$k    $j";
}
// Prints:
 
// iterating through the words in a string
// to
// iterate
// through
// this
// we
// match
// at
// punctuation
 
// Alternative - capturing non-spaces
// to
// iterate
// through
// this,
// we
// match
// at
// ...
// punctuation!
 
// Capture a char, then greedy to last repeat of it
// t    o iterate through this, we match at ... punctua    t
 
// Capture a char, then sparse to last repeat of it
// t    o i    t
// e    rat    e
//      through
// t    his, we ma    t
// c    h at ... pun    c
// t    ua    t
 
// 6 chars at a time, 2 to one variable and 4 to another
// to     ite
// ra    te t
// hr    ough
// t    his,
// w    e ma
// tc    h at
// .    .. p
// un    ctua
 
// iterating through the parameters to a CGI script
// this    17
// that    24
// somemore    extra text
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox