[Plugin] Regular Expressions Plugin v0.2 (RegEx)
#1

Introduction
First of all, i am sorry that i didn't released the first version here. Anyway, it's not such a big loose since this one is way better .
Secondly I know that there's already a plugin that does this, but without driving someone angry, i dare to say that that plugin is outdated (Latest release somewhere in 2011 i think) and is different from this, since it is using the boost.regex library. Now I know that boost includes very cool libraries and it is used by many programmers, even here. I too think that the functionality that it comes with it is great, but when it comes to regex, i am not very satisfied. It crashes sometimes (Yes, the crashes are not coming from the samp implementation, but from the library itself) and there are other libraries which are more stable and faster, so why not using them?
Plugin basic info
Now returning to this plugin.
This is a basic plugin based on Oniguruma library that allows the use of regular expressions in samp scripting.

As it is stated in this link http://lh3lh3.users.sourceforge.net/reb.shtml , onig is the fastest regex library at the moment. Even so, you must consider that it uses an BT-based alogrithm, so passing an non appropriate syntax, could take a lot of time to proccess on big strings. (Personally, i never encountered this problem).
Natives
pawn Code:
native regex_match(const string[], const RegularExpression[]);
native regex_search(const string[], const RegularExpression[], &startpos, &endpos);
native regex_replace(const string[], const RegularExpression[], const replacer[]);

native regex_end();
native regex_syntax(syntax);
native regex_exbuild(const RegularExpression[]);
native regex_set(const string[], RegularExpression);

native regex_exmatch(const string[], RegularExpression);
native regex_exsearch(const string[], RegularExpression, &startpos, &endpos);
native regex_exreplace(const string[], RegularExpression, const replacer[]);
Natives Info
-I already wrote the info in the windows .inc file, so i'm just copyin' it here and i'll give some examples below.
Code:
Added since v0.2
->regex_end() - This goes in your OnGameModeExit. It is important to put this function there if you don't want to deal with
				memory leackage problems. You can't use the regex functions after this function is called.
				
->regex_syntax(syntax) - This usually goes in your OnGameModeInit, as it specifies the default syntax which will be used in 
						regex_exbuild. If it is needed, this can be used more times in your script.
	*syntax - This is a number from 1 to 8. I defined those in this include but if u're too lazy to scroll, let me help:
	**#define SYNTAX_PERL 					1
	**#define SYNTAX_RUBY 					2
	**#define SYNTAX_JAVA 					3
	**#define SYNTAX_GNU 					4
	**#define SYNTAX_GREP 					5
	**#define SYNTAX_EMACS 					6
	**#define SYNTAX_POSIX_EXTENDED 		7
	**#define SYNTAX_POSIX_BASIC 			8

->regex_exbuild(const RegularExpression[]) - This function builds the regular expression and returns an unique number from 0-100.
										It is important that you store that number in a Global Variable so you can use it 
										anywhere in your script. As you can figure out, there's a limit of 100
										regex expressions that can be built. Also, if you build the expressions with this,
										it will use the syntax that you specified in regex_syntax. If none was specified, it
										will use the RUBY syntax. As character encoding, it uses UTF8.
	*const RegularExpression[] - The string that contains the Regular Expression(RegEx) syntax that will be built
	**Returns an unique number from 0-100 wich you'll need to store in an g-variable.

->regex_set(const string[], RegularExpression) - Modifies an already created(with regex_exbuild) regex.
	*const string[] - The string that contains the Regular Expression(RegEx) syntax.
	**RegularExpression - The number(returned from regex_exbuild) that indicates the regex construction you want to change.

->regex_exmatch(const string[], RegularExpression) - Same as regex_match, only that it uses a regex syntax already created with
													regex_exbuild. This is way faster.
													
->regex_exsearch(const string[], RegularExpression, &startpos, &endpos) - Same as regex_search, only that it uses a regex syntax
																		already created with regex_exbuild. This is way faster.

->regex_exreplace(const string[], RegularExpression, const replacer[]) - Same as regex_replace, only that it uses a regex syntax
																		already created with regex_exbuild. This is way faster.
																		
->regex_exreplaceall(const string[], RegularExpression, const replacer[]) - Same as regex_exreplace, only that this replaces
																		all the substrings that are matching the regex. This
																		function is a simple define wich you can see up in this
																		include. This function stands only as an example. You
																		can do one on your own and use it.

****Added since v0.1 - I left those in the plugin only for testing purpose. I recommend you use the ones that i added in v0.2

->regex_match(const string[], const RegularExpression[]) - Check if a string matches a regex.
	*const string[] - The string that you want to check.
	*const RegularExpression[] - The string that contains the Regular Expression(RegEx) syntax.
	**This function normally returns the length of the matching string. So the correct statement to check if a string
	 matches a regular expression is "if(regex_match(string, regex)>=0)".
	***In case that this function won't function correctly, it will return a value < 0 and you'll see an error in the
	  server log, that describes the problem. (Ex: "[REGEX ERROR]: unmatched close parenthesis").
	  
->regex_search(const string[], const RegularExpression[], &startpos, &endpos) - Get the first and the last position of the first sub-string of the string,
																				that matches the regex.
	*const string[] - The string that you want to search in
	*const RegularExpression[] - The string that contains the Regular Expression(RegEx) syntax.
	*&startpos - An integer variable passed by reference that will contain the first(start) position of the first sub-string that matches
	 the search. If none is found, it will be -1.
	*&endpos - An integer variable passed by reference that will contain the last(end) position of the first sub-string that matches
	 the search. If none is found, it will be -1.
	**This function normally returns 1.
	***In case that this function won't function correctly, it will return -1 and you'll see an error in the
	  server log, that describes the problem. (Ex: "[REGEX ERROR]: unmatched close parenthesis").
	  
->regex_replace(const string[], const RegularExpression[], const replacer[]) - Replace a sub-string of a string
	*const string[] - The string that you want to replace in
	*const RegularExpression[] - The string that contains the Regular Expression(RegEx) syntax.
	*const replacer[] - The string that you want to replace the sub-string found with.
	**This function normally returns the first position of the matching sub-string.
	***In case that this function won't function correctly, it will return -1 and you'll see an error in the
	  server log, that describes the problem. (Ex: "[REGEX ERROR]: unmatched close parenthesis").
	****The Oniguruma library is not providing a default function to do this, so i implemented a basic one
	   by myself.
Example:
pawn Code:
new
    SrIP[]=".*\\d{1,3}\\.+\\d{1,3}\\.+\\d{1,3}\\.+\\d{1,3}.*",
    rIP,
    startpos,
    endpos,
    string[]="This string 123.123.123.123 contains an ip.",
    replace[]="does not";
   
public OnGameModeInit()
{
    regex_syntax(SYNTAX_PERL);//We're setting the default syntax to PERL since i am used to it.
    rIP=regex_exbuild(SrIP);
    if(regex_match(string, SrIP)>=0)//note that this function does not return true/false. Check the info
    {
        print("Wooohoooo. Now i know that my string matches my regex syntax.");
    }
    if(regex_exmatch(string, rIP)>=0)
    {
        print("Wooohoooo. Now i know that my string matches my regex syntax. And i know this even faster!");
    }
    regex_search(string, SrIP, startpos, endpos);
    if(startpos>=0)
    {
        printf("Wooohoooo. Now i know that my string contains an ip from %d to %d.", startpos, endpos);
    }
    regex_exsearch(string, rIP, startpos, endpos);
    if(startpos>=0)
    {
        printf("Wooohoooo. Now i know that my string contains an ip from %d to %d. And i know this even faster!", startpos, endpos);
    }
    if(regex_replace(string, SrIP, replace)>=0)//Note how my replace is not longer than the substring it will replace!
    {
        printf("Wooohoooo. Now i know that my string does not contain an ip anymore.\nNow my string is \"%s\"", string);
        string="This string 123.123.123.123 contains an ip.";
    }
    if(regex_exreplace(string, rIP, replace)>=0)
    {
        printf("Wooohoooo. Now i know that my string does not contain an ip anymore. And i know this even faster!\nNow my string is \"%s\"", string);
    }
    return 1;
}

public OnGameModeExit()
{
    regex_end();//I no longer plan to use it so i'll end it in order to avoid memory leakage !IMPORTANT TO USE IT
    return 1;
}
Download&Install
Download url: https://github.com/FF-Koala/Regular-...lugin/releases
Source code:https://github.com/FF-Koala/Regular-Expressions-Plugin

Instalation is simple. Just copy the plugin in the "plugins" folder and the include in the pawno/includes. The file "onig.dll"(windows) or "libonig.so.2"(linux) in to the root folder. Add libRegEx\libRegEx.so in server.cfg
**If you have problems (usually on linux x64 it won't find the library event if it is there. i encountered this on debian) i compiled a static version of this plugin. You can use it.
Bugs, credits and links
If you use one to three special characters like "." or "\w" etc. alone, you should always specify the length. Like ".+" or ".*" or "\w{1,3}". This does not bugs always, but i thought it's better that you know. I am currently making some tests on this. It won't bother you if you don't plan using alone special characters without specified-length (I don't know why you'd do that).
I don't know/encountered any other bugs. If you find any, let me know by replying in this topic or send me a private message.

Credits goes to:
-maddinat0r (Got the SDK from his github)
-Incognito (I learned a lot from his github)
-K.Kosako (Author of Oniguruma library)

Useful links:
http://www.geocities.jp/kosako3/oniguruma/
http://en.wikipedia.org/wiki/Regular_expression
http://www.regexr.com/
Reply


Messages In This Thread
Regular Expressions Plugin v0.2 (RegEx) - by Koala818 - 19.07.2014, 00:54
Re: Regular Expressions Plugin v0.2 (RegEx) - by Kar - 19.07.2014, 17:39
Re : Regular Expressions Plugin v0.2 (RegEx) - by S4t3K - 19.07.2014, 18:14
Re: Regular Expressions Plugin v0.2 (RegEx) - by iFarbod - 19.07.2014, 18:24
Re: Regular Expressions Plugin v0.2 (RegEx) - by Koala818 - 19.07.2014, 20:15
Re: Regular Expressions Plugin v0.2 (RegEx) - by Lorenc_ - 20.07.2014, 03:48
Re: Regular Expressions Plugin v0.2 (RegEx) - by DRIFT_HUNTER - 20.07.2014, 06:01
Re: Regular Expressions Plugin v0.2 (RegEx) - by Koala818 - 20.07.2014, 08:01
Re: Regular Expressions Plugin v0.2 (RegEx) - by nGen.SoNNy - 20.07.2014, 08:35
Re: Regular Expressions Plugin v0.2 (RegEx) - by iFarbod - 20.07.2014, 09:34
Re: Regular Expressions Plugin v0.2 (RegEx) - by Lorenc_ - 20.07.2014, 10:35
Re: Regular Expressions Plugin v0.2 (RegEx) - by Koala818 - 20.07.2014, 12:43
Re: Regular Expressions Plugin v0.2 (RegEx) - by Kar - 20.07.2014, 17:30
Re: Regular Expressions Plugin v0.2 (RegEx) - by BeckzyBoi - 04.09.2014, 16:28
Re: Regular Expressions Plugin v0.2 (RegEx) - by StreetGT - 04.09.2014, 16:43
Re: Regular Expressions Plugin v0.2 (RegEx) - by BeckzyBoi - 04.09.2014, 17:45
Re: Regular Expressions Plugin v0.2 (RegEx) - by 0x41726d79 - 18.10.2014, 18:41
Re: Regular Expressions Plugin v0.2 (RegEx) - by iLuXing - 20.12.2014, 06:15
Re: Regular Expressions Plugin v0.2 (RegEx) - by Moron - 31.03.2016, 11:35
Re: Regular Expressions Plugin v0.2 (RegEx) - by Thyphon - 10.04.2016, 16:36
Re: Regular Expressions Plugin v0.2 (RegEx) - by maksie23 - 29.04.2016, 12:17
Re: Regular Expressions Plugin v0.2 (RegEx) - by MerryDeer - 09.09.2016, 07:12
Re: Regular Expressions Plugin v0.2 (RegEx) - by SickAttack - 09.09.2016, 07:36

Forum Jump:


Users browsing this thread: 1 Guest(s)