In SM 1.10 and 1.9 soon(tm) You can use MatchAll to make the regex even easier. Although, using args is much better. But, since i decided to test with this type of an example... hf
PHP Code:
#pragma newdecls required
#include <sourcemod>
#include <regex>
public void OnPluginStart()
{
int buff[4];
char test_strings[][] = {"128 233 255", "123 232 22 156", "126 130 300", "257 220 300 100"};
for(int i = 0; i < sizeof(test_strings); i++)
{
int ret = CheckRGBA(test_strings[i], buff, (i % 2) ? true : false);
if(ret == 3)
{
PrintToServer("RGB values %i %i %i", buff[0], buff[1], buff[2]);
}
else if(ret == 4)
{
PrintToServer("RGBA values %i %i %i %i", buff[0], buff[1], buff[2], buff[3]);
}
else
{
PrintToServer("Invalid RGB(A) values passed");
}
}
}
int CheckRGBA(const char [] szRGB, int values[4], bool hasAlpha)
{
Regex re = new Regex("\\b([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\b");
int ret = -1;
int matches = re.MatchAll(szRGB);
if((matches == 3 && !hasAlpha) || (matches == 4 && hasAlpha))
{
ret = matches;
for(int i = 0; i < matches; i++)
{
char temp[4];
re.GetSubString(1, temp, sizeof(temp), i);
values[i] = StringToInt(temp);
}
}
delete re;
return ret;
}
And the output
Code:
RGB values 128 233 255
RGBA values 123 232 22 156
Invalid RGB(A) values passed
Invalid RGB(A) values passed
I wouldnt pay much attention to the fiasco in onpluginstart though... im hella lazy.