[英]How to not capture whitespaces after a new line with regex in c++
我试图从 c/c++/java 文件中捕获注释,但我找不到跳过新行后可能存在的空格的方法。 我的正则表达式模式是
regex 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();
}
注意:原始字符串文字简化了正则表达式定义。
R"(//.*|/\\*[^*]*\\*+(?:[^/*][^*]*\\*+)*/)"
匹配//
+ 任意 0+ 个字符但是换行符(单行注释)和/\\*[^*]*\\*+(?:[^/*][^*]*\\*+)*/
匹配/*
后跟 0+ 非*
s带有 1+ *
s,后跟 0+ 字符序列,而不是/
和*
,然后是 0+ 非*
,然后是 1+ *
s(多行注释)。 这个多行注释比你的多行注释高效得多,因为它是写成 acc 的。 到展开循环技术。
我用regex_replace(result[0].str(), std::regex(R"((^|\\n)[^\\S\\r\\n]+)"), "$1")
删除了一行上的第一个水平空格regex_replace(result[0].str(), std::regex(R"((^|\\n)[^\\S\\r\\n]+)"), "$1")
: (^|\\n)[^\\S\\r\\n]+
匹配并捕获字符串开头的锚点或后跟 1 个以上字符的换行符,而不是非空白、CR 和如果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.