Amxxprogramer / Regular Expressions. regex_tester.txt
RichieBurundi's picture
Upload 28 files
82d6031 verified
#include < amxmodx >
#include < amxmisc >
#include < regex >
new Regex:g_pPattern = REGEX_PATTERN_FAIL;
new Regex:g_pPatternFlags;
public plugin_init( )
{
register_plugin( "Regex Tester", "0.0.1", "Exolent" );
register_concmd( "regex_pattern", "CmdPattern", _, "<pattern>" );
register_concmd( "regex_test", "CmdTest", _, "<test data>" );
new iReturn, szError[ 128 ];
g_pPatternFlags = regex_compile( "^^\/(.*)\/([imsx]*)$", iReturn, szError, charsmax( szError ) );
if( g_pPatternFlags == REGEX_PATTERN_FAIL )
{
log_amx( "Error creating pattern for patterns: %s", szError );
}
}
public CmdPattern( iPlayer, iLevel, iCID )
{
if( !cmd_access( iPlayer, iLevel, iCID, 2 ) )
{
return PLUGIN_HANDLED;
}
new szPattern[ 128 ];
read_args( szPattern, charsmax( szPattern ) );
trim( szPattern );
remove_quotes( szPattern );
new szFlags[ 5 ];
new iReturn;
if( regex_match_c( szPattern, g_pPatternFlags, iReturn ) > 0 )
{
regex_substr( g_pPatternFlags, 1, szPattern, charsmax( szPattern ) );
regex_substr( g_pPatternFlags, 2, szFlags, charsmax( szFlags ) );
}
new szError[ 128 ];
g_pPattern = regex_compile( szPattern, iReturn, szError, charsmax( szError ), szFlags );
if( g_pPattern == REGEX_PATTERN_FAIL )
{
console_print( iPlayer, "Error with pattern: %s", szError );
}
else
{
console_print( iPlayer, "Pattern set to: %s", szPattern );
console_print( iPlayer, "Flags set to: %s", szFlags );
}
return PLUGIN_HANDLED;
}
public CmdTest( iPlayer, iLevel, iCID )
{
if( !cmd_access( iPlayer, iLevel, iCID, 2 ) )
{
return PLUGIN_HANDLED;
}
if( g_pPattern == REGEX_PATTERN_FAIL )
{
console_print( iPlayer, "No pattern set. Use regex_pattern first." );
return PLUGIN_HANDLED;
}
new szData[ 256 ];
read_args( szData, charsmax( szData ) );
trim( szData );
remove_quotes( szData );
new iReturn;
if( regex_match_c( szData, g_pPattern, iReturn ) != -2 )
{
console_print( iPlayer, "%d matches", iReturn );
for( new i = 0; i < iReturn; i++ )
{
regex_substr( g_pPattern, i, szData, charsmax( szData ) );
console_print( iPlayer, "%d. ^"%s^"", ( i + 1 ), szData );
}
}
else
{
console_print( iPlayer, "Regex match error code %d", iReturn );
}
return PLUGIN_HANDLED;
}