[Plugin] Pawn.Regex - full support of regular expressions
#1

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_DEFAULTE_REGEX_GRAMMAR:grammar REGEX_ECMASCRIPT);
native Regex_Delete(&Regex:r);
native Regex_Check(const str[], Regex:rE_MATCH_FLAG:flags MATCH_DEFAULT);
native Regex_Match(const str[], Regex:r, &RegexMatch:mE_MATCH_FLAG:flags MATCH_DEFAULT);
native Regex_Search(const str[], Regex:r, &RegexMatch:m, &posstartpos 0E_MATCH_FLAG:flags MATCH_DEFAULT);
native Regex_Replace(const str[], Regex:r, const fmt[], dest[], E_MATCH_FLAG:flags MATCH_DEFAULTsize sizeof dest);
native Match_GetGroup(RegexMatch:mindexdest[], &lengthsize sizeof dest);
native Match_Free(&RegexMatch:m); 
Examples:
PHP Code:
#include <Pawn.Regex>
stock IsRpNickname(nickname[])
{
    new 
Regex:Regex_New("[A-Z][a-z]+_[A-Z][a-z]+");
    new 
check Regex_Check(nicknamer);
    
Regex_Delete(r);
    return 
check;
}
public 
OnPlayerCommandText(playeridcmdtext[])
{
    new
        
Regex:Regex_New("^\\/([\\w]+)\\s*(.+?)?\\s*$"),
        
RegexMatch:m,
        
cmd[16], params[256],
        
cmd_lengthparams_length;
    if (
Regex_Match(cmdtextrm))
    {
        
Match_GetGroup(m1cmdcmd_length);
        
Match_GetGroup(m2paramsparams_length);
        
Match_Free(m);
    }
    
printf("cmd '%s' (%d), params '%s' (%d)"cmdcmd_lengthparamsparams_length);
    
Regex_Delete(r);
    return 
1;
}
stock SplitAndPrint(str[])
{
    new 
Regex:Regex_New("[^\\s]+");
    if (
r)
    {
        new 
RegexMatch:m;
        new 
startpospos;
        while (
Regex_Search(strrmposstartpos))
        {
            new 
word[128], length;
            if (!
Match_GetGroup(m0wordlength))
            {
                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:Regex_New(regexp);
    if (
r)
    {
        
Regex_Replace(strrfmtdestMATCH_DEFAULTsize);
        
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
Reply
#2

You are Incredible
Reply
#3

Such wonderrful plugin , good job mate
Reply
#4

Another Awesome Release by You,
YourShadow.
Reply
#5

Quote:
Originally Posted by Ralfie
View Post
Are we supposed to regex_search to detect occurrences in the middle of strings?
Yes.
Quote:
Originally Posted by Ralfie
View Post
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).
Reply
#6

i can check Valid Email string with this?
Reply
#7

Quote:
Originally Posted by Gasman
View Post
i can check Valid Email string with this?
Of course.
Reply
#8

Pawn.Regex was updated to version 1.1.

- Fixed bugs.
- Changed native "match_get_group"
Reply
#9

Nice I guess, But what makes this so much better than strfind? All difference I can see are it's cleaner.
Reply
#10

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.
Reply
#11

Quote:
Originally Posted by Salik
View Post
Actually, i got this message in my project, Fix this pls.
Fixed.
Reply
#12

Quote:
Originally Posted by YourShadow
View Post
Fixed.
Thanks!
Reply
#13

Good job!
Reply
#14

Updated.

1.1.2:
- Changed naming conventions
- Added pawn.json
- Improved OnPlayerCommandText example
Reply
#15

How can I block IP advertisements using this?
Reply
#16

Quote:
Originally Posted by sampkinq
View Post
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.
Reply
#17

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
Reply
#18

Yep.
Reply
#19

Quote:
Originally Posted by Kar
View Post
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.
Reply
#20

IsPasswordCorrect(szPassword[])
{
static Regex:rPASS;

if(!rPASS) rPASS = Regex_New("^[a-zA-Z0-9!@#\\$%\\^&*()_+-\\[\\];\\\\<>,\\.\\/?`~\"]{5,30}+$");

return Regex_Check(szPassword, rPASS);
}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)