Pawn.Regex - full support for regular expressions -
YourShadow - 14.06.2016
Pawn.Regex 1.1.3
Description:
Plugin that adds support for regular expressions in Pawn.
Why is it better than others plugins? Because it gives you an opportunity to get match groups.
Natives:
PHP Code:
native Regex:Regex_New(const pattern[], E_REGEX_FLAG:flags = REGEX_DEFAULT, E_REGEX_GRAMMAR:grammar = REGEX_ECMASCRIPT);
native Regex_Delete(&Regex:r);
native Regex_Check(const str[], Regex:r, E_MATCH_FLAG:flags = MATCH_DEFAULT);
native Regex_Match(const str[], Regex:r, &RegexMatch:m, E_MATCH_FLAG:flags = MATCH_DEFAULT);
native Regex_Search(const str[], Regex:r, &RegexMatch:m, &pos, startpos = 0, E_MATCH_FLAG:flags = MATCH_DEFAULT);
native Regex_Replace(const str[], Regex:r, const fmt[], dest[], E_MATCH_FLAG:flags = MATCH_DEFAULT, size = sizeof dest);
native Match_GetGroup(RegexMatch:m, index, dest[], &length, size = sizeof dest);
native Match_Free(&RegexMatch:m);
Examples:
PHP Code:
#include <Pawn.Regex>
stock IsRpNickname(nickname[])
{
new Regex:r = Regex_New("[A-Z][a-z]+_[A-Z][a-z]+");
new check = Regex_Check(nickname, r);
Regex_Delete(r);
return check;
}
public OnPlayerCommandText(playerid, cmdtext[])
{
new
Regex:r = Regex_New("^\\/([\\w]+)\\s*(.+?)?\\s*$"),
RegexMatch:m,
cmd[16], params[256],
cmd_length, params_length;
if (Regex_Match(cmdtext, r, m))
{
Match_GetGroup(m, 1, cmd, cmd_length);
Match_GetGroup(m, 2, params, params_length);
Match_Free(m);
}
printf("cmd '%s' (%d), params '%s' (%d)", cmd, cmd_length, params, params_length);
Regex_Delete(r);
return 1;
}
stock SplitAndPrint(str[])
{
new Regex:r = Regex_New("[^\\s]+");
if (r)
{
new RegexMatch:m;
new startpos, pos;
while (Regex_Search(str, r, m, pos, startpos))
{
new word[128], length;
if (!Match_GetGroup(m, 0, word, length))
{
break;
}
printf("word: %s", word);
startpos += pos + length;
Match_Free(m);
}
Regex_Delete(r);
}
}
stock ReplaceString(const str[], const regexp[], const fmt[], dest[], size = sizeof dest)
{
new Regex:r = Regex_New(regexp);
if (r)
{
Regex_Replace(str, r, fmt, dest, MATCH_DEFAULT, size);
Regex_Delete(r);
}
}
main()
{
new str[128];
ReplaceString("Pawn.CMD", "CMD", "Regex", str);
printf("%s", str);
SplitAndPrint("4 8 15 16 23 42");
OnPlayerCommandText(-1, "/ban 42");
OnPlayerCommandText(-1, "/kill");
printf("%d %d", IsRpNickname("Your_Shadow"), IsRpNickname("urShadow"));
}
Changelog:
1.0:
- First release
1.1:
- Fixed bugs
- Changed native "match_get_group"
1.1.1:
- Fixed error "File or function is not found"
1.1.2:
- Changed naming conventions
- Added pawn.json
- Improved OnPlayerCommandText example
1.1.3:
- Fixed replacement null length
- Fixed multibyte strings
- Completely rewritten using samp-ptl
Download binaries:
https://github.com/urShadow/Pawn.Regex/releases
Source code:
https://github.com/urShadow/Pawn.Regex
Re: Pawn.Regex - full support of regular expressions -
Max_Andolini - 14.06.2016
You are Incredible
Re: Pawn.Regex - full support of regular expressions -
Roozevelt - 14.06.2016
Such wonderrful plugin , good job mate
Re: Pawn.Regex - full support of regular expressions -
FuNkYTheGreat - 14.06.2016
Another Awesome Release by You,
YourShadow.
Re: Pawn.Regex - full support of regular expressions -
YourShadow - 17.06.2016
Quote:
Originally Posted by Ralfie
Are we supposed to regex_search to detect occurrences in the middle of strings?
|
Yes.
Quote:
Originally Posted by Ralfie
Can't we simply use regex_check?
|
The entire target sequence must match the regular expression for this function to return true (i.e., without any additional characters before or after the match).
Re: Pawn.Regex - full support of regular expressions -
Gasman - 24.06.2016
i can check Valid Email string with this?
Re: Pawn.Regex - full support of regular expressions -
YourShadow - 24.06.2016
Quote:
Originally Posted by Gasman
i can check Valid Email string with this?
|
Of course.
Re: Pawn.Regex - full support of regular expressions -
YourShadow - 03.07.2016
Pawn.Regex was updated to version 1.1.
- Fixed bugs.
- Changed native "match_get_group"
Re: Pawn.Regex - full support of regular expressions -
Luicy. - 03.07.2016
Nice I guess, But what makes this so much better than strfind? All difference I can see are it's cleaner.
Re: Pawn.Regex - full support of regular expressions -
Arturo226 - 03.07.2016
I've downloaded 1.1, however I get the following on server launch:
Code:
Plugin::AmxLoad: .inc-file version does not equal the plugin's version
Run time error 19: "File or function not found"
I am running this on a Windows server.
Re: Pawn.Regex - full support of regular expressions -
YourShadow - 09.07.2017
Quote:
Originally Posted by Salik
Actually, i got this message in my project, Fix this pls.
|
Fixed.
Re: Pawn.Regex - full support of regular expressions -
Salik - 10.07.2017
Quote:
Originally Posted by YourShadow
Fixed.
|
Thanks!
Re: Pawn.Regex - full support of regular expressions -
Barnwell - 18.07.2017
Good job!
Re: Pawn.Regex - full support of regular expressions -
YourShadow - 24.03.2018
Updated.
1.1.2:
- Changed naming conventions
- Added pawn.json
- Improved OnPlayerCommandText example
Re: Pawn.Regex - full support of regular expressions -
sampkinq - 25.03.2018
How can I block IP advertisements using this?
Re: Pawn.Regex - full support of regular expressions -
Kaperstone - 25.03.2018
Quote:
Originally Posted by sampkinq
How can I block IP advertisements using this?
|
I hope you understand that you cannot completely eliminate the issue with this plugin, you can avoid most of it, but people will still find innovative ways to advertise.
As for the code, learn how to use Regex.
Re: Pawn.Regex - full support of regular expressions -
Kar - 29.03.2018
Is this correct usage of this plugin's functions?
pawn Code:
#if defined _pawnregex_included
regex_IsValidIP(const string[])
{
// = \\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)+([(/|,.)?\\s]{1,})+(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)+([(/|,.)?\\s]{1,})+(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)+([(/|,.)?\\s]{1,})+(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\b // Pooh7's second
// = ([0-9\\s]{1,})+([(\/|_,.)?\\s]{1,})+([0-9\\s]{1,})+([(\/|_,.)?\\s]{1,})+([0-9\\s]{1,})+([(\/|_,.)?\\s]{1,})+([0-9\\s]{1,}) Pooh7's
// = (.*?)([0-9]{1,3})\\.([0-9]{1,3})\\.([0-9]{1,3})\\.([0-9]{1,3})(.*?)
// = ((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)([\s\.\$]+)){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\s*:\s*[0-9]+)?
new
result,
regex:ipRegex = Regex_New("((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)([\s\.\$\,\_\-\@\!\#\+\=\<\>]+)){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\s*:\s*[0-9]+)??");
result = Regex_Check(string, ipRegex);
Regex_Delete(ipRegex);
return result;
}
#endif
Re: Pawn.Regex - full support of regular expressions -
Evocator - 29.03.2018
Yep.
Re: Pawn.Regex - full support of regular expressions -
Kaperstone - 30.03.2018
Quote:
Originally Posted by Kar
Is this correct usage of this plugin's functions?
pawn Code:
#if defined _pawnregex_included regex_IsValidIP(const string[]) { // = \\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)+([(/|,.)?\\s]{1,})+(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)+([(/|,.)?\\s]{1,})+(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)+([(/|,.)?\\s]{1,})+(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\b // Pooh7's second // = ([0-9\\s]{1,})+([(\/|_,.)?\\s]{1,})+([0-9\\s]{1,})+([(\/|_,.)?\\s]{1,})+([0-9\\s]{1,})+([(\/|_,.)?\\s]{1,})+([0-9\\s]{1,}) Pooh7's // = (.*?)([0-9]{1,3})\\.([0-9]{1,3})\\.([0-9]{1,3})\\.([0-9]{1,3})(.*?) // = ((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)([\s\.\$]+)){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\s*:\s*[0-9]+)?
new result, regex:ipRegex = Regex_New("((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)([\s\.\$\,\_\-\@\!\#\+\=\<\>]+)){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\s*:\s*[0-9]+)??"); result = Regex_Check(string, ipRegex); Regex_Delete(ipRegex); return result; } #endif
|
No, you'd need to escape once more the escapes.
The first escape should be for regex, if you leave it that way, you're escaping the chars in Pawn and it will send the regex without the escapes \
The second escape will escape from making the escapes disappear \\ and it will send the regex query with the escape \ and will work fine.
In other words, the first for pawn, the second for regex.
Re: Pawn.Regex - full support of regular expressions -
YourShadow - 14.04.2018
IsPasswordCorrect(szPassword[])
{
static Regex:rPASS;
if(!rPASS) rPASS = Regex_New("^[a-zA-Z0-9!@#\\$%\\^&*()_+-\\[\\];\\\\<>,\\.\\/?`~\"]{5,30}+$");
return Regex_Check(szPassword, rPASS);
}