View Single Post
Author Message
FAQU
Member
Join Date: Sep 2020
Location: Romania
Old 01-06-2021 , 02:07   MatchRegex stopping after the first match
Reply With Quote #1

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 clientint args)
{
    
char test[100] = "« • # . 123 Test";
    
char substr[100];
    
    
int captures MatchRegex(regextest);
    if (
captures 0)
    {
        for (
int i 0capturesi++)
        {
            
bool match GetRegexSubString(regex0substrsizeof(substr));
            
            if (!
match)
            {
                break;
            }
            else 
ReplaceString(testsizeof(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

Last edited by FAQU; 01-08-2021 at 02:24. Reason: Solved
FAQU is offline