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

Solved Regex MatchOffset - Does it work right ?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Bacardi
Veteran Member
Join Date: Jan 2010
Location: mom's basement
Old 04-16-2022 , 15:59   Regex MatchOffset - Does it work right ?
Reply With Quote #1

About this https://sm.alliedmods.net/new-api/re...ex/MatchOffset
- I'm not sure do I use it right.
Should this return offset of match in string ?
It now return offset of end of match in string.

Below example match STEAM_ id pattern.
output
Code:
sm plugins reload test
"<STEAM_0:1:23456> Baca bacardi jump over brown fox STEAM_7:8:9012345678 rolling down the hill, hit his head on rock"

numofmatch 2
> Baca bacardi jump over brown fox STEAM_7:8:9012345678 rolling down the hill, hit his head on rock
matchoffset 16

 rolling down the hill, hit his head on rock
matchoffset 71

[SM] Plugin test.smx reloaded successfully.


PHP Code:

#include <regex>

Regex rextest;

char loremipsum[] = {
    
"<STEAM_0:1:23456> Baca bacardi jump over brown fox \
    STEAM_7:8:9012345678 rolling down the hill, hit his head on rock"
}


public 
void OnPluginStart()
{
    
rextest = new Regex("(STEAM_\\d:\\d:\\d+)");

    
int numofmatch rextest.MatchAll(loremipsum);
    
int matchoffset;

    if(
numofmatch 0)
    {
        
PrintToServer("\"%s\"\n"loremipsum);

        
PrintToServer("numofmatch %i"numofmatch);

        for(
int a 1numofmatcha++)
        {
            for(
int b 0rextest.CaptureCount(a); b++)
            {
                
matchoffset rextest.MatchOffset(b);
                
PrintToServer("%s"loremipsum[matchoffset]);
                
PrintToServer("matchoffset %i\n"matchoffset);
            }
        }
    }


Last edited by Bacardi; 04-21-2022 at 12:11.
Bacardi is offline
Dragokas
Veteran Member
Join Date: Nov 2017
Location: Ukraine on fire
Old 04-19-2022 , 10:09   Re: Regex MatchOffset - Does it work right ?
Reply With Quote #2

Likes a bug.

However, your double-cycle is a bit strange.
Here is more clear test:
PHP Code:
#pragma semicolon 1
#pragma newdecls required

#include <sourcemod>
#include <regex>

Regex regex;

char loremipsum[] = {
    
"<STEAM_0:1:23456> Baca bacardi jump over brown fox \
    STEAM_7:8:9012345678 rolling down the hill, hit his head on rock"
};

public 
void OnPluginStart()
{
    
char buf[128];
    
regex = new Regex("STEAM_\\d:\\d:\\d+");
    
    
int numMatches regex.MatchAll(loremipsum);
    if( 
numMatches )
    {
        
PrintToServer("Full string: \"%s\"\n"loremipsum);
        
PrintToServer("Num of matches: %i\n"numMatches);

        for(
int iMatch 0iMatch numMatchesiMatch ++)
        {
            
int offsetMatch regex.MatchOffset(iMatch);
            
regex.GetSubString(0bufsizeof(buf), iMatch);
            
            
PrintToServer("[substring:%i, offs:%i, len:%i]: %s"iMatchoffsetMatchstrlen(buf), buf);
        }
    }

Quote:
Full string: "<STEAM_0:1:23456> Baca bacardi jump over brown fox STEAM_7:8:9012345678 rolling down the hill, hit his head on rock"

Num of matches: 2

[substring:0, offs:16, len:15]: STEAM_0:1:23456
[substring:1, offs:71, len:20]: STEAM_7:8:9012345678
__________________
Expert of CMD/VBS/VB6. Malware analyst. L4D fun (Bloody Witch & FreeZone)
[My plugins] [My tools] [GitHub] [Articles] [HiJackThis+] [Donate]

Last edited by Dragokas; 04-19-2022 at 10:16.
Dragokas is offline
Bacardi
Veteran Member
Join Date: Jan 2010
Location: mom's basement
Old 04-19-2022 , 12:12   Re: Regex MatchOffset - Does it work right ?
Reply With Quote #3

Quote:
Originally Posted by Dragokas View Post
Likes a bug.

However, your double-cycle is a bit strange.
Here is more clear test:
It's from previous practising code, looking log outputs.
https://regex101.com/r/OESJ0R/1

Using Capturing Group ()
__________________
Do not Private Message @me
Bacardi is offline
Dragokas
Veteran Member
Join Date: Nov 2017
Location: Ukraine on fire
Old 04-19-2022 , 13:38   Re: Regex MatchOffset - Does it work right ?
Reply With Quote #4

Yeah, but, in such case it should be rextest.MatchOffset(a) in your code, because MatchOffset is accepting index of match, not index of capture.
Index of capture can be used with GetSubString (argument str_id) as according to docs.
One match can contain several captures. The capture is a part enclosed in parentheses. If you would have several enclosed parts in expression e.g. "(STEAM_)(\\d:\\d:\\d+)", so you'll have several captures in each match.
Anyway, it doesn't change that the result offset is a bugged value. It should point to the start of a string index where match was found.
__________________
Expert of CMD/VBS/VB6. Malware analyst. L4D fun (Bloody Witch & FreeZone)
[My plugins] [My tools] [GitHub] [Articles] [HiJackThis+] [Donate]

Last edited by Dragokas; 04-19-2022 at 13:41.
Dragokas is offline
Bacardi
Veteran Member
Join Date: Jan 2010
Location: mom's basement
Old 04-19-2022 , 18:22   Re: Regex MatchOffset - Does it work right ?
Reply With Quote #5

Aaa ok, I got confused with matches and captures. I need practise those again.
Bacardi is offline
asherkin
SourceMod Developer
Join Date: Aug 2009
Location: OnGameFrame()
Old 04-21-2022 , 10:22   Re: Regex MatchOffset - Does it work right ?
Reply With Quote #6

The docs aren't the best, but it is for implementing MatchAll yourself - it gives you the offset to start searching for the next match from. It's too late to change the implementation I think.
__________________
asherkin is offline
Bacardi
Veteran Member
Join Date: Jan 2010
Location: mom's basement
Old 04-21-2022 , 12:10   Re: Regex MatchOffset - Does it work right ?
Reply With Quote #7

Thaw would make sense.

By reducing offset with strlen of match string, can get matching start offset.
Bacardi 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 04:09.


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