AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting (https://forums.alliedmods.net/forumdisplay.php?f=107)
-   -   Solved Regex MatchOffset - Does it work right ? (https://forums.alliedmods.net/showthread.php?t=337374)

Bacardi 04-16-2022 15:59

Regex MatchOffset - Does it work right ?
 
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);
            }
        }
    }



Dragokas 04-19-2022 10:09

Re: Regex MatchOffset - Does it work right ?
 
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


Bacardi 04-19-2022 12:12

Re: Regex MatchOffset - Does it work right ?
 
Quote:

Originally Posted by Dragokas (Post 2777216)
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 ()

Dragokas 04-19-2022 13:38

Re: Regex MatchOffset - Does it work right ?
 
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.

Bacardi 04-19-2022 18:22

Re: Regex MatchOffset - Does it work right ?
 
Aaa ok, I got confused with matches and captures. I need practise those again.

asherkin 04-21-2022 10:22

Re: Regex MatchOffset - Does it work right ?
 
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.

Bacardi 04-21-2022 12:10

Re: Regex MatchOffset - Does it work right ?
 
Thaw would make sense.

By reducing offset with strlen of match string, can get matching start offset.


All times are GMT -4. The time now is 04:02.

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