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
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:
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).