View Single Post
Old 14/06/2016, 08:11 AM   #1
YourShadow
Big Clucker
 
YourShadow's Avatar
 
Join Date: Apr 2016
Posts: 122
Reputation: 241
Exclamation 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

Last edited by YourShadow; 30/03/2018 at 03:10 PM.
YourShadow is offline   Reply With Quote