DISCLAIMER: The following is a rather naive approach that is neither quick nor beautiful. However, it does the job for a simple regular expression. I do not recommend using this without understanding what he is doing.
#include <string> #include <iostream> #include <regex> bool is_valid_regex_string(const std::string& rgx_str) { bool bResult = true; try { std::regex tmp(rgx_str); } catch (const std::regex_error& e) { (e); bResult = false; } return bResult; } std::string create_partial_regex_string(const std::string& rgx_str) { std::string par_rgx_str; for (int i = 1; i <= rgx_str.size(); i++) { std::string sub_rgx_str = rgx_str.substr(0, i); if (is_valid_regex_string(sub_rgx_str)) { if (!par_rgx_str.empty())par_rgx_str += "|"; par_rgx_str += "(" + sub_rgx_str + ")"; } } //return par_rgx_str; return "^" + par_rgx_str + "$"; } void testPartialRegex(const std::string& rgx, const std::string& str) { std::string partialRegexString = create_partial_regex_string(rgx); std::regex partRegex(partialRegexString); std::cout << "\tTESTING \"" << str << "\" against \"" << partialRegexString << "\" :" << std::endl; std::smatch base_match; std::cout << "\t\t-> " << (std::regex_match(str, partRegex) ? "true" : "false") << std::endl; } void test(const std::string& str) { std::cout << "\n###########################################\nTESTING \"" << str << "\"\n" << std::endl; for (int i = 1; i <= str.size(); i++) { testPartialRegex("#[0-9]+", str.substr(0, i)); } std::cout << "\n###########################################\n" << std::endl; } int main() { test("#123456"); test("#12a3456"); test("#b"); test("123456"); test("##"); return 0; }
Output:
########################################### TESTING "#123456" TESTING "#" against "^(#)|(#[0-9])|(#[0-9]+)$" : -> true TESTING "#1" against "^(#)|(#[0-9])|(#[0-9]+)$" : -> true TESTING "#12" against "^(#)|(#[0-9])|(#[0-9]+)$" : -> true TESTING "#123" against "^(#)|(#[0-9])|(#[0-9]+)$" : -> true TESTING "#1234" against "^(#)|(#[0-9])|(#[0-9]+)$" : -> true TESTING "#12345" against "^(#)|(#[0-9])|(#[0-9]+)$" : -> true TESTING "#123456" against "^(#)|(#[0-9])|(#[0-9]+)$" : -> true ########################################### ########################################### TESTING "#12a3456" TESTING "#" against "^(#)|(#[0-9])|(#[0-9]+)$" : -> true TESTING "#1" against "^(#)|(#[0-9])|(#[0-9]+)$" : -> true TESTING "#12" against "^(#)|(#[0-9])|(#[0-9]+)$" : -> true TESTING "#12a" against "^(#)|(#[0-9])|(#[0-9]+)$" : -> false TESTING "#12a3" against "^(#)|(#[0-9])|(#[0-9]+)$" : -> false TESTING "#12a34" against "^(#)|(#[0-9])|(#[0-9]+)$" : -> false TESTING "#12a345" against "^(#)|(#[0-9])|(#[0-9]+)$" : -> false TESTING "#12a3456" against "^(#)|(#[0-9])|(#[0-9]+)$" : -> false ########################################### ########################################### TESTING "#b" TESTING "#" against "^(#)|(#[0-9])|(#[0-9]+)$" : -> true TESTING "#b" against "^(#)|(#[0-9])|(#[0-9]+)$" : -> false ########################################### ########################################### TESTING "123456" TESTING "1" against "^(#)|(#[0-9])|(#[0-9]+)$" : -> false TESTING "12" against "^(#)|(#[0-9])|(#[0-9]+)$" : -> false TESTING "123" against "^(#)|(#[0-9])|(#[0-9]+)$" : -> false TESTING "1234" against "^(#)|(#[0-9])|(#[0-9]+)$" : -> false TESTING "12345" against "^(#)|(#[0-9])|(#[0-9]+)$" : -> false TESTING "123456" against "^(#)|(#[0-9])|(#[0-9]+)$" : -> false ########################################### ########################################### TESTING "##" TESTING "#" against "^(#)|(#[0-9])|(#[0-9]+)$" : -> true TESTING "##" against "^(#)|(#[0-9])|(#[0-9]+)$" : -> false ###########################################
Simon kraemer
source share