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

[EXTENSION] Regex


Post New Thread Reply   
 
Thread Tools Display Modes
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, 1283 views)
File Type: zip regex_source.zip (295.2 KB, 694 views)

Last edited by API; 05-12-2007 at 20:23. Reason: Final Release
API is offline
Send a message via AIM to API
API
Veteran Member
Join Date: May 2006
Old 05-12-2007 , 20:23   Re: [EXTENSION] Regex
Reply With Quote #2

Update
Updated to windows and linux fully working.
API is offline
Send a message via AIM to API
Scuzzy
Senior Member
Join Date: Oct 2007
Old 02-03-2008 , 10:30   Re: [EXTENSION] Regex
Reply With Quote #3

Any chance of this being natively included in SourceMod in a future release?

Scuzzy
Scuzzy is offline
BAILOPAN
Join Date: Jan 2004
Old 02-11-2008 , 22:49   Re: [EXTENSION] Regex
Reply With Quote #4

It's implemented in the 1.1 branch.
__________________
egg
BAILOPAN is offline
Scuzzy
Senior Member
Join Date: Oct 2007
Old 02-13-2008 , 10:23   Re: [EXTENSION] Regex
Reply With Quote #5

Quote:
Originally Posted by BAILOPAN View Post
It's implemented in the 1.1 branch.
I realize this is going to sound very stupid, but I don't know what that exactly means. I have sourcemod 1876. I haven't seen anything in the scripting pages, and didn't see an include file for it, so I'm a bit lost... Again, probably very stupid, but I guess my question was will it (or is it) natively usable in 1876 that is on the main download page?

Scuzzy
Scuzzy is offline
bl4nk
SourceMod Developer
Join Date: Jul 2007
Old 02-13-2008 , 11:51   Re: [EXTENSION] Regex
Reply With Quote #6

He's talking about version 1.1 of SM, which has yet to come. SM is still in it's late beta form, and 1.0 is coming in the near future.
bl4nk is offline
Scuzzy
Senior Member
Join Date: Oct 2007
Old 02-13-2008 , 21:57   Re: [EXTENSION] Regex
Reply With Quote #7

Thanks, sorry for the confusion. BTW, the fact that it runs this smooth in Beta is extremely impressive to me.

Scuzzy
Scuzzy is offline
Sexual Harassment Panda
Veteran Member
Join Date: Dec 2008
Location: San Diego, CA
Old 06-10-2009 , 19:07   Re: [EXTENSION] Regex
Reply With Quote #8

It does not seem this extension wants to work. How do you compile an extension?
__________________
Sexual Harassment Panda is offline
naris
AlliedModders Donor
Join Date: Dec 2006
Old 06-10-2009 , 19:46   Re: [EXTENSION] Regex
Reply With Quote #9

Quote:
Originally Posted by Sexual Harassment Panda View Post
It does not seem this extension wants to work. How do you compile an extension?
Bail mentioned that it was included into SM version 1.1 just a couple of posts up. The current version is 1.2.1, so the extension is NOT needed.
naris is offline
.Dare Devil.
Veteran Member
Join Date: Sep 2010
Old 02-11-2012 , 08:56   Re: [EXTENSION] Regex
Reply With Quote #10

one question
is the system same as amxmodx regex?

Did I done this correctly?
PHP Code:
bool:is_invalid(const text[])
{
    new 
error[50], num
    
new Regex:regex regex_match (text"([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]))"numerror49"i")
    if(
regex >= REGEX_OK)
    {
        
regex_free(regex)
        return 
true
    
}

    return 
false

PS: can not test things on this week.
.Dare Devil. is offline
Reply


Thread Tools
Display Modes

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 16:14.


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