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

时间:2015-11-03 12:49:13

标签: c++ regex

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

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

由于

3 个答案:

答案 0 :(得分:6)

您可以使用Boost.Regex,它已经实现了Partial Matches

  

使用时表示应找到部分匹配和完全匹配。 部分匹配是匹配文本输入结尾处的一个或多个字符但与所有正则表达式不匹配的匹配(尽管它可能已经这样做了)更多输入可用)。

<强>代码

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

rextester demo

答案 1 :(得分: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;
}

输出:

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

###########################################

答案 2 :(得分:-4)

以下代码会检查字符串是否与表达式匹配&#34;#&#34;在开头,然后是一个或多个数字...

#include <iostream>
#include <regex>

using namespace std;

int main()
{

    string str;
    while (true) {
        cin >> str;
        regex pat{ "(#[[:d:]]+)" };
        bool match = regex_search(str, pat);
        cout << (match ? "Matched" : "Not matched") << endl;
    }

    return 0;
}