SA-MP Forums

SA-MP Forums (https://forum.sa-mp.com/index.php)
-   Plugin Development (https://forum.sa-mp.com/forumdisplay.php?f=18)
-   -   [Plugin] Pawn.Regex - full support of regular expressions (https://forum.sa-mp.com/showthread.php?t=609570)

YourShadow 14/06/2016 07:11 AM

Pawn.Regex - full support for regular expressions
 
Pawn.Regex 1.1.2


Description:
Pawn.Regex is a plugin that allows you to work with regular expressions.

Why is it better than others plugins? Because it gives you an opportunity to get match results.

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

Download binaries:
https://github.com/urShadow/Pawn.Regex/releases

Source code:
https://github.com/urShadow/Pawn.Regex

Max_Andolini 14/06/2016 03:59 PM

Re: Pawn.Regex - full support of regular expressions
 
You are Incredible :D

Roozevelt 14/06/2016 05:20 PM

Re: Pawn.Regex - full support of regular expressions
 
Such wonderrful plugin , good job mate

FuNkYTheGreat 14/06/2016 05:47 PM

Re: Pawn.Regex - full support of regular expressions
 
Another Awesome Release by You,
YourShadow.

YourShadow 17/06/2016 07:35 AM

Re: Pawn.Regex - full support of regular expressions
 
Quote:

Originally Posted by Ralfie (Post 3718221)
Are we supposed to regex_search to detect occurrences in the middle of strings?

Yes.
Quote:

Originally Posted by Ralfie (Post 3718221)
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).

Gasman 24/06/2016 05:26 AM

Re: Pawn.Regex - full support of regular expressions
 
i can check Valid Email string with this?

YourShadow 24/06/2016 06:29 AM

Re: Pawn.Regex - full support of regular expressions
 
Quote:

Originally Posted by Gasman (Post 3722419)
i can check Valid Email string with this?

Of course.

YourShadow 03/07/2016 06:32 PM

Re: Pawn.Regex - full support of regular expressions
 
Pawn.Regex was updated to version 1.1.

- Fixed bugs.
- Changed native "match_get_group"

Luicy. 03/07/2016 07:22 PM

Re: Pawn.Regex - full support of regular expressions
 
Nice I guess, But what makes this so much better than strfind? All difference I can see are it's cleaner.

Arturo226 03/07/2016 08:01 PM

Re: Pawn.Regex - full support of regular expressions
 
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.


All times are GMT. The time now is 04:34 AM.

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