C++ C++;正则表达式匹配不起作用

C++ C++;正则表达式匹配不起作用,c++,regex,c++11,C++,Regex,C++11,这是我的部分代码 bool CSettings::bParseLine ( const char* input ) { //_asm INT 3 std::string line ( input ); std::size_t position = std::string::npos, comment; regex cvarPattern ( "\\.([a-zA-Z_]+)" ); regex parentPattern ( "^([a-zA-Z0-

这是我的部分代码

bool CSettings::bParseLine ( const char* input )
{
    //_asm INT 3


    std::string line ( input );
    std::size_t position = std::string::npos, comment;

    regex cvarPattern ( "\\.([a-zA-Z_]+)" );
    regex parentPattern ( "^([a-zA-Z0-9_]+)\\." );
    regex cvarValue ( "\\.[a-zA-Z0-9_]+[ ]*=[ ]*(\\d+\\.*\\d*)" );
    std::cmatch matchedParent, matchedCvar;


    if ( line.empty ( ) )
        return false;

    if ( !std::regex_match ( line.c_str ( ), matchedParent, parentPattern ) )
        return false;

    if ( !std::regex_match ( line.c_str ( ), matchedCvar, cvarPattern ) )
        return false;
...
}
我尝试将从文件行中读取的行与it分离-看起来像:

foo.bar = 15
baz.asd = 13
ddd.dgh = 66
我想从中提取部分-例如,对于第1行foo.bar=15,我想得到如下结果:

a = foo
b = bar
c = 15

<>但是,ReGEX总是返回false,我在很多在线ReGeX检查器上测试,甚至在VisualStudio上测试过,它的工作很好,我需要C++ C++ RexExk匹配的一些不同语法吗?我正在使用visual studio 2013社区来关注我更喜欢在c++中使用的
regex
模式:

<> > <代码> RGX( <代码>)RGX < /Calp>分隔符不需要任何额外的C++字符字符转义。


实际上,您在问题中编写的内容类似于我作为原始字符串文字编写的正则表达式。
你可能只是想说

regex cvarPattern ( R"rgx(.([a-zA-Z_]+))rgx" );
regex parentPattern ( R"rgx(^([a-zA-Z0-9_]+).)rgx" );
regex cvarValue ( R"rgx(.[a-zA-Z0-9_]+[ ]*=[ ]*(\d+(\.\d*)?))rgx" );
我没有深入研究,但我现在不会在正则表达式模式中获得所有这些转义字符


对于注释中的问题,您可以选择匹配的子模式组,并检查其中哪一个应用于匹配结构:

regex cvarValue ( 
   R"rgx(.[a-zA-Z0-9_]+[ ]*=[ ]*((\d+)|(\d+\.\d?)|([a-zA-Z]+)){1})rgx" );
                             // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
您可能不需要这些
cvarPattern
parentPattern
正则表达式来检查有关匹配模式的其他(更详细的)视图。

问题是必须匹配整个字符串,但您只尝试匹配其中的一部分

您需要使用或更改正则表达式以同时匹配所有三个部分:

#include <regex>
#include <string>
#include <iostream>

const auto test =
{
      "foo.bar = 15"
    , "baz.asd = 13"
    , "ddd.dgh = 66"
};

int main()
{
    const std::regex r(R"~(([^.]+)\.([^\s]+)[^0-9]+(\d+))~");
    //                     (  1  )  (   2  )       ( 3 ) <- capture groups

    std::cmatch m;

    for(const auto& line: test)
    {
        if(std::regex_match(line, m, r))
        {
            // m.str(0) is the entire matched string
            // m.str(1) is the 1st capture group
            // etc...
            std::cout << "a = " << m.str(1) << '\n';
            std::cout << "b = " << m.str(2) << '\n';
            std::cout << "c = " << m.str(3) << '\n';
            std::cout << '\n';
        }
    }
}

你用的是什么编译器和stdlib?你必须双重转义backslashes@HAL9000它们不是被正确地转义了吗?@HAL9000我认为他/她不想匹配字面上的反斜杠。@StefanoSanfilippo完全忘记了这一点,我编辑了这个问题-我正在使用visual studio 2013I添加\。在我的正则表达式模式中,因为也可能有浮点值,比如2.5,我可能犯了一些错误,因为我编辑了它times@mlgpro您需要有一个组的选择,如
(\d+)|(\d+\。\d?)|([a-zA-Z]+)
等等。
#include <regex>
#include <string>
#include <iostream>

const auto test =
{
      "foo.bar = 15"
    , "baz.asd = 13"
    , "ddd.dgh = 66"
};

int main()
{
    const std::regex r(R"~(([^.]+)\.([^\s]+)[^0-9]+(\d+))~");
    //                     (  1  )  (   2  )       ( 3 ) <- capture groups

    std::cmatch m;

    for(const auto& line: test)
    {
        if(std::regex_match(line, m, r))
        {
            // m.str(0) is the entire matched string
            // m.str(1) is the 1st capture group
            // etc...
            std::cout << "a = " << m.str(1) << '\n';
            std::cout << "b = " << m.str(2) << '\n';
            std::cout << "c = " << m.str(3) << '\n';
            std::cout << '\n';
        }
    }
}
a = foo
b = bar
c = 15

a = baz
b = asd
c = 13

a = ddd
b = dgh
c = 66