C++ C++;正则表达式行为

C++ C++;正则表达式行为,c++,regex,c++11,c++14,C++,Regex,C++11,C++14,我在regexr或regexpal中检查了这个简单的regex测试: \“[a-zA-Z\U 0-9-]+\”[\s]:[\s]([0-9]1[0-9]\“[a-zA-Z\U 0-9:\s-\,./\=;]1[a-zA-Z\U 0-9:\s-\,./\=;]\” 正文: “数据存储Maxconns”:100, “dataStoreName”:“cofax”, “dataStorePassword”:“dataStoreTestQuery”, “dataStoreTestQuery”:“设置无计数

我在regexr或regexpal中检查了这个简单的regex测试: \“[a-zA-Z\U 0-9-]+\”[\s]:[\s]([0-9]1[0-9]\“[a-zA-Z\U 0-9:\s-\,./\=;]1[a-zA-Z\U 0-9:\s-\,./\=;]\”

正文: “数据存储Maxconns”:100, “dataStoreName”:“cofax”, “dataStorePassword”:“dataStoreTestQuery”, “dataStoreTestQuery”:“设置无计数;选择test='test';”, “数据存储URL”:“jdbc:microsoft:sqlserver://LOCALHOST:1433;DatabaseName=goon“

结果:2场比赛

  • “数据存储Maxconns”:100
  • “jdbc:microsoft:sqlserver://LOCALHOST:1433;DatabaseName=goon“
  • 我尝试过C++和VS2010独立版:

     #include <iostream>
    #include <fstream>
    #include <string>
    #include <regex>
    #include <iterator>
    
    //using namespace std;
    
    int main()
    {
        std::string format_1;
        std::string main,dig,phrase;
        std::ifstream infile("noname-2");
        if(infile.good()==false)
            return false;
    
    
    
        main="\\\"[a-zA-Z_0-9\\-]+\\\"[\\s]*:[\\s]*";
        dig="[0-9]*1[0-9]*";
        phrase="\\\"[a-zA-Z_0-9:\\s\\-\\,\\.\/\\=;]*1[a-zA-Z_0-9:\\s\\-\\,\\.\/\\=;]*\\\"";
    
        format_1=main+"("+dig+"|"+phrase+")";
        std::string str((std::istreambuf_iterator<char>(infile)),std::istreambuf_iterator<char>());
        std::regex f_1(format_1,std::regex_constants::icase);
    
        std::smatch m_1;
        for(auto it = std::sregex_iterator(str.begin(), str.end(), f_1);it != std::sregex_iterator();++it)
        {
             std::cout<<it->str()<<std::endl;
        }
       return 0;
    }
    
    #包括
    #包括
    #包括
    #包括
    #包括
    //使用名称空间std;
    int main()
    {
    std::字符串格式_1;
    std::stringmain、dig、短语;
    标准::ifstream infle(“noname-2”);
    if(infle.good()==false)
    返回false;
    main=“\\\”[a-zA-Z_0-9\\-]+\\\”[\\s]*:[\\s]*”;
    dig=“[0-9]*1[0-9]*”;
    短语=“\\\”[a-zA-Z\U 0-9:\\s\\-\,\.\/\=;]*1[a-zA-Z\U 0-9:\\s\-\,\.\/\=;]*\\”;
    格式_1=main+“(“+dig+”|“+phrase+”)”;
    std::string str((std::istreambuf_迭代器(infle)),std::istreambuf_迭代器();
    std::regex f_1(格式_1,std::regex_常量::icase);
    标准::smatch m_1;
    for(auto it=std::sregex_迭代器(str.begin(),str.end(),f_1);it!=std::sregex_迭代器();+it)
    {
    
    STD::CUT< P>您需要删除不必要的逃逸符号,并确保<代码> > /COD>符号放置在字符类的开始/结束(当逃逸时,在C++ STD中::ReGEX并不能真正处理它作为文字字符)。 使用

    见:

    #包括
    #包括
    #包括
    int main()
    {
    std::string main2=“\”[a-zA-Z_0-9-]+\”\\s*:\\s*”;
    std::string dig=“[0-9]*1[0-9]*”;
    std::string phrase=“\”[a-zA-Z\u 0-9:\\s,./=;\\\-]*1[a-zA-Z\u 0-9:\\s,./=;\\\-]*\”;
    std::字符串格式_1=main2+“(“+dig+”|“+phrase+”);
    std::string str(“\”dataStoreMaxConns\”:100、\n\“dataStoreName\”:\“cofax\”、\n\“dataStorePassword\”:\“dataStoreTestQuery\”、\n\“dataStoreTestQuery\”:\“SET NOCOUNT ON;select test='test';\”、\n\“dataStoreUrl\”:“jdbc:microsoft:sqlserver://LOCALHOST:1433;DatabaseName=goon\“”);
    std::regex f_1(格式_1,std::regex_常量::icase);
    标准::smatch m_1;
    for(auto it=std::sregex_迭代器(str.begin(),str.end(),f_1);it!=std::sregex_迭代器();+it)
    {
    
    STD::CUT< P>您需要删除不必要的逃逸符号,并确保<代码> > /COD>符号放置在字符类的开始/结束(当逃逸时,在C++ STD中::ReGEX并不能真正处理它作为文字字符)。 使用

    见:

    #包括
    #包括
    #包括
    int main()
    {
    std::string main2=“\”[a-zA-Z_0-9-]+\”\\s*:\\s*”;
    std::string dig=“[0-9]*1[0-9]*”;
    std::string phrase=“\”[a-zA-Z\u 0-9:\\s,./=;\\\-]*1[a-zA-Z\u 0-9:\\s,./=;\\\-]*\”;
    std::字符串格式_1=main2+“(“+dig+”|“+phrase+”);
    std::string str(“\”dataStoreMaxConns\”:100、\n\“dataStoreName\”:\“cofax\”、\n\“dataStorePassword\”:\“dataStoreTestQuery\”、\n\“dataStoreTestQuery\”:\“SET NOCOUNT ON;select test='test';\”、\n\“dataStoreUrl\”:“jdbc:microsoft:sqlserver://LOCALHOST:1433;DatabaseName=goon\“”);
    std::regex f_1(格式_1,std::regex_常量::icase);
    标准::smatch m_1;
    for(auto it=std::sregex_迭代器(str.begin(),str.end(),f_1);it!=std::sregex_迭代器();+it)
    {
    
    std::coutRemove不必要的转义符并将
    -
    始终放在字符类的开始/结束处-原始字符串可能有助于
    “(“[a-zA-Z_0-9-]+”\s*:\s*)”
    。移除不必要的转义符并将
    -
    始终放在字符类的开始/结束处-原始字符串可能有助于
    ”(“[a-zA-Z_0-9-]+”\s*:\s*)”
    .Raw string可能有助于
    ”(“[a-zA-Z_0-9-]+”\s*:\s*)“
    @Jarod42:是的,为什么不呢,这是最佳实践()。主要问题是
    -
    应该放在字符类的末尾。谢谢!但是对于json.org/example.html中的第四个示例,“\”dataStoreUrl\”:\“jdbc:microsoft:sqlserver://LOCALHOST:1433未检测到“;DatabaseName=goon\”。原因可能是什么?您有
    \/\/
    -您也需要匹配
    \
    字符。修复。原始字符串可能有助于
    ”(“[a-zA-Z_0-9-]+“\s*:\s*)”
    @Jarod42:是的,为什么不,这是最佳做法()。主要的问题是
    -
    应该放在字符类的末尾。谢谢!但是对于json.org/example.html中的第四个示例,“\”dataStoreUrl\”:\“jdbc:microsoft:sqlserver://LOCALHOST:1433;DatabaseName=goon\“'未检测到。原因可能是什么?您有
    \/\/
    -您还需要匹配
    \
    字符。修复。
    std::string main2="\"[a-zA-Z_0-9-]+\"\\s*:\\s*";
    std::string dig="[0-9]*1[0-9]*";
    std::string phrase="\"[a-zA-Z_0-9:\\s,./=;\\\\-]*1[a-zA-Z_0-9:\\s,./=;\\\\-]*\"";
    
    #include <iostream>
    #include <string>
    #include <regex>
    
    int main()
    {
        std::string main2="\"[a-zA-Z_0-9-]+\"\\s*:\\s*";
        std::string dig="[0-9]*1[0-9]*";
        std::string phrase="\"[a-zA-Z_0-9:\\s,./=;\\\\-]*1[a-zA-Z_0-9:\\s,./=;\\\\-]*\"";
    
        std::string format_1=main2+"("+dig+"|"+phrase+")";
        std::string str("\"dataStoreMaxConns\" : 100,\n\"dataStoreName\" : \"cofax\",\n\"dataStorePassword\" : \"dataStoreTestQuery\",\n\"dataStoreTestQuery\" : \"SET NOCOUNT ON;select test='test';\",\n\"dataStoreUrl\" : \"jdbc:microsoft:sqlserver://LOCALHOST:1433;DatabaseName=goon\"");
        std::regex f_1(format_1,std::regex_constants::icase);
    
        std::smatch m_1;
        for(auto it = std::sregex_iterator(str.begin(), str.end(), f_1);it != std::sregex_iterator();++it)
        {
             std::cout<<it->str()<<std::endl;
        }
       return 0;
    }