gpt4 book ai didi

c++ - 如何在 C++ 中使用正则表达式换行后不捕获空格

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:13:36 30 4
gpt4 key购买 nike

我试图从 c/c++/java 文件中捕获注释,但我找不到跳过新行后可能存在的空格的方法。我的正则表达式模式是

正则表达式 reg("(//.*|/\\*(.|\\n)*?\\*/)");

例如在下面的代码中(不要理会随机代码片段,它们可以是任何东西......)我正确地捕捉到了评论:

// my  program in C++
#include <iostream>
/** playing around in
a new programming language **/
using namespace std;

输出是:

// my  program in C++
/** playing around in
a new programming language **/

但是,当我在多行注释中使用空格代码时,例如:

int main(){
/* start always points to the first node of the linked list.
temp is used to point to the last node of the linked list.*/
node *start,*temp;
start = (node *)malloc(sizeof(node));
temp = start;
temp -> next = NULL;
temp -> prev = NULL;
/* Here in this code, we take the first node as a dummy node.
The first node does not contain data, but it used because to avoid handling special cases
in insert and delete functions.
*/
printf("1. Insert\n");

我捕获:

/* start always points to the first node of the linked list.
temp is used to point to the last node of the linked list.*/
/* Here in this code, we take the first node as a dummy node.
The first node does not contain data, but it used because to avoid handling special cases
in insert and delete functions.
*/

代替:

/* start always points to the first node of the linked list.
temp is used to point to the last node of the linked list.*/
/* Here in this code, we take the first node as a dummy node.
The first node does not contain data, but it used because to avoid handling special cases
in insert and delete functions.
*/

我怎样才能在正则表达式模式中绕过它来避免这种情况?

注意:如果可能的话,我想避免使用字符串操纵器等,只需修改正则表达式即可。

最佳答案

正在转换我上面的评论。

无法匹配不连续的文本。相反,您可以将文本的一部分与正则表达式匹配,然后使用另一个正则表达式或字符串操作对匹配(或捕获)的值进行后处理。

这是一个例子(不是最好的,只是为了展示这个概念):

string data("int main(){// Singleline content\n        /* start always points to the first node of the linked list.\n           temp is used to point to the last node of the linked list.*/\n        node *start,*temp;\n        start = (node *)malloc(sizeof(node));\n        temp = start;\n        temp -> next = NULL;\n        temp -> prev = NULL;\n        /* Here in this code, we take the first node as a dummy node.\n           The first node does not contain data, but it used because to avoid handling special cases\n           in insert and delete functions.\n         */\n        printf(\"1. Insert\n\");");
//std::cout << "Data: " << data << std::endl;
std::regex pattern(R"(//.*|/\*[^*]*\*+(?:[^/*][^*]*\*+)*/)");
std::smatch result;

while (regex_search(data, result, pattern)) {
std::cout << std::regex_replace(result[0].str(), std::regex(R"((^|\n)[^\S\r\n]+)"), "$1") << std::endl;
data = result.suffix().str();
}

参见 the IDEONE demo

注意:原始字符串文字简化了正则表达式定义。

R"(//.*|/\*[^*]*\*+(?:[^/*][^*]*\*+)*/)" 匹配 // + 任何 0+ 个字符,但换行符(单行注释)和 /\*[^*]*\*+(?:[^/*][^* ]*\*+)*/ 匹配 /* 后跟 0+ 个非 * 后跟 1+ 个 *后面跟着 0+ 个非 /* 字符序列,然后是 0+ 非 * 和 1+ *s(多行注释)。这个多行注释比你的注释更有效率,因为它是按照 acc 编写的。展开循环技术。

我用 regex_replace(result[0].str(), std::regex(R "((^|\n)[^\S\r\n]+)"), "$1"): (^|\n)[^\S\r\n]+ 匹配并捕获字符串开头 anchor 或换行符后跟除非空白、CR 和 LF 以外的 1+ 个字符。

关于c++ - 如何在 C++ 中使用正则表达式换行后不捕获空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36983464/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com