Last active
January 12, 2017 06:29
-
-
Save balmyBanzai/a4df40ec9bf84c57a6e000547eb225c3 to your computer and use it in GitHub Desktop.
cmd_regex_verifier
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Commandline Regular Expression (regex) Verifier Tool | |
* This tool finds the first partial match in the regex's capture group 1 in the string and pipes it to stdout and exits 0 on success. | |
* Parameter 1 is the string to search, e.g., "C:\Some\Directory\with\a\special\file_unique12354.exe" | |
* Parameter 2 is the regex, e.g., "\\.+(thing to find after last backslash, like `name`).+$" | |
* which would be written like, "\\.+\\.+(unique.+)\." | |
* From CMD.exe>call crv.exe "C:\Some\Directory\with\a\special\file_unique12354.exe" "\\.+\\.+(unique.+)\." | |
* That would pipe unique12354 to STDOUT and exit 0. | |
*/ | |
#include <iostream> | |
#include <regex> | |
#include <string> | |
using namespace std; | |
static int parseParameters(int arg_counter, char * args[]); | |
/*=======================================================================================================================================================*/ | |
int main(int argc, char * argv[]) | |
{ | |
int parameter_check = parseParameters(argc, argv); | |
if (parameter_check != 0) { return parameter_check; } | |
string regex_string = argv[2]; | |
const string this_string = string(argv[1]); | |
const regex this_regex(regex_string); | |
smatch match; | |
if (regex_search(this_string.begin(), this_string.end(), match, this_regex)) | |
{ | |
cout << match[1] << endl; | |
return 0; | |
} | |
return 1; | |
} | |
/*=======================================================================================================================================================*/ | |
static int parseParameters(int arg_counter, char * args[]) | |
{ | |
int return_code = 0; | |
if (arg_counter == 2) | |
{ | |
string this_arg = args[1]; | |
if ((this_arg == "?") || (this_arg == "/?") || (this_arg == "help") || (this_arg == "-help") || (this_arg == "--help") || (this_arg == "-h")) | |
{ | |
return_code = 2; | |
} | |
else | |
{ | |
cout << "\nIncorrect parameters sent to " << args[0] << endl; | |
return_code = 1; | |
} | |
} | |
else if (arg_counter < 3) | |
{ | |
cout << "\nIncorrect parameters sent to " << args[0] << endl; | |
return_code = 1; | |
} | |
if (return_code != 0) | |
{ | |
cout << "\n===== " << args[0] << " HELP =====" << endl; | |
cout << "\nThe first parameter should be the string to search through." << endl; | |
cout << "The second parameter should be the regular expression to use against the string." << endl; | |
cout << "For example, you would call this program at commandline like so: " << args[0] << " \"This is my string\" " << "\"my (string$)\"" << endl; | |
cout << "If there is a regex match, it will print to console the first match it finds and exit 0." << endl; | |
cout << "If no match is found, it will print nothing and exit -1." << endl; | |
cout << "Returning multiple matches falls outside the scope of this utility's intent." << endl; | |
cout << "If this was called with incorrect parameters, it will print this help and exit 1." << endl; | |
cout << "If the help was successfully queried with a typical help parameter, it will print this help and exit 2.\n" << endl; | |
cout << "Exiting " << args[0] << "..." << endl; | |
} | |
return return_code; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment