SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Plugin Development

Reply
 
Thread Tools Display Modes
Old 19/07/2014, 12:54 AM   #1
Koala818
Big Clucker
 
Koala818's Avatar
 
Join Date: Apr 2014
Location: [+!+[]]+[!+[]+!+[]+!+[]]+[!+[]+!+[]+!+[]]+[!+[]+!+[]+!+[]+!+[]+!+[]+!+[]+!+[]]
Posts: 118
Reputation: 9
Default Regular Expressions Plugin v0.2 (RegEx)

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/
__________________
Koala818 is offline   Reply With Quote
Old 19/07/2014, 05:39 PM   #2
Kar
Banned
 
Join Date: May 2010
Location: Black Asylum
Posts: 2,992
Reputation: 538
Default Re: Regular Expressions Plugin v0.2 (RegEx)

Finally! A new plugin, thanks. I will test and get back to you!
Kar is offline   Reply With Quote
Old 19/07/2014, 06:14 PM   #3
S4t3K
Gangsta
 
S4t3K's Avatar
 
Join Date: Jan 2013
Posts: 870
Reputation: 165
Default Re : Regular Expressions Plugin v0.2 (RegEx)

Nice job, I haven't got yet the point of regular expressions, but it's true that I don't search so much on my side.

Anyway, a plugin like this already exists here : http://forum.sa-mp.com/showthread.php?t=247893

But a new up to date version is not refused. Keep it up.
__________________
S4t3K is offline   Reply With Quote
Old 19/07/2014, 06:24 PM   #4
iFarbod
Gangsta
 
Join Date: Jan 2014
Location: Los Santos (HD Universe)
Posts: 655
Reputation: 59
Default Re: Regular Expressions Plugin v0.2 (RegEx)

Good work, This one is better than that one above ^. +1 REP, And i am using it to validate email addresses!
iFarbod is offline   Reply With Quote
Old 19/07/2014, 08:15 PM   #5
Koala818
Big Clucker
 
Koala818's Avatar
 
Join Date: Apr 2014
Location: [+!+[]]+[!+[]+!+[]+!+[]]+[!+[]+!+[]+!+[]]+[!+[]+!+[]+!+[]+!+[]+!+[]+!+[]+!+[]]
Posts: 118
Reputation: 9
Default Re: Regular Expressions Plugin v0.2 (RegEx)

Thanks for feedback :d. This can have more uses. Beside email validation, i use it for detecting the advertisments made by players on my server. So each time a player uses the chat, this is used.
__________________
Koala818 is offline   Reply With Quote
Old 20/07/2014, 03:48 AM   #6
Lorenc_
High-roller
 
Lorenc_'s Avatar
 
Join Date: Jan 2010
Location: Australia
Posts: 3,802
Reputation: 1160
Default Re: Regular Expressions Plugin v0.2 (RegEx)

Cheers, I'll give this a shot. The other one has some issues with regex_replace, hopefully this fixes them.

Again, cheers.
__________________
Join the best Cops And Robbers in SA-MP, today. svr.sfcnr.com:7777

Lorenc_ is offline   Reply With Quote
Old 20/07/2014, 06:01 AM   #7
DRIFT_HUNTER
High-roller
 
Join Date: Oct 2009
Posts: 2,070
Reputation: 184
Default Re: Regular Expressions Plugin v0.2 (RegEx)

Well i always used other one (mainly because its the only one) and i must say i never had problems (But i also didnt use it to much). I was using it only for detection and validations of IP,MAIL,RP and WEBSITE NAMES....Never had a problem...

Dont see a reason for these but since regex pattern stays same every time and we just need to change few functions why now?
DRIFT_HUNTER is offline   Reply With Quote
Old 20/07/2014, 08:01 AM   #8
Koala818
Big Clucker
 
Koala818's Avatar
 
Join Date: Apr 2014
Location: [+!+[]]+[!+[]+!+[]+!+[]]+[!+[]+!+[]+!+[]]+[!+[]+!+[]+!+[]+!+[]+!+[]+!+[]+!+[]]
Posts: 118
Reputation: 9
Default Re: Regular Expressions Plugin v0.2 (RegEx)

@DRIFT_HUNTER, as i said before, i used that plugin for detecting the advertisements made by players on my server, so that plugin was tested a lot. After i built some nice and working syntaxes i set up a system on my server that would verify anything the players would type. I observed that sometimes my server crashes. After i used crash_detect i found out that if a player would type something in the chat, the plugin that would verify what he typed would crash and so would my server. I thought it's about the plugin, but i looked up the source code and i didn't really so something wrong, so i made a little c++ console app to test the regex library that that plugin uses. And for the same syntax and for the same string it would crash. So it's not a plugin fault, but a library that it uses one (boost.regex).
I didn't wanted to loose the system i made just because of a buggy library so i looked up, learned and made this plugin that is based on another library (oniguruma) which is faster and doesn't crash. This also supports more syntaxes (RUBY, PERL, etc. see full list in the description) and more than this it supports UTF8 encoding. This means that you can detect almost any letter (Usefull for those who admit letters like "Ț, Ă ș Ș" etc. You got the point.
Now it's up to you to choose what plugin to use. I just posted this here and on github because i thought that someone who would have the same problem with the last plugin, would find this one more usefull.
Cheers and thanks for the feedback
__________________
Koala818 is offline   Reply With Quote
Old 20/07/2014, 08:35 AM   #9
nGen.SoNNy
Gangsta
 
nGen.SoNNy's Avatar
 
Join Date: Sep 2009
Location: Romania
Posts: 941
Reputation: 69
Default Re: Regular Expressions Plugin v0.2 (RegEx)

I'm sure i'm gonna test and use it.
__________________
If i've helped you, please click
nGen.SoNNy is offline   Reply With Quote
Old 20/07/2014, 09:34 AM   #10
iFarbod
Gangsta
 
Join Date: Jan 2014
Location: Los Santos (HD Universe)
Posts: 655
Reputation: 59
Default Re: Regular Expressions Plugin v0.2 (RegEx)

Quote:
Originally Posted by Koala818 View Post
Thanks for feedback :d. This can have more uses. Beside email validation, i use it for detecting the advertisments made by players on my server. So each time a player uses the chat, this is used.
Detecting an IP. For players advertise their servers.

[DIGIT (3)] . [DIGIT (3)] . [DIGIT (3)] . [DIGIT (3)] : [DIGIT (5)]
Like this
127.0.0.1:7777
iFarbod is offline   Reply With Quote
Reply

Thread Tools
Display Modes

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Regex plugin needed!? Djane Scripting Help 7 13/12/2013 10:00 AM
expressions string to expressions? iJumbo Scripting Help 7 04/05/2013 02:33 PM
[REGEX] Acceptable nickname regular expression ikkentim Scripting Help 1 29/01/2013 08:13 PM
[Plugin] mtime Time Plugin [UnixTime convert, Regular Time Convert and more] Mellnik Plugin Development 9 08/12/2012 10:16 PM


All times are GMT. The time now is 03:30 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.