简体   繁体   English

C ++正则表达式行为

[英]C++ regex behavior

I have check this simple regex test in regexr or regexpal : \\"[a-zA-Z_0-9-]+\\"[\\s] :[\\s] ([0-9] 1[0-9] |\\"[a-zA-Z_0-9:\\s-\\,./\\=;] 1[a-zA-Z_0-9:\\s-\\,./\\=;] \\") 我在regexr或regexpal中检查了这个简单的正则表达式测试: \\“[a-zA-Z_0-9 - ] + \\”[\\ s] :[\\ s] ([0-9] 1 [0-9] | \\ “[a-zA-Z_0-9:\\ s - \\,。/ \\ =;] 1 [a-zA-Z_0-9:\\ s - \\,。/ \\ =;] \\”)

text : "dataStoreMaxConns" : 100, "dataStoreName" : "cofax", "dataStorePassword" : "dataStoreTestQuery", "dataStoreTestQuery" : "SET NOCOUNT ON;select test='test';", "dataStoreUrl" : "jdbc:microsoft:sqlserver://LOCALHOST:1433;DatabaseName=goon" text: “dataStoreMaxConns”:100,“dataStoreName”:“cofax”,“dataStorePassword”:“dataStoreTestQuery”,“dataStoreTestQuery”:“SET NOCOUNT ON; select test ='test';”,“dataStoreUrl”:“jdbc:microsoft :SQLSERVER:// LOCALHOST:1433;数据库名=打手”

Result : 2 matches 结果:2场比赛

  1. "dataStoreMaxConns" : 100 “dataStoreMaxConns”:100
  2. "jdbc:microsoft:sqlserver://LOCALHOST:1433;DatabaseName=goon" “JDBC:微软:SQLSERVER:// LOCALHOST:1433;数据库名=打手”

I have try with c++ any in https://wandbox.org/ and vs2010 standalone: 我在https://wandbox.org/和vs2010 standalone中尝试使用c ++:

 #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;
}

nonname-2 contains the text nonname-2包含文本

Result is only one: "dataStoreMaxConns" : 100 结果只有一个: “dataStoreMaxConns”:100

Why? 为什么?

You need to remove unnecessary escape symbols and make sure the - symbol is placed at the start/end of the character class (when escaped, in C++ std::regex does not really handle it as a literal char). 你需要删除不必要的转义符号,并确保-符号放在字符类的开头/结尾(当转义时,在C ++中,std :: regex并不真正处理它作为文字字符)。

Use 采用

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,./=;\\\\-]*\"";

See the C++ demo : 请参阅C ++演示

#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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM