While experimenting with regex I've noticed that the function MatchRegex doesn't work as expected. And what I mean by this is that it only returns the first match.
Is there a way to enable global matching (modifier "/g") so that it returns all occurences within the string, instead of only the first one?
Code used:
PHP Code:
#include <sourcemod>
#include <sdktools>
#include <regex>
#pragma semicolon 1
#pragma newdecls required
Regex regex;
public void OnPluginStart()
{
regex = CompileRegex("[^a-z 1-9#\\.]+", PCRE_CASELESS|PCRE_UTF8);
if (!regex)
{
SetFailState("Invalid regex pattern");
}
RegConsoleCmd("sm_test", Command_Test);
}
public Action Command_Test(int client, int args)
{
char test[100] = "« • # . 123 Test";
char substr[100];
int captures = MatchRegex(regex, test);
if (captures > 0)
{
for (int i = 0; i < captures; i++)
{
bool match = GetRegexSubString(regex, 0, substr, sizeof(substr));
if (!match)
{
break;
}
else ReplaceString(test, sizeof(test), substr, "", false);
}
}
PrintToChat(client, "Captures: %d", captures);
PrintToChat(client, "String: %s", test);
return Plugin_Handled;
}
In-game debugging:
Quote:
Captures: 1
String: • # . 123 Test
|