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

Check if string is a potential match for regex

本文关键字:正则表达式 字符串 是否 检查      更新时间:2023-10-16

基本上,我希望能够拥有一个正则表达式,如#[0-9]+,并能够检查字符串是否与之匹配#"该字符串与正则表达式不匹配,但如果用户也输入了一些数字,则可能匹配。

我知道C++有matches()函数,但有什么像我想要的那样吗?或者有什么办法?

您可以使用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 << " tMATCHESt " << pattern << endl;
    } else {
        cout << subject << " tDOESN'T MATCHt " << pattern << endl;
    }

    return 0;
}

rextester演示

免责声明:以下是一种非常天真的方法,既不快速也不愉快。然而,它完成了简单regex的工作我不建议在不了解它的作用的情况下使用它

#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 << "tt-> " << (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
###########################################

以下代码检查字符串是否与开头带有"#"并后跟一个或多个数字的表达式匹配。。。

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