[Plugin] Regular expression
#1

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:
Reply
#2

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).
Reply
#3

Quote:
Originally Posted by Y_Less
View Post
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
View Post
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
Reply
#4

New version

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);
Reply
#5

awesome xD
Reply
#6

nice! хорошая работа! ждал давно!

P.S. See this http://gskinner.com/RegExr/
Reply
#7

Nice, thanks!
Mandatory relevant(ish) xkcd comic strip here: http://xkcd.com/208/
Reply
#8

Heh, nice find.
Reply
#9

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

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

thanks, it is realy nice one
Reply
#12

This is exact what I need. I wasn't looking for it, but I'm gonna use this, I think.
Thank you.
Reply
#13

Isn't this more accurate and faster:
pawn Code:
stock IsValidRpNameEx(const string[])
{
    static
        RegEx:rRpName
    ;
   
    if ( !rRpName )
    {      
        rRpName = regex_build("[A-Z][a-z]+_[A-Z][a-z]{1,3}[A-Z]?[a-z]*");
    }
   
    return regex_match_exid(string, rRpName);
}
Some Roleplay names may be eg. Roberto_DeLuca.

// Fixed a little bug in the code, Thanks to Y_Less.
Reply
#14

Oh yeah, my bad. I was actually just testing it out and could not understand why wont it work.

Thanks.
Reply
#15

http://pastebin.com/u16PdHhS

See my anti AD script...
Reply
#16

I picked up a ready-made regular expression from the internet and upgraded it a bit. I basically relied on the IP address in string.
Reply
#17

Quote:
Originally Posted by Y_Less
View Post
That can be vastly simplified, plus I doubt it will work on:

Also, one of my websites uses very clever .htaccess files to do:

That is a valid web address. If you understand how web addresses are made you can improve the regex massively:

Code:
([\w-]*://)?([\w-]+\.)+([\w-]+)(/[^\s]*)*
That will match any address of the form:

Code:
optional-prefix://dot.spaced.domain/some/path.or?frankly&anything=else
If the first parts are detected anything up to a space is detected. If you need to get the parts in your results you will probably need a more advanced system, but there's nothing in your code for parsing, only detecting. As for IPs, they're dead easy.
Since \ can't work in a string, i guess, there is nothing bad if i do it like

Code:
([\92w-]*://)?([\92w-]+\92.)+([\92w-]+)(/[^\92s]*)*
Which, theoretically, \92 should change to \.
Reply
#18

Quote:
Originally Posted by Y_Less
View Post
\ in a string is \\ - you escape the escape.
Thanks!! Even if this was so dumb.
Reply
#19

Thanks, just what i was looking for!
Reply
#20

Why dont this work?
pawn Code:
#define IsValidText(%1) regex_match(%1, "[a-zA-Z0-9]")
pawn Code:
if(!IsValidText(string) return SendClientMessage(playerid, 0xFF0000FF, "Error: Only use letters from A till Z uppercase and undrcase! you can also use numbers 0 till 9");
i can only use numbers 1, 2, 3, 4, 5, 6, 7, 8, 9. if the string is "500" then i get the Error message, but i want to use 500!
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)