Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/147.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
正则表达式行首不包含任何字符,仅包含注释 我试图删除正则表达式C++源文件中的任何注释。_C++_Regex - Fatal编程技术网

正则表达式行首不包含任何字符,仅包含注释 我试图删除正则表达式C++源文件中的任何注释。

正则表达式行首不包含任何字符,仅包含注释 我试图删除正则表达式C++源文件中的任何注释。,c++,regex,C++,Regex,下面是一个代码示例: std::regex single_regex("(//.*)"); std::regex multi_regex("/\\*.*?"); std::regex multi_close_regex("\\*/"); 下面是示例代码: /* multi line comment */ //single line comment some code //another single line comment some other code //comment 下面是我进

下面是一个代码示例:

std::regex single_regex("(//.*)");
std::regex multi_regex("/\\*.*?");
std::regex multi_close_regex("\\*/");
下面是示例代码:

/* multi line
comment 
*/

//single line comment
some code
//another single line comment
some other code //comment
下面是我进行迭代的代码:

 void CommentStrip::GetLines(const std::string &line, size_t &single_comm, size_t &multi_comm, const std::string &language, bool &flag)
    {
        if (language == "C" || language == "C++" || language == "C/C++ Header") {

            std::regex single_regex("(//.*)");
            std::regex multi_regex("/\\*.*?");
            std::regex multi_close_regex("\\*/");

            std::string tmp_line;

            if (!line.empty()) {
                if (std::regex_search(line,single_regex))
                    single_comm++;
                if(std::regex_search(line,multi_regex))
                    flag = true;
                if(flag)
                    multi_comm++;
                if(std::regex_search(line, multi_close_regex) && flag == true)
                    flag = false;
            }

        }
    }
}
我的问题是,当前正则表达式将最后一行计算为注释,例如(其他一些代码
/
注释),但我不想


有没有办法修改我的正则表达式,使其忽略任何不以注释开头的代码行(
/
/*..*/
)?

将正则表达式修改为
std::regex single_regex((^/.*))
这样它将在行首搜索
“//”

要只在行首找到注释,您可以使用^。

您必须将代码发布到实际迭代的地方。是的,很抱歉,我现在将编辑,我应该如何做?你能给我举个例子吗?