Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/134.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.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,我试图实现简单的case,基本上是在两个标记之间查找文本,不管它们是什么。 我要排队 /*我的评论1*/ #include <iostream> #include <string> #include <regex> int main(int argc, const char * argv[]) { std::string str = "Hello /* my comment 1 */ world /* my comment 2 */ of /* my

我试图实现简单的case,基本上是在两个标记之间查找文本,不管它们是什么。 我要排队

/*我的评论1*/

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

int main(int argc, const char * argv[])
{
    std::string str = "Hello /* my comment 1 */ world /* my comment 2 */ of /* my comment 3 */ cpp";

    std::cmatch res;
    std::regex rx("/\\*(.*)\\*/");

    std::regex_search(str.c_str(), res, rx);

    for (int i = 0; i < sizeof(res) / sizeof(res[0]); i++) {
        std::cout << res[i] << std::endl;
    }

    return 0;
}
/*我的评论2*/

/*我的评论3*/

作为输出。似乎我需要将捕获组限制为1?因为在字符串Hello/*my comment 1*/world上,我得到了我想要的东西-res[0]包含/*my comment 1*/

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

int main(int argc, const char * argv[])
{
    std::string str = "Hello /* my comment 1 */ world /* my comment 2 */ of /* my comment 3 */ cpp";

    std::cmatch res;
    std::regex rx("/\\*(.*)\\*/");

    std::regex_search(str.c_str(), res, rx);

    for (int i = 0; i < sizeof(res) / sizeof(res[0]); i++) {
        std::cout << res[i] << std::endl;
    }

    return 0;
}

通过将量词*转换为其版本,使正则表达式仅与第一次出现的*/匹配。这是通过在后面添加问号来实现的:

std::regex rx("/\\*(.*?)\\*/");

通过将量词*转换为其版本,使正则表达式仅与第一次出现的*/匹配。这是通过在后面添加问号来实现的:

std::regex rx("/\\*(.*?)\\*/");

要澄清问题,请写下您现在得到的。可能的副本等。要澄清问题,请写下您现在得到的。可能的副本等。感谢您的帮助-由于奇怪的新规则,我只能在8分钟冷却后接受:对于某些正则表达式引擎,对于。要匹配换行符,也可以将其更改为:[\s\s],然后它也将匹配换行符。因此,在Python中,这将是:multi\u line\u comment=re.compiler/\*[\s\s]*?\*/\s*,re.multilithanks有帮助-由于奇怪的新规则,我只能在8分钟冷却后接受:对于某些正则表达式引擎,对于。要匹配换行符,也可以将其更改为:[\s\s],然后它也将匹配换行符。所以在Python中,这应该是:multi\u line\u comment=re.compiler/\*[\s\s]*?\*/\s*,re.MULTILINE