Raised This Month: $51 Target: $400
 12% 

[EXTENSION] Regex


  
 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
Author Message
API
Veteran Member
Join Date: May 2006
Old 05-11-2007 , 01:05   [EXTENSION] Regex
Reply With Quote #1

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.
Attached Files
File Type: zip regex_1_0_0.zip (133.2 KB, 1285 views)
File Type: zip regex_source.zip (295.2 KB, 700 views)

Last edited by API; 05-12-2007 at 20:23. Reason: Final Release
API is offline
Send a message via AIM to API
 



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 15:20.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode