Raised This Month: $51 Target: $400
 12% 

need help with Regular Expressions in sourcemod


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
ambn
Veteran Member
Join Date: Feb 2015
Location: Fun servers
Old 03-03-2018 , 03:11   need help with Regular Expressions in sourcemod
Reply With Quote #1

hi everyone, yesterday i tried to create a plugin that catch player's ip who attempts to SendNetMsg (flooding server using several console commands + sometimes happens on player join), so i'm catching the console message with PTaH extension and will give it as an input for this function and the result which is going to be stored into the string should be an ip but it's just an empty string, here is my code + The numbers match is always 0:

PHP Code:
bool getAttackerIp(const char[] attackInputchar[] ipOutAttackint maxlen)
{
    if(
StrContains(attackInput"SendNetMsg"false) != -1)
    {
        
//char ipRegexMatch[32];
        //Format(ipRegexMatch, sizeof(ipRegexMatch), "(\d+\.\d+\.\d+\.\d+)");
        
Regex regex CompileRegex("(\\d+\\.\\d+\\.\\d+\\.\\d+)");
        
int numMatched regex.Match(attackInput);
        
PrintToServer("numMatch : %d"numMatched);
        if(
numMatched != -1)
        {
            
char ipGet[64];
            
regex.GetSubString(0ipGetsizeof(ipGet));
            
Format(ipOutAttackmaxlen"%s"ipGet);
            return 
true;
        }
        else
        {
            return 
false;
        }
    }
    else
    {
        return 
false;
    }

any help?
NOTE: i've tested the regular expression code and i'm sure which that is not the problem.
__________________

Last edited by ambn; 03-03-2018 at 03:11.
ambn is offline
Fyren
FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren
Join Date: Feb 2106
Old 03-03-2018 , 09:38   Re: need help with Regular Expressions in sourcemod
Reply With Quote #2

This complete test plugins works as expected:
PHP Code:
#include <regex>

public Action test(int clientint args)
{
        
char out[64];
        
test2("lksjflkdsfSendNetMsglkjsdflks 1.2.3 skljdfldksj 1.2.3.4lkjsdflk"outsizeof(out));
        
PrintToServer("ret: %s"out);
}

void test2(char[] schar[] outint len) {
        if(
StrContains(s"SendNetMsg"false) != -1)
        {
                
Regex regex CompileRegex("(\\d+\\.\\d+\\.\\d+\\.\\d+)");
                
int numMatched regex.Match(s);
                
PrintToServer("numMatch : %d"numMatched);
                
regex.GetSubString(0outlen);
        }
}

public 
void OnPluginStart() {
        
RegConsoleCmd("regextest"test);

The problem is probably not in the code you provided.

You don't need the parentheses in the regex because the match at index 0 will be what matched the entire regex. The match at index 1 will be what matched the first subgroup (which will be the same thing). You also don't need to compile the regex every time, just once.

Last edited by Fyren; 03-03-2018 at 09:39. Reason: rephrase
Fyren is offline
ambn
Veteran Member
Join Date: Feb 2015
Location: Fun servers
Old 05-22-2018 , 11:24   Re: need help with Regular Expressions in sourcemod
Reply With Quote #3

well the post has got a little old but i haven't been able to get it working til now, any suggestions? here's my code , what's is really the problem?
PHP Code:
stock void getAttackerIP(const char[] messagechar[] ipint maxlen)
{
    
Regex regex CompileRegex("\\d+\\.\\d+\\.\\d+\\.\\d+");
    
int numMatched regex.Match(message);
    
PrintToServer("numMatch : %d"numMatched);
    
regex.GetSubString(0ipmaxlen);

the matchNum is 0 and the string is empty..
__________________
ambn is offline
Fyren
FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren
Join Date: Feb 2106
Old 05-22-2018 , 11:39   Re: need help with Regular Expressions in sourcemod
Reply With Quote #4

Quote:
Originally Posted by Fyren View Post
The problem is probably not in the code you provided.
Once again:

PHP Code:
#include <regex>

public void OnPluginStart() {
        
RegConsoleCmd("regextest"test);
}

public 
Action test(int clientint args)
{
        
char out[64];
        
getAttackerIP("lksjflkdsfSendNetMsglkjsdflks 1.2.3 skljdfldksj 1.2.3.4lkjsdflk"outsizeof(out));
        
PrintToServer("ret: %s"out);
}

stock void getAttackerIP(const char[] messagechar[] ipint maxlen)
{
        
Regex regex CompileRegex("\\d+\\.\\d+\\.\\d+\\.\\d+");
        
int numMatched regex.Match(message);
        
PrintToServer("numMatch : %d"numMatched);
        
regex.GetSubString(0ipmaxlen);

Code:
regextest
numMatch : 1
ret: 1.2.3.4
Fyren is offline
ambn
Veteran Member
Join Date: Feb 2015
Location: Fun servers
Old 05-22-2018 , 11:56   Re: need help with Regular Expressions in sourcemod
Reply With Quote #5

awh well thanks for clearing that, i might have just to figure out what's wrong with the input rather than the regular expression.
__________________
ambn is offline
Reply



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 19:13.


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