Raised This Month: $29 Target: $400
 7% 

Solved MatchRegex stopping after the first match


Post New Thread Reply   
 
Thread Tools Display Modes
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
FAQU
Member
Join Date: Sep 2020
Location: Romania
Old 01-06-2021 , 04:50   Re: MatchRegex stopping after the first match
Reply With Quote #2

Found a workaround by using a while loop.
PHP Code:
while (MatchRegex(regextest) > 0)
    {
        if (
GetRegexSubString(regex0substrsizeof(substr)))
        {
            
ReplaceString(testsizeof(test), substr""false);
        }
    } 
I'm still interested to know if there's another way of doing this.
FAQU is offline
StrikeR14
AlliedModders Donor
Join Date: Apr 2016
Location: Behind my PC
Old 01-07-2021 , 11:37   Re: MatchRegex stopping after the first match
Reply With Quote #3

PHP Code:
int captures regex.MatchAll(test); 
https://sourcemod.dev/#/regex/method...ction.MatchAll
__________________
Currently taking TF2/CSGO paid private requests!

My Plugins | My Discord Account
StrikeR14 is offline
FAQU
Member
Join Date: Sep 2020
Location: Romania
Old 01-08-2021 , 02:23   Re: MatchRegex stopping after the first match
Reply With Quote #4

Quote:
Originally Posted by StrikeR14 View Post
PHP Code:
int captures regex.MatchAll(test); 
https://sourcemod.dev/#/regex/method...ction.MatchAll
Thank you ! I have no ideea how I missed that part of the documentation.
FAQU is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 21:26.


Powered by vBulletin®
Copyright ©2000 - 2021, vBulletin Solutions, Inc.
Theme made by Freecode