Rob Pike's regex matcher -
dxhj - 08.12.2015
Hey, everyone. Here's a implementation of Rob Pike's simple regex algorithm.
The matcher handles:
c matches any literal character c
. matches any single character
^ matches the beginning of the input string
$ matches the end of the input string
* matches zero or more occurrences of the previous character
* It doesn't handle parentheses (its purpose is to be simple not a complex regex engine)
- but it's not hard to implement it, we can use a recursive descent parser or a elaborated Shunting-yard algorithm, and send the result to the matcher (next version, maybe

)
Код:
sregex_match(regexp[], text[])
Return values:
0 - doesn't match
1 - found a match
Include:
sregex.inc
Anyway, have fun.
Re: Rob Pike's regex matcher -
Crayder - 09.12.2015
Or... we just use the regex plugin, which is a lot faster and supports all of regex.
Good effort though.
Re: Rob Pike's regex matcher -
Kevln - 09.12.2015
Quote:
Originally Posted by Crayder
Or... we just use the regex plugin, which is a lot faster and supports all of regex.
Good effort though.
|
Which one exactly?
This one (
https://sampforum.blast.hk/showthread.php?tid=526725) causes crashes.
Re: Rob Pike's regex matcher -
Crayder - 09.12.2015
Quote:
Originally Posted by Kevln
|
Why choose that one over
this one? You think newer is always better? Shnot...
As long as you manage the memory right it works like a charm!
Re: Rob Pike's regex matcher -
Vince - 09.12.2015
I guess this could be an alternative to a plugin if you only need to use a regex once or twice. To me it always seems silly to load an entire plugin in order to be able to use a function once. Unfortunately, this code is GPL, which is a really crappy license because it is completely incompatible with any other license and it basically requires you to make your entire code base open source.
Re: Rob Pike's regex matcher -
SickAttack - 09.12.2015
Quote:
Originally Posted by Crayder
Why choose that one over this one? You think newer is always better? Shnot...
As long as you manage the memory right it works like a charm!
|
What's the difference?
http://forum.sa-mp.com/showpost.php?...&postcount=130
Re: Rob Pike's regex matcher -
Crayder - 10.12.2015
Quote:
Originally Posted by SickAttack
|
I've honestly never had a crash with it, or the other one.
Also. It'd be quite simple implementing a library such as
this into a new plugin. Or even ******'s RE2 library would do, but of course that shit looks a bit more complicated... xD
Re: Rob Pike's regex matcher -
dxhj - 10.12.2015
Quote:
Originally Posted by Crayder
Or... we just use the regex plugin, which is a lot faster and supports all of regex.
Good effort though.
|
I didn't run any tests, but I think this code is faster than the regex library because it doesn't use any kind of "automata simulation"(constructing states, simulating, ARGH!). Run some tests, it would be nice.
Anyway, this include doesn't mean to be better than whatever plugin you use, its purpose is to help people on simple things(that's what this area is for) without the need to use the whole plugin - as you can see, it covers a small set of operations -
Have fun!
Re: Rob Pike's regex matcher -
SickAttack - 10.12.2015
Quote:
Originally Posted by dxhj
I didn't run any tests, but I think this code is faster than the regex library because it doesn't use any kind of "automata simulation"(constructing states, simulating, ARGH!). Run some tests, it would be nice.
Anyway, this include doesn't mean to be better than whatever plugin you use, its purpose is to help people on simple things(that's what this area is for) without the need to use the whole plugin - as you can see, it covers a small set of operations -
Have fun! 
|
Could you make it process parentheses and able to execute the following?
pawn Код:
forward bool:IsAdvertisement(text[]);
public bool:IsAdvertisement(text[])
{
new message[128], expression[] = "(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.+){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)", match;
strcpy(message, text, sizeof(message));
for(new i = 0, j = strlen(message); i < j; i ++)
{
switch(message[i])
{
case '0'..'9': continue;
case '.': continue;
default:
{
strdel(message, i, i + 1);
strins(message, ".", i);
}
}
}
match = sregex_match(expression, message);
if(match) return true;
return false;
}
Re: Rob Pike's regex matcher -
dxhj - 10.12.2015
Quote:
Originally Posted by SickAttack
Could you make it process parentheses and able to execute the following?
pawn Код:
forward bool:IsAdvertisement(text[]); public bool:IsAdvertisement(text[]) { new message[128], expression[] = "(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.+){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)", match; strcpy(message, text, sizeof(message));
for(new i = 0, j = strlen(message); i < j; i ++) { switch(message[i]) { case '0'..'9': continue; case '.': continue; default: { strdel(message, i, i + 1); strins(message, ".", i); } } }
match = sregex_match(expression, message);
if(match) return true; return false; }
|
Yes. I'm thinking about a more complete version, probably not the same algorithm.