Regex 正则表达式不匹配';文件中没有匹配项

Regex 正则表达式不匹配';文件中没有匹配项,regex,c++11,Regex,C++11,我想使用regex识别文本文件中的一些行,但regex_match与任何行都不匹配,即使我使用regex用户(“.*”) string dirin=“/home/user/in.srt”; 字符串dirout=“/home/user/out.srt”; ifstream-in(dirin.c_str()); 流输出(dirout.c_str()); 弦线; //正则表达式用户(“(\\d{2}):(\\d{2}):(\\d{2}),(\\d{3})\\s-->\\s(\\d{2}):(\\d{2

我想使用regex识别文本文件中的一些行,但regex_match与任何行都不匹配,即使我使用regex用户(“.*”)

string dirin=“/home/user/in.srt”;
字符串dirout=“/home/user/out.srt”;
ifstream-in(dirin.c_str());
流输出(dirout.c_str());
弦线;
//正则表达式用户(“(\\d{2}):(\\d{2}):(\\d{2}),(\\d{3})\\s-->\\s(\\d{2}):(\\d{2}):(\\d{2}),(\\d{3})”;
正则表达式赞助人(“*”);
smatch m;
while(getline(in,line)){
if(正则表达式匹配(行、m、用户)){
out注意,
getline()
读取带有尾随回车符CR符号的行,并注意ECMAScript
模式与CR符号不匹配,因为它是一个行尾符号

regex\u match
要求整个字符串与模式匹配

因此,您需要在模式末尾说明可选的回车符。您可以通过在模式末尾附加
\r?
\s*
来完成此操作:

regex patron("(\\d{2}):(\\d{2}):(\\d{2}),(\\d{3})\\s-->\\s(\\d{2}):(\\d{2}):(\\d{2}),(\\d{3})\\s*");

也可以考虑如果C++版本允许的话,使用原始字符串文字:

regex patron(R"((\d{2}):(\d{2}):(\d{2}),(\d{3})\s-->\s(\d{2}):(\d{2}):(\d{2}),(\d{3})\s*)");

我们应该猜猜你想匹配什么吗?我想匹配注释行,但不匹配任何行line@DiegoSilvera-无法复制:使用您的代码,所有行(也为空)生成一个“ok”,您需要在模式末尾追加
\n?
regex用户((\\d{2}):(\\d{2}):(\\d{2}):(\\d{2}),(\\d{3})\\s-->\\s(\\d{2}):(\\d{2}):(\\d{2}),(\\d{3})\n?);
正则表达式用户(.*\n?);
regex patron(".*\\s*");
regex patron(R"((\d{2}):(\d{2}):(\d{2}),(\d{3})\s-->\s(\d{2}):(\d{2}):(\d{2}),(\d{3})\s*)");