SA-MP Forums Archive
[Plugin] Regular Expressions Plugin v0.2 (RegEx) - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Plugin Development (https://sampforum.blast.hk/forumdisplay.php?fid=18)
+--- Thread: [Plugin] Regular Expressions Plugin v0.2 (RegEx) (/showthread.php?tid=526725)

Pages: 1 2


Regular Expressions Plugin v0.2 (RegEx) - Koala818 - 19.07.2014

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/



Re: Regular Expressions Plugin v0.2 (RegEx) - Kar - 19.07.2014

Finally! A new plugin, thanks. I will test and get back to you!


Re : Regular Expressions Plugin v0.2 (RegEx) - S4t3K - 19.07.2014

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 : https://sampforum.blast.hk/showthread.php?tid=247893

But a new up to date version is not refused. Keep it up.


Re: Regular Expressions Plugin v0.2 (RegEx) - iFarbod - 19.07.2014

Good work, This one is better than that one above ^. +1 REP, And i am using it to validate email addresses!


Re: Regular Expressions Plugin v0.2 (RegEx) - Koala818 - 19.07.2014

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.


Re: Regular Expressions Plugin v0.2 (RegEx) - Lorenc_ - 20.07.2014

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

Again, cheers.


Re: Regular Expressions Plugin v0.2 (RegEx) - DRIFT_HUNTER - 20.07.2014

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?


Re: Regular Expressions Plugin v0.2 (RegEx) - Koala818 - 20.07.2014

@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


Re: Regular Expressions Plugin v0.2 (RegEx) - nGen.SoNNy - 20.07.2014

I'm sure i'm gonna test and use it.


Re: Regular Expressions Plugin v0.2 (RegEx) - iFarbod - 20.07.2014

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


Re: Regular Expressions Plugin v0.2 (RegEx) - Lorenc_ - 20.07.2014

The syntax function overall complicates things and necessarily isn't a 'user-friendly' addition.

Trying some of this stuff out, but I just have a feeling that it's not going to be as good as the other plugin...?


Re: Regular Expressions Plugin v0.2 (RegEx) - Koala818 - 20.07.2014

Quote:
Originally Posted by Lorenc_
Посмотреть сообщение
The syntax function overall complicates things and necessarily isn't a 'user-friendly' addition.

Trying some of this stuff out, but I just have a feeling that it's not going to be as good as the other plugin...?
You can or you can not use that function. If you won't use it, regex will build with SYNTAX_RUBY. That function was meant for people who need it.
I won't say that this plugin is better or not, because it's not fair to compete with a plugin that's no longer working at. I just gave you all the functionality that this plugin gives you and i also told why i was unpleased with the last plugin. You can test both and use whatever you like.

@iFarbod, yes and not only IPs. I have like 4-5 regexes that matches ip's, ports, websites, other known comunities names and so on. But those must be built carefully since you can detect phrases wich are not ads. Like: "Hei, i just bought that car for 1.000.000.000$"


Re: Regular Expressions Plugin v0.2 (RegEx) - Kar - 20.07.2014

Koala818 is right about the other plugin, it crashes if a player enters not so hot characters. If this plugin has the same functionality, and doesn't crash from characters then it is BETTER than the other one.


Re: Regular Expressions Plugin v0.2 (RegEx) - beckzy - 04.09.2014

How would I use this plugin to check if a string contains an IP address? Thanks


Re: Regular Expressions Plugin v0.2 (RegEx) - StreetGT - 04.09.2014

Good work man!


Re: Regular Expressions Plugin v0.2 (RegEx) - beckzy - 04.09.2014

pawn Код:
stock textContainsIP(const string[])
{
    static
        RegEx:rCIP
    ;

    if ( !rCIP )
    {
        rCIP = regex_build("(.*?)([0-9]{1,3})\\.([0-9]{1,3})\\.([0-9]{1,3})\\.([0-9]{1,3})(.*?)");
    }

    return regex_match_exid(string, rCIP);
}
This is what someone posted using the old regex plugin. Would this work with this plugin?


Re: Regular Expressions Plugin v0.2 (RegEx) - 0x41726d79 - 18.10.2014

the plugin still need MSVCPD100D.dll, but i have visual studio 2013 installed + the library in root server.
trolololol


Re: Regular Expressions Plugin v0.2 (RegEx) - iLuXing - 20.12.2014

Quote:
Originally Posted by 0x41726d79
Посмотреть сообщение
the plugin still need MSVCPD100D.dll, but i have visual studio 2013 installed + the library in root server.
trolololol
Yea me too.

How to fix that?

Windows 8.1

All runtime libs have been installed.

+rep for you.


Re: Regular Expressions Plugin v0.2 (RegEx) - Moron - 31.03.2016

I need this for email and username validation, and more, but I can't get it to load. I don't want to use Fro1sha's plugin, because I read it's slower and outdated, but most importantly can cause crashes under certain circumstances.

The only message I get in the console:
Код:
Loading plugin: libRegEx
  Failed.
1. I have libRegEx.dll in the plugins folder.
2. #include <libRegEx> is written in my gamemode.
3. I have onig.dll in the folder where samp-server.exe is located.

server.cfg
Код:
plugins Whirlpool sscanf libRegEx mysql
All other plugins load fine. I'm using Windows 10.


Re: Regular Expressions Plugin v0.2 (RegEx) - Thyphon - 10.04.2016

Unbeliveable Thanks!!