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] Regular expression (https://forum.sa-mp.com/showthread.php?t=247893)

Fro1sha 11/04/2011 09:42 AM

Regular expression 0.2
 
Description:
Wiki
boost

Natives:
pawn Code:
native RegEx:regex_build(const expression[]);
native regex_delete(RegEx:expID);
native regex_isvalid(RegEx:expID);
native regex_delete_all();

native regex_match(const string[], const expression[]);
native regex_search(const string[], const expression[]);
native regex_replace(const string[], const expression[], const to[], dest[], size = sizeof dest);

native regex_match_exid(const string[], RegEx:expID);
native regex_search_exid(const string[], RegEx:expID);
native regex_replace_exid(const string[], RegEx:expID, const to[], dest[], size = sizeof dest);

Examples:
pawn Code:
#define IsValidEmail(%1) \
    regex_match(%1, "[a-zA-Z0-9_\\.]+@([a-zA-Z0-9\\-]+\\.)+[a-zA-Z]{2,4}")
   
#define IsValidRpName(%1) \
    regex_match(%1, "([A-Z]{1,1})[a-z]{2,9}+_([A-Z]{1,1})[a-z]{2,9}")
   
#define IsValidText(%1) \
    regex_match(%1, "[ --a-zA-Z0-9_,!\\.\\?\\-\\+\\(\\)]+")

stock IsValidRpNameEx(const string[])
{
    static
        RegEx:rRpName
    ;
   
    if ( !rRpName )
    {      
        rRpName = regex_build("([A-Z]{1,1})[a-z]{2,9}+_([A-Z]{1,1})[a-z]{2,9}");
    }
   
    return regex_match_exid(string, rRpName);
}

stock IsValidTextEx(const string[])
{
    static
        RegEx:rText
    ;
   
    if ( !rText )
    {      
        rText = regex_build("[ --a-zA-Z0-9_,!\\.\\?\\-\\+\\(\\)]+");
    }
   
    return regex_match_exid(string, rText);
}
pawn Code:
stock IsValidEmailEx(const string[])
{
    static
        RegEx:rEmail
    ;
   
    if ( !rEmail )
    {      
        rEmail = regex_build("[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?");
    }
   
    return regex_match_exid(string, rEmail);
}

main()
{      
    printf("ValidEmail: %s", IsValidEmailEx("test@test.test") ? ("true") : ("false"));
    printf("Non ValidEmail: %s", !IsValidEmailEx("t[e]st@test.test") ? ("true") : ("false"));
}

Log:
  • 0.2.1
    - Fix params in (regex_delete, regex_isvalid)
  • 0.2
    - Added new natives
    pawn Code:
    native RegEx:regex_build(const expression[]);
    native regex_delete(RegEx:expID);
    native regex_isvalid(RegEx:expID);
    native regex_delete_all();

    native regex_match_exid(const string[], RegEx:expID);
    native regex_search_exid(const string[], RegEx:expID);
    native regex_replace_exid(const string[], RegEx:expID, const to[], dest[], size = sizeof dest);
  • 0.1
    - First release

Download:

Calgon 11/04/2011 10:03 AM

Re: Regular expression
 
Thank you. SO MUCH!

I've been waiting for something like this forever, I've been having to create complex (well, I say complex, in comparison to regex, but functions generally containing a billion unnecessary checks in comparison to one regex statement).

Do you mind if I mirror the download?

Mirror: http://www.fwright.com/files/regex.zip (only includes include & plugin .so + .dll).

Fro1sha 11/04/2011 10:10 AM

Re: Regular expression
 
Quote:

Originally Posted by ****** (Post 1160639)
Ahh, I was wondering if anyone would ever do this - it has been something I've wanted for a while but never enough to actually get around to producing. Excellent work. One question, could you add pre-compilation for speed, example:

pawn Code:
new RegEx:rRPName = regex_build("([A-Z]{1,1})[a-z]{2,9}+_([A-Z]{1,1})[a-z]{2,9}");

regex_match_compile(name0, rRPName);
regex_match_compile(name1, rRPName);
regex_match_compile(name2, rRPName);

Also, that example doesn't actually check for valid emails:

http://haacked.com/archive/2007/08/2...s-until-i.aspx

Good suggestion =)

Quote:

Originally Posted by Calg00ne (Post 1160649)
Thank you. SO MUCH!

I've been waiting for something like this forever, I've been having to create complex (well, I say complex, in comparison to regex, but functions generally containing a billion unnecessary checks in comparison to one regex statement).

Do you mind if I mirror the download?

Ok

Fro1sha 11/04/2011 02:06 PM

Re: Regular expression
 
New version :p

0.2
- Added new natives
pawn Code:
native RegEx:regex_build(const expression[]);
native regex_delete(RegEx:expID);
native regex_isvalid(RegEx:expID);
native regex_delete_all();

native regex_match_exid(const string[], RegEx:expID);
native regex_search_exid(const string[], RegEx:expID);
native regex_replace_exid(const string[], RegEx:expID, const to[], dest[], size = sizeof dest);

wheelman_WM 11/04/2011 02:41 PM

Re: Regular expression
 
awesome xD

Romanius 11/04/2011 03:34 PM

Re: Regular expression
 
nice! ! !

P.S. See this http://gskinner.com/RegExr/

sooky 11/04/2011 04:25 PM

Re: Regular expression
 
Nice, thanks!
Mandatory relevant(ish) xkcd comic strip here: http://xkcd.com/208/

Calgon 11/04/2011 05:41 PM

Re: Regular expression
 
Heh, nice find.

JaTochNietDan 11/04/2011 06:18 PM

Re: Regular expression
 
Ah, the beauty of regular expressions, it was only a matter of time before somebody decided to make a plugin for it for SA-MP. Thanks for the effort and submission.

armyoftwo 11/04/2011 06:39 PM

Re: Regular expression
 
Hi,
Can someone explain why this is needed and for what?
I've looked the wiki and didn't find anything that explains what is this ( or i can't properly understand ,because my english is not very good).
Can someone explain more simple?


All times are GMT. The time now is 02:45 PM.

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