Hey guys,
For those of you that do not know what regex is, it is an extension that adds regular string support.
Quote:
Provides Regular Expression functions. Regular Expressions let you describe ways in which to break down strings. They are extremely powerful. SourceMod uses the Perl Compatible RE library, you can read the specifics at their site.
|
Credit
Thanks to everyone in IRC, most of it is not original work and was a port of the AMXModX module.
Installation
1. Extract 'regex.ext.dll' and 'regex.ext.so' to the 'addons/sourcemod/extensions' folder.
2. Extract 'regex.inc' to the 'addons/sourcemod/scripting/include' folder.
Functions
There are
3 different functions that you can use in your plugins.
They are documented and called as follows.
Code:
/**
* Initiates a regular expression match
* @param str The string used to init a regular expression match
* @param pattern The pattern used to init a regular expression match
* @param ret It's either the number of substrings, a match error code
* @return The handle to use for regex functions
*/
native Handle:Regex_Match(const String:str[],const String:pattern[],&ret);
/**
* Retrieves a matched sub string
* @param regexhdl The handle of the regex data
* @param str_id Substr ID to search for
* @param buffer The buffer to store to
* @param maxlen Max length of the return result
* @return Returns 0 on error
*/
native Regex_Substr(Handle:regexhdl,str_id,String:buffer[],maxlen);
/**
* Free a regex piece of data, used to clean memory
* @param regexhdl The handle of the regex data
* @return 0 means you passed a bad ID
*/
native Regex_Free(Handle:regexhdl);
Here is a small example on usage of the functions, this is also a port of the old AMXModX example.
Code:
#include <sourcemod>
#include <regex>
public Plugin:myinfo =
{
name = "Regex Test",
author = "PimpinJuice",
description = "Testing out the regex extension",
version = "1.0.0.0",
url = "http://pimpinjuice.net/"
};
public OnPluginStart()
{
new String:str[] = "It's Walky!";
new String:pattern[] = "(.+) (.+)";
new Handle:re,num;
re=Regex_Match(str,pattern,num);
PrintToServer("num of substrs=%d",num);
if(num>0)
{
new String:str2[64];
new i;
for (i=0; i<num; i++)
{
Regex_Substr(re,i,str2,63);
PrintToServer("Substring %d: %s",i,str2);
}
Regex_Free(re);
}
}
The output will follow as:
Quote:
num of substrs=3
Substring 0: It's Walky!
Substring 1: It's
Substring 2: Walky!
|
Note: I have included the source code, feel free to modify and take code from it, but credit me.
Have fun.