Raised This Month: $ Target: $400
 0% 

Regular Expression API


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
BAILOPAN
Join Date: Jan 2004
Old 10-05-2004 , 05:13   Regular Expression API
Reply With Quote #1

As of 10-05-2004 (and soon 0.20-RC7) AMX Mod X has a module for dealing with Regular Expressions. It's pretty simple and functionality will be added as needed.

Regular Expressions let you describe ways in which to break down strings. They are extremely powerful. AMX Mod X uses the Perl Compatible RE library, you can read the specifics at their site.

Here is a quick example script:
Code:
#include <amxmodx> #include <regex> public plugin_init() {     register_plugin("Regex", "1.0", "BAILOPAN")     register_srvcmd("amx_regex", "cmdtest") } public cmdtest() {     new str[] = "It's Walky!"     //this pattern will match any string which contains     // two groupings of characters separated by a space     // the two groupings are substrings 1 and 2     new pattern[] = "(.+) (.+)"         new num, error[128]     //str = string     //pattern = pattern to use     //num = special return case code     //error = if there's an error, it will go here     //127 - error's max length     new Regex:re = regex_match(str, pattern, num, error, 127)     server_print("Result=%d, num=%d, error=%s", re, num, error)     //REGEX_OK means there was a match     if (re >= REGEX_OK)     {         new str2[64]         new i         //since it returned REGEX_OK, num has         // the number of substrings matched by the pattern.         //the first substring (0) seems to match the whole string.         for (i=0; i<num; i++)         {             regex_substr(re, i, str2, 63)             server_print("Substring %d: %s", i, str2)         }         //the regular expression matcher uses memory.         //you must free it if you get REGEX_OK         //This will also set re to 0.         regex_free(re)     }         //note the invalid regular expression pattern     //this will return REGEX_PATTERN_FAIL, -1     re = regex_match("Bruno the Bandit", ".+(]", num, error, 127)         server_print("Result=%d, num=%d, error=%s", re, num, error) }

If you compile and run this script (amx_regex in server console) you will see the following output:
Code:
Result=1, num=3, error=
Substring 0: It's Walky!
Substring 1: It's
Substring 2: Walky!
Result=-1, num=4, error=missing )
Note that the third parameter to "regex_match()" is special. It's either the number of substrings, a match error code, or a pattern error position (depending on the return value).

I hope this helps, post any questions here. Use the funcwiki to get specific details (or read the include file).
__________________
egg
BAILOPAN is offline
devicenull
Veteran Member
Join Date: Mar 2004
Location: CT
Old 10-05-2004 , 14:52  
Reply With Quote #2

/me claps

Yay
__________________
Various bits of semi-useful code in a bunch of languages: http://code.devicenull.org/
devicenull is offline
Infra
Senior Member
Join Date: Aug 2004
Location: California
Old 10-05-2004 , 14:59  
Reply With Quote #3

Yay, now time for the non-beta of .20 ^_^

GJ Bail...and all other devs of course
__________________
--Infra
Infra is offline
Send a message via AIM to Infra
Reply



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 17:28.


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