AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   pawn check if string contains (https://forums.alliedmods.net/showthread.php?t=187292)

AcidLys 06-11-2012 11:05

pawn check if string contains
 
Hello guys how i can check if string contains symbols

for example

if(playerName.Contains("<") || playerName.Contains("-")){

}

or how i can check string with regex pattern

Thanks

Exolent[jNr] 06-11-2012 11:14

Re: pawn check if string contains
 
Code:
if(contain(playerName, "<") >= 0 || contain(playerName, "-") >= 0) {

Here is how you would check against a pattern:
Code:
new ret, error[128] new Regex:check = regex_match(playerName, "^^[a-z0-9\s]+$", ret, error, charsmax(error), "i") switch(check) {     case REGEX_MATCH_FAIL: // failed match     case REGEX_PATTERN_FAIL: // error with pattern     case REGEX_NO_MATCH: // string did not match pattern     default: {         // string matched pattern         regex_free(check);     } }

If you are going to use the same pattern more than once, it would be best to compile it.
See regex.inc for more information.

PredatorBlock 04-28-2020 11:38

Re: pawn check if string contains
 
Hello,

How to make it work with double quotes? "

It won't work both of them.

Code:

new FirstArg[ 32 ], SecondArg[ 32 ];
read_argv( 1, FirstArg, sizeof ( FirstArg ) -1 );
read_argv( 2, SecondArg, sizeof ( SecondArg ) -1 );

if(contain(SecondArg, "^"") == 0 )
{
        console_print(id, "Working...");
}

new Regex:check = regex_match(SecondArg, "^"", ret, error, charsmax(error), "i")

if(Regex:check != REGEX_MATCH_FAIL && Regex:check != REGEX_PATTERN_FAIL && Regex:check != REGEX_NO_MATCH)
{
        console_print(id, "Working...");
}


P.S
Till i got answer i found a solution... i know is not the best but at least it work

        /* === START REASON === */
        new CheckReason[ 100 ];
        read_argv( 2, CheckReason, sizeof ( CheckReason ) -1 );
       
        if( strlen(CheckReason) < 10 )
        {
                console_print(id, "Reason isn't sufficient, please describe more (minimum - 10 chars)");
                return PLUGIN_HANDLED
        }
       
        read_args( CheckReason, charsmax ( CheckReason ) ); //catch charachters
        if( contain( CheckReason, "^"" ) <= 0 )
        {
                console_print(id, "Invalid reason format, please use: amx_kick <name, #userid> ^"reason^"");
                return PLUGIN_HANDLED
        }
       
        // Clean & final reason
        new reason[ 100 ];
        read_argv( 2, reason, sizeof ( reason ) -1 );
       
        /* === END REASON === */


Well what i want to do is to force admin to use command in this way:
amx_kick Nickname "reason_between_quotes"

My solution from beloved it check only if contain at least 1 double_quote

Why i force using ""
Cause if you execute command in this way: amx_kick Nickname You have been kicked from server because you are inactive!

The reason isn't printed fully

If someone can help me to find a simple and clean solution for checking reason argument 2 from kick and force admin to use double quote "" i appreciate.

Thank you


Bugsy 04-28-2020 19:04

Re: pawn check if string contains
 
I don't know reg-ex, but this will cover checking that the second arg is wrapped in quotes.
Code:

register_concmd( "amx_test" , "Test" );

public Test( id )
{
        if ( !VerifyArgQuotes() )
        {
                console_print( id , "Proper usage of command is: amx_test <name> ^"Reason^"." );
                return;
        }
       
        //your code   
}
       
bool:VerifyArgQuotes()
{
        new szText[ 128 ] , iPos , iLen;
       
        iLen = read_args( szText , charsmax( szText ) );
       
        if ( szText[ 0 ] == '^"' )
                iPos = strfind( szText , "^"" , .pos = 1 ) + 2;
        else
                iPos = strfind( szText , " " , .pos = 1 ) + 1;
       
        return bool:( iPos && ( szText[ iPos ] == '^"' ) && ( szText[ iLen - 1 ] == '^"' ) );
}


PredatorBlock 04-28-2020 22:38

Re: pawn check if string contains
 
Quote:

Originally Posted by Bugsy (Post 2696773)
I don't know reg-ex, but this will cover checking that the second arg is wrapped in quotes.
Code:

register_concmd( "amx_test" , "Test" );

public Test( id )
{
        if ( !VerifyArgQuotes() )
        {
                console_print( id , "Proper usage of command is: amx_test <name> ^"Reason^"." );
                return;
        }
       
        //your code   
}
       
bool:VerifyArgQuotes()
{
        new szText[ 128 ] , iPos , iLen;
       
        iLen = read_args( szText , charsmax( szText ) );
       
        if ( szText[ 0 ] == '^"' )
                iPos = strfind( szText , "^"" , .pos = 1 ) + 2;
        else
                iPos = strfind( szText , " " , .pos = 1 ) + 1;
       
        return bool:( iPos && ( szText[ iPos ] == '^"' ) && ( szText[ iLen - 1 ] == '^"' ) );
}



Works like a charm, very good thanks a lot @Bugsy

Bugsy 04-28-2020 22:57

Re: pawn check if string contains
 
With my method it will need coding to handle every single scenario. So the above could be named VerifyArg3HasQuotes(), then another can be created for Arg1HasQuotesArg2IsNumberArg3HasQuotes() and so on.

PredatorBlock 04-29-2020 18:02

Re: pawn check if string contains
 
Quote:

Originally Posted by Bugsy (Post 2696811)
With my method it will need coding to handle every single scenario. So the above could be named VerifyArg3HasQuotes(), then another can be created for Arg1HasQuotesArg2IsNumberArg3HasQuotes() and so on.


Ok but how i can specify in your code the arg which to read for example 1, 2 or 3

Cause your code is: read_args and can't specify the arg which needs to be read like is

Code:

static arg_reason[ 70 ];
read_argv(2, arg_reason, sizeof(arg_reason) - 1);

Can you help me?
i need this:

amx_ban "STEAM_ID" 5 "reason"

So steam_id and reason require for double quotes!

Thanks.


P.S - i found this it works to require for ARG 1 and 2 and only ARG 2
Code:

bool:Quotes_req_arg12()
{
        new szText[ 128 ] , iPos , iLen;
       
        iLen = read_args( szText , charsmax( szText ) );
       
        if ( szText[ 0 ] == '^"' )
                iPos = strfind( szText , "^"" , .pos = 1 ) + 2;
        else
                iPos = strfind( szText , " " , .pos = 1 ) + 2;
       
        return bool:( iPos && ( szText[ iPos ] == '^"' ) && ( szText[ iLen - 1 ] == '^"' ) );
}


bool:Quotes_req_arg2()
{
        new szText[ 128 ] , iPos , iLen;
       
        iLen = read_args( szText , charsmax( szText ) );
       
        if ( szText[ 0 ] == '^"' )
                iPos = strfind( szText , "^"" , .pos = 1 ) + 2;
        else
                iPos = strfind( szText , " " , .pos = 1 ) + 1;
       
        return bool:( iPos && ( szText[ iPos ] == '^"' ) && ( szText[ iLen - 1 ] == '^"' ) );
}

But i have no idea how to make it require from 3 ARGS only 2: 1 and 3

Bugsy 04-29-2020 18:16

Re: pawn check if string contains
 
What variations do you need?

I gave you: command <no quotes> <quotes>

Bugsy 04-29-2020 19:04

Re: pawn check if string contains
 
This will get the full argument, including the quotes. There may be a native that I don't know about that does this. Not thoroughly tested.

Input: amx_test "123" 456 "789" "1"

Output:
Code:

4 args
Arg[0]="123"
Arg[1]=456
Arg[2]="789"
Arg[3]="1"

PHP Code:

#include <amxmodx>

public plugin_init() 
{
    
register_concmd"amx_test" "Test" );
}

public 
Testid )
{
    new 
szArgs10 ][ 32 ];
    
    new 
iNumArgs GetArgsszArgs sizeofszArgs ) , charsmaxszArgs[] ) );
    
    
server_print"%d args" iNumArgs );
    
    for ( new 
iNumArgs i++ )
    {
        
server_print"Arg[%d]=%s" szArgs] );
    }
}
    
GetArgsszArgs[][] , iSize iLen )
{
    new 
szText128 ] , iSourceLen iPos bool:bInArg iArgIndex iArgPos;
    
    
iSourceLen read_argsszText charsmaxszText ) );
    
    while ( ( 
iPos iSourceLen ) && ( iArgIndex iSize ) )
    {
        if ( !
bInArg && szTextiPos ] != ' ' )
            
bInArg true;
        
        if ( 
szTextiPos ] != ' ' )
        {
            
szArgsiArgIndex ][ iArgPos++ ] = szTextiPos ];
            
            if ( 
iArgPos >= iLen )
            {    
                    
                while ( ( 
iPos iSourceLen ) && szText[ ++iPos ] != ' ' )
                {}
                
                
bInArg false;
                
iArgIndex++;
                
iArgPos 0;
            }
        }
        else if ( 
bInArg && ( szTextiPos ] == ' ' ) )
        {
            
bInArg false;
            
iArgIndex++;
            
iArgPos 0;
        }
        
        
iPos++;
    }
    
    return ( 
iArgIndex );



PredatorBlock 04-29-2020 20:03

Re: pawn check if string contains
 
Thanks for reply.

I don't want to bother you but i am new to pawn coding so i have to learn.

Look this is what i have:

Code:

#include <amxmodx>

public plugin_init()
{
        register_concmd("amx_ban", "CmdBan", ADMIN_BAN, "^"nick, #userid, steamid, ip^" <time in minutes> ^"reason^"");
}

public CmdBan(client, level, cid)
{
        if( !cmd_access(client, level, cid, 4) ) return PLUGIN_HANDLED;
       
        static arg[128];
        read_argv(1, arg, sizeof(arg) - 1);
       
        new target = cmd_target(client, arg, GetTargetFlags(client));
        if( !target ) return PLUGIN_HANDLED;
       
        static target_authid[35];
        get_user_authid(target, target_authid, sizeof(target_authid) - 1);
       
        static target_ip[35];
        get_user_ip(target, target_ip, sizeof ( target_ip ) -1, 1 );
       
        if( !IsValidAuthid(target_authid) )
        {
                console_print(client, "%L", client, "NOT_AUTHORIZED");
                return PLUGIN_HANDLED;
        }
       
        // ******** HERE I NEED TO CHECK IF args have quote *************
        if ( !Quotes_req_arg13() )
        {
                console_print( client , "Proper usage of command is: amx_ban ^"nick, #userid, steamid, ip^" <time in minutes> ^"reason^"" );
                return PLUGIN_HANDLED;
        }

        Can you make me the code please how to make it?
       
        Thank you.
        ....................
}



All times are GMT -4. The time now is 06:17.

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