检查字符串是否可能与正则表达式匹配
Check if string is a potential match for regex
基本上,我希望能够拥有一个正则表达式,如#[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;
}
相关文章:
- 使用正则表达式regex_search在字符串中查找字符串
- 如何在C++中使用正则表达式"|"后选择字符串?
- 如何在 c++11 中使用正则表达式找到确切的子字符串?
- 如何编写将验证以下字符串的正则表达式?
- C++带有捕获组的正则表达式字符串文本
- 如何使用正则表达式从字符串中提取值
- 使用正则表达式c++从单词和分隔符之间的字符串中提取所有子字符串
- 如何在没有外部库的情况下使用C++03约束执行基于正则表达式的字符串操作
- 需要忽略日期/序列号,但匹配字符串其余部分的正则表达式
- 如何将正则表达式向量与一个字符串匹配?
- 正则表达式匹配字符串之间的数字
- 如何根据某个索引将字符串与正则表达式匹配?
- 如何从字符串变量为正则表达式构建原始字符串
- C++ 中用于搜索字符串模式的正则表达式
- C++无符号字符串类型的正则表达式
- 如何使用正则表达式将输入数字格式化为字符串
- 使用具有特殊字符的正则表达式标记 C++ 字符串
- C 11正则表达式和字符串U8前缀
- 处理正则表达式字符串时编译崩溃(内存泄漏)
- 正则表达式字符串过滤 - 如何允许“-”符号