AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Extensions (https://forums.alliedmods.net/forumdisplay.php?f=134)
-   -   [EXTENSION] Regex (https://forums.alliedmods.net/showthread.php?t=55012)

API 05-11-2007 01:05

[EXTENSION] Regex
 
2 Attachment(s)
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. :)

API 05-12-2007 20:23

Re: [EXTENSION] Regex
 
Update
Updated to windows and linux fully working.

Scuzzy 02-03-2008 10:30

Re: [EXTENSION] Regex
 
Any chance of this being natively included in SourceMod in a future release?

Scuzzy

BAILOPAN 02-11-2008 22:49

Re: [EXTENSION] Regex
 
It's implemented in the 1.1 branch.

Scuzzy 02-13-2008 10:23

Re: [EXTENSION] Regex
 
Quote:

Originally Posted by BAILOPAN (Post 584260)
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

bl4nk 02-13-2008 11:51

Re: [EXTENSION] Regex
 
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.

Scuzzy 02-13-2008 21:57

Re: [EXTENSION] Regex
 
Thanks, sorry for the confusion. BTW, the fact that it runs this smooth in Beta is extremely impressive to me.

Scuzzy

Sexual Harassment Panda 06-10-2009 19:07

Re: [EXTENSION] Regex
 
It does not seem this extension wants to work. How do you compile an extension?

naris 06-10-2009 19:46

Re: [EXTENSION] Regex
 
Quote:

Originally Posted by Sexual Harassment Panda (Post 845972)
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.

.Dare Devil. 02-11-2012 08:56

Re: [EXTENSION] Regex
 
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.


All times are GMT -4. The time now is 10:55.

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