REGEX - YourShadow's plugin
#1

Hello.
I'm learning a bit REGEX and I tried to use the regular expression I made on pawn by using Pawn.Regex plugin (made by YourShadow - https://sampforum.blast.hk/showthread.php?tid=609570) but nothing works.

There is my expression : https://regex101.com/r/FT3L4d/1/
Here is my code:
PHP Code:
main()
{
    new
        
_result[500],
        
_result_length,
        
expr[] = "object CJ_BIG_SKIP1 (1)\" doublesided=\"false\" model=\"1365\" interior=\"0\" dimension=\"0\" posX=\"-2681.3000488281\" posY=\"865.29998779297\" posZ=\"76.400001525879\" rotX=\"0\" rotY=\"0\" rotZ=\"0\"";
    new 
Regex:reg Regex_New("[A-Z0-9_]*-?\\d+(.\\d+)?"),
        
RegexMatch:match;
    if (
Regex_Match(exprregmatch)) 
    {
        
Match_GetGroup(match1_result_result_length);
        
Match_Free(match);
    }
    
printf("- '%s' - %i"_result_result_length);
    for(new 
i!= _result_lengthi++)
        
printf("- '%s'"_result[i]);
    
Regex_Delete(reg);

Maybe I misunderstood the usage of natives functions?
Reply
#2

Uhm if i put your regex into a regex tester, it doesn't fetch anything., as you can see here: https://regexr.com/3t4hc
Reply
#3

Quote:
Originally Posted by BlackBank
View Post
Uhm if i put your regex into a regex tester, it doesn't fetch anything., as you can see here: https://regexr.com/3t4hc
You need to use
Code:
[A-Z0-9_]*-?\d+(.\d+)?
and not
Code:
[A-Z0-9_]*-?\\d+(.\\d+)?
The second one is made for pawn because of escaped characters in string.

Quote:
Originally Posted by [HLF]Southclaw
View Post
The reason regex101 is highlighting your values is because your expression is so generic that it's matching almost anything. Another thing is, don't include the escape characters in your test data. Your string in Pawn requires `\"` to simple insert `"` into the string literal. When you read the string as data the `\` character will not be there.

The data you've supplied will need a much more complex regular expression to parse. You should move left to right matching what you need and adding to it. I've gotten you started: ^\"([a-zA-Z() 0-9_-]+)\" doublesided=\"(\w+)\" model=\"(\w+)\"

But there's a bigger problem here:

You're using the wrong tool for the job. This is not a task for regular expressions. I'm guessing you're parsing MTA's map editor output? That output is XML - a very standardised format that probably has more parsers than AFK systems on this forum! IIRC ****** wrote an XML parser, Zeex has done and a few others. XML is pretty heavy to parse so I'd advise using a plugin if speed is a problem. If you're doing this on server load, you probably don't need to worry.

And finally, if you're actually just converting the code yourself - Pawn is absolutely the wrong tool for the job! You'll be surprised how fast you can accomplish things like text conversion and menial tasks like this in a language like Python, JavaScript, Ruby, Perl or even Bash.
Regex101 is actually highlighting what I want to retrieve. I guess it's doing what I want it do!
Yep, you are right. Escaped characters are needed for pawn that's why I linked a online regex tester to show you the expression without any escaped characters.

I just wanted to get the object's name, positions, rotations plus the interior. Nothing more! I don't even map, I just took the line of code from a random map found on Internet. I just wanted to train with regular expressions and do something other than detecting a RP name, a phone number or an email address. If you think that dumb to train on pawn, any advices is appreciated.

Thanks for your detailed message.
Reply
#4

Quote:
Originally Posted by ******
View Post
You have so many *s and ?s in there that basically all of the expression can be ignored:

"[A-Z0-9_]*-?\\d+(.\\d+)?"

Basically becomes:

"\\d+"

I.e. will match (but not capture) any numbers anywhere (even the (1) near the start).
I'm catching the object's name and some coordinates. Using only "\d+" doesn't take in count negative values and there is no way to know numer's type (float, int, ..).
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)