C++ 我的Boost正则表达式与任何内容都不匹配

C++ 我的Boost正则表达式与任何内容都不匹配,c++,regex,boost,C++,Regex,Boost,我正在尝试匹配如下所示的字符串: 3月25日19:17:55 127.0.0.1用户:[pool-15-thread-17]INTOUCH;0;信息;软负载服务;安装已启动 使用正则表达式。下面是我定义正则表达式的代码: #include <boost/regex.hpp> #include <boost/date_time/posix_time/posix_time.hpp> #include <tuple> #include <string>

我正在尝试匹配如下所示的字符串:

3月25日19:17:55 127.0.0.1用户:[pool-15-thread-17]INTOUCH;0;信息;软负载服务;安装已启动

使用正则表达式。下面是我定义正则表达式的代码:

#include <boost/regex.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <tuple>
#include <string>
const std::string softload_startup = "(\\w{3}) (\\d{1,2}) (\\d{2}): 
(\\d{2}):(\\d{2})*SOFTLOADSERVICE;Install started\\s"; //NOLINT

const boost::regex softload_start(softload_startup);

class InTouchSoftload {
 public:
   explicit InTouchSoftload(std::string filename);
 private:
    std::string _log_name;
    std::tuple<unsigned int, std::string> software_update_start;
};
(\\w{3}) (\\d{1,2}) (\\d{2}):(\\d{2}):(\\d{2}).*SOFTLOADSERVICE;Install started\\s*
#包括
#包括
#包括
#包括
const std::string softload_startup=“(\\w{3})(\\d{1,2})(\\d{2}):
(\\d{2}):(\\d{2})*SOFTLOADSERVICE;安装已启动\\s”//诺林特
const boost::regex软加载启动(软加载启动);
类装入软负载{
公众:
显式InTouchSoftload(标准::字符串文件名);
私人:
std::string\u log\u name;
std::tuple软件\u更新\u启动;
};
我在这里称之为:

 int main() {
        fin.open(input_file);

        if (fin.fail()) {
            std::cerr << "Failed to open " << input_file << std::endl;
            exit(1);
        }

        while (std::getline(fin, line)) {
                line_no++;
                if (regex_match(line, softload_start)) {
                    std::cout << line << std::endl;
                }
            }
        return 0;
    }
intmain(){
fin.open(输入_文件);
if(fin.fail()){

std::cerr虽然您尚未提供完整的示例,但您最近的编辑表明您失败了,因为您正在尝试匹配单个行,这是std::getline()
的结果,而您的模式涉及两行

如果确实如此,您可能应该执行以下操作之一:

  • 匹配成对的连续行(即在每次迭代中尝试匹配前一行+当前行)
  • 将regexp拆分为两个正则表达式,每行一个。现在,每当一行与第一个regexp匹配时,请尝试将下一行与第二行匹配;否则,请尝试将其与第一行匹配
  • ^
    添加到regexp的开头,将
    $
    添加到结尾(以便它在行边界上匹配,并将regexp与整个输入流匹配,而不是逐行匹配)

如果您的正则表达式与您希望它匹配的字符串不匹配,则您的正则表达式是错误的。我已更正了您的正则表达式:

#include <boost/regex.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <tuple>
#include <string>
const std::string softload_startup = "(\\w{3}) (\\d{1,2}) (\\d{2}): 
(\\d{2}):(\\d{2})*SOFTLOADSERVICE;Install started\\s"; //NOLINT

const boost::regex softload_start(softload_startup);

class InTouchSoftload {
 public:
   explicit InTouchSoftload(std::string filename);
 private:
    std::string _log_name;
    std::tuple<unsigned int, std::string> software_update_start;
};
(\\w{3}) (\\d{1,2}) (\\d{2}):(\\d{2}):(\\d{2}).*SOFTLOADSERVICE;Install started\\s*
在这里,您可以测试正则表达式和您自己:


试试
(\\s)(\\w{3})(\\d{1,2})(\\d{2}):(\\d{2})。*SOFTLOADSERVICE;Install started
是调试正则表达式“代码”(\\w{3})(\\d{1,2})(\\d{2}):(\\d{2}):(\\d{2})的一个非常好的工具.*SOFTLOADSERVICE;Install started没有给我任何表达式:/请编辑您的问题以包含-我们看不到您使用了哪个函数,您传递了什么标志等。nromer:继续@Slava所说的-用regexp和
main()编写一个小程序
函数,该函数匹配您希望匹配但不匹配的特定字符串(将其放入字符串常量中)。使用该代码更新您的问题。感谢提醒,我在哪里尝试匹配多行?很抱歉,我是boost regex库的新手(第一次使用它的作业)。我已经尽了最大努力查找文档,并认为\w{n}将匹配n个字母数字字符,而\d{n}将匹配n个数字。我尝试匹配的表达式只有一行,它显示为两行,因为我没有空间键入?我的格式设置可能会关闭,我将尝试修复它。@nromer:
std::getline()
给你一行。如果我的帖子不清楚,我很抱歉,我确实在尝试只匹配一行。我试图编辑我的第一篇帖子,让它更清晰一些。@nromer:啊,但是你的regexp中有一个换行文字!谢谢,这似乎很有效。正如我提到的,这是我第一次在gen中使用boost regex和正则表达式艾尔,谢谢你的帮助。