检查字符串是否与正则表达式匹配

Pot*_*ex5 9 c++ regex

基本上,我希望能够有一个正则表达式,例如#[0-9] +,并能够检查字符串是否可以匹配它.例如,如果我收到用户输入并输入"#",则此字符串与正则表达式不匹配,但也可以是用户输入的数字.

我知道C++有match()函数,但有什么东西像我在寻找什么?还是某种方式呢?

谢谢

Mar*_*ano 6

您可以使用Boost.Regex,它已经实现了部分匹配.

使用时,表示应找到部分匹配和完全匹配.一个部分匹配是一个匹配的文本输入结束一个或多个字符,但不匹配所有的正则表达式的(虽然它可能已经这样做已经提供更多的输入).

#include <iostream>
#include <boost/regex.hpp>
using namespace std;
using namespace boost;


int main  () {
    string subject = "#";
    string pattern = "#[0-9]+";


    const regex e(pattern);
    if (regex_match(subject, e, match_partial)) {
        cout << subject << " \tMATCHES\t " << pattern << endl;
    } else {
        cout << subject << " \tDOESN'T MATCH\t " << pattern << endl;
    }


    return 0;
}
Run Code Online (Sandbox Code Playgroud)

rextester demo

  • 这比接受的解决方案更好. (4认同)

Sim*_*mer 4

免责声明:以下是一个非常幼稚的方法,既不快也不漂亮。但它可以完成简单正则表达式的工作。我不建议在不了解它的作用的情况下使用它

#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;
}
Run Code Online (Sandbox Code Playgroud)

输出:

###########################################
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

###########################################
Run Code Online (Sandbox Code Playgroud)