找不到匹配的正则表达式
Cannot find regex match C++
我是c++的初学者,试图找到一个正则表达式匹配,但即使有<身份证号码> 94893274123 & lt;/ID-Number>(不带空格)在xml文件中找不到匹配。我不太确定它是否正确读取xml文件…身份证号码>
int main (){
string line;
ifstream infile("example.xml");
if (infile.is_open()){
ofstream outfile ("out.txt", ios_base::ate | ios_base::app);
if (outfile.is_open()){
while (getline(infile, line)){
//ID
regex idnr (".*+<ID-Number>([0-9]+)<.*");
smatch id_match;
if (regex_match (line.cbegin(), line.cend(), id_match, idnr)){
cout << "Match found n";
}
}
//closing all files
outfile.close();
infile.close();
}
else cout << "Unable to open output filen";
}
else cout << "Unable to open input filen";
return 0;
}
程序运行返回0,但仅此而已。我想做的是将xml文件分解为我需要的信息片段,稍后将其保存为变量或向量,并将它们重新组织到另一个文件中。
使用dev - c++ 5.11编译时,以下代码按预期工作(报告匹配发现)(确保您设置代码生成以符合GNU c++ 11)。我所做的唯一更改是添加了#include语句。
确保文件位于执行代码可以找到它的目录中。你的ifstream构造函数是一个相对路径。"example.xml"文件需要与程序运行的目录相同。
#include <iostream>
#include <fstream>
#include <regex>
using namespace std;
int main (){
string line;
ifstream infile("example.xml");
if (infile.is_open()){
ofstream outfile ("out.txt", ios_base::ate | ios_base::app);
if (outfile.is_open()){
while (getline(infile, line)){
//ID
regex idnr (".*+<ID-Number>([0-9]+)<.*");
smatch id_match;
if (regex_match (line.cbegin(), line.cend(), id_match, idnr)){
cout << "Match found n";
}
}
//closing all files
outfile.close();
infile.close();
}
else cout << "Unable to open output filen";
}
else cout << "Unable to open input filen";
return 0;
}
尝试使用Visual Studio 2013相同的代码,我得到一些错误。首先,进入DEBUG->Project Properties->Configuration Properties->Debugging,并确保您的工作目录指向包含您正在解析的example.xml文件的目录。接下来,看看regex构造函数
regex idnr(".*+<ID-Number>([0-9]+)<.*");
点号+星号序列将贪婪地消耗任何字符串,并阻止序列中其余部分的匹配。
相关文章:
- FindPackageHandleStandardArgs.cmake:137 的 CMake 错误(消息):找不到 Boost (缺少:正则表达式)(找到合适的版本"1.72.0",
- 正则表达式 - 匹配不遵循特定模式的字符
- C++11 正则表达式匹配不以句点结尾的完整单词
- 如何使用正则表达式匹配不包含特殊字符(&、\、<、>、|、)的字符串,除非它们以反斜杠开头
- 正则表达式C++不起作用
- 使用带有提升/正则表达式的 Rcpp,在 eval(expr, envir, enclos) 中出错:找不到对象 'libboost_regex'
- 正则表达式C++不使用方括号
- 正则表达式 找不到数字序列
- 为什么正则表达式在C++的日语字符串中找不到“(”
- 正则表达式:捕获不带前导字符的字符串
- 正则表达式 stl 不起作用
- 正则表达式,找不到匹配项
- 找不到c++正则表达式搜索模式
- PHP-CPP C++ 扩展找不到正则表达式
- C++ 2011 std::正则表达式根本不起作用
- 创建单个正则表达式而不是多个正则表达式
- 找不到匹配的正则表达式
- 在c++正则表达式中不工作
- 将两个正则表达式结果连接到一个输出字段中,一次只能使用一个
- 整数匹配正则表达式模式不工作