ds@1952: #if defined _regex_included ds@1952: #endinput ds@1952: #endif ds@1952: #define _regex_included ds@1952: ds@1952: /** ds@1952: * @section Flags for compiling regex expressions. These come directly from the ds@1952: * pcre library and can be used in MatchRegex and CompileRegex. ds@1952: */ ds@1952: #define PCRE_CASELESS 0x00000001 /* Ignore Case */ ds@1952: #define PCRE_MULTILINE 0x00000002 /* Multilines (affects ^ and $ so that they match the start/end of a line rather than matching the start/end of the string). */ ds@1952: #define PCRE_DOTALL 0x00000004 /* Single line (affects . so that it matches any character, even new line characters). */ ds@1952: #define PCRE_EXTENDED 0x00000008 /* Pattern extension (ignore whitespace and # comments). */ ds@1952: #define PCRE_UNGREEDY 0x00000200 /* Invert greediness of quantifiers */ ds@1952: #define PCRE_UTF8 0x00000800 /* Use UTF-8 Chars */ ds@1952: #define PCRE_NO_UTF8_CHECK 0x00002000 /* Do not check the pattern for UTF-8 validity (only relevant if PCRE_UTF8 is set) */ ds@1952: ds@1952: ds@1952: /** ds@1952: * Regex expression error codes. ds@1952: */ ds@1952: enum RegexError ds@1952: { ds@1952: REGEX_ERROR_NONE = 0, /* No error */ ds@1952: REGEX_ERROR_NOMATCH = -1, /* No match was found */ ds@1952: REGEX_ERROR_NULL = -2, ds@1952: REGEX_ERROR_BADOPTION = -3, ds@1952: REGEX_ERROR_BADMAGIC = -4, ds@1952: REGEX_ERROR_UNKNOWN_OPCODE = -5, ds@1952: REGEX_ERROR_NOMEMORY = -6, ds@1952: REGEX_ERROR_NOSUBSTRING = -7, ds@1952: REGEX_ERROR_MATCHLIMIT = -8, ds@1952: REGEX_ERROR_CALLOUT = -9, /* Never used by PCRE itself */ ds@1952: REGEX_ERROR_BADUTF8 = -10, ds@1952: REGEX_ERROR_BADUTF8_OFFSET = -11, ds@1952: REGEX_ERROR_PARTIAL = -12, ds@1952: REGEX_ERROR_BADPARTIAL = -13, ds@1952: REGEX_ERROR_INTERNAL = -14, ds@1952: REGEX_ERROR_BADCOUNT = -15, ds@1952: REGEX_ERROR_DFA_UITEM = -16, ds@1952: REGEX_ERROR_DFA_UCOND = -17, ds@1952: REGEX_ERROR_DFA_UMLIMIT = -18, ds@1952: REGEX_ERROR_DFA_WSSIZE = -19, ds@1952: REGEX_ERROR_DFA_RECURSE = -20, ds@1952: REGEX_ERROR_RECURSIONLIMIT = -21, ds@1952: REGEX_ERROR_NULLWSLIMIT = -22, /* No longer actually used */ ds@1952: REGEX_ERROR_BADNEWLINE = -23 ds@1952: } ds@1952: ds@1952: /** ds@1952: * Precompile a regular expression. Use this if you intend on using the ds@1952: * same expression multiple times. Pass the regex handle returned here to ds@1952: * MatchRegex to check for matches. ds@1952: * ds@1952: * @param pattern The regular expression pattern. ds@1952: * @param flags General flags for the regular expression. ds@1952: * @param error Error message encountered, if applicable. ds@1952: * @param maxLen Maximum string length of the error buffer. ds@1952: * @param errcode Regex type error code encountered, if applicable. ds@1952: * @return Valid regex handle on success, INVALID_HANDLE on failure. ds@1952: */ ds@1952: native Handle:CompileRegex(const String:pattern[], flags = 0, String:error[]="", maxLen = 0, &RegexError:errcode = REGEX_ERROR_NONE); ds@1952: ds@1952: /** ds@1952: * Matches a string against a pre-compiled regular expression pattern. ds@1952: * ds@1952: * @param str The string to check. ds@1952: * @param regex Regex Handle from CompileRegex() ds@1952: * @param ret Error code, if applicable. ds@1952: * @return Number of substrings found or -1 on failure. ds@1952: * ds@1952: * @note Use the regex handle passed to this function to extract ds@1952: * matches with GetRegexSubString(). ds@1952: */ ds@1952: native MatchRegex(Handle:regex, const String:str[], &RegexError:ret = REGEX_ERROR_NONE); ds@1952: ds@1952: /** ds@1952: * Returns a matched substring from a regex handle. ds@1952: * Substring ids start at 0 and end at substrings-1, where substrings is the number returned ds@1952: * by MatchRegex ds@1952: * ds@1952: * @param regex The regex handle to extract data from. ds@1952: * @param str_id The index of the expression to get - starts at 0, and ends at substrings - 1. ds@1952: * @param buffer The buffer to set to the matching substring. ds@1952: * @param maxLen The maximum string length of the buffer. ds@1952: * @return True if a substring was found, False on fail/error ds@1952: */ ds@1952: native bool:GetRegexSubString(Handle:regex, str_id, String:buffer[], maxlen); ds@1952: ds@1952: /** ds@1952: * Matches a string against a regular expression pattern. ds@1952: * ds@1952: * @note If you intend on using the same regular expression pattern ds@1952: * multiple times, consider using CompileRegex and MatchRegex ds@1952: * instead of making this function reparse the expression each time. ds@1952: * ds@1952: * @param str The string to check. ds@1952: * @param pattern The regular expression pattern. ds@1952: * @param flags General flags for the regular expression. ds@1952: * @param error Error message, if applicable. ds@1952: * @param maxLen Maximum length of the error buffer. ds@1952: * @return Number of substrings found or -1 on failure. ds@1952: */ ds@1952: stock SimpleRegexMatch(const String:str[], const String:pattern[], flags = 0, String:error[]="", maxLen = 0) ds@1952: { ds@1952: new Handle:regex = CompileRegex(pattern, flags, error, maxLen); ds@1952: ds@1952: if (regex == INVALID_HANDLE) ds@1952: { ds@1952: return -1; ds@1952: } ds@1952: ds@1952: new substrings = MatchRegex(regex, str); ds@1952: ds@1952: CloseHandle(regex); ds@1952: ds@1952: return substrings; ds@1952: } ds@1952: ds@1952: /** ds@1952: * @endsection ds@1952: */ ds@1952: ds@1952: /** ds@1952: * Do not edit below this line! ds@1952: */ ds@1952: public Extension:__ext_regex = ds@1952: { ds@1952: name = "Regex Extension", ds@1952: file = "regex.ext", ds@1952: #if defined AUTOLOAD_EXTENSIONS ds@1952: autoload = 1, ds@1952: #else ds@1952: autoload = 0, ds@1952: #endif ds@1952: #if defined REQUIRE_EXTENSIONS ds@1952: required = 1, ds@1952: #else ds@1952: required = 0, ds@1952: #endif ds@1952: };