正则表达式qt中的条件
condition in regular expression qt
我写了一个正则表达式来查找范围('{'|'}')。我想找到没有评论的范围,但我不明白我在哪里犯了错误。下面是一段代码:
while (start >= 0 && end >=0) {
start = text.indexOf("n", start);
end = text.indexOf(QRegExp("^.*[{](?!=[//]|[/*]|[*]))"),start);
end2 = text.indexOf(QRegExp("^.*[}](?!=[//]|[/*]|[*]))"), start);
if (end < end2) {
text.replace(end,"n{n");
}
else text.replace(end2,"n}n");
++start;
}
例如,我们有一些文本:
//dfsdkfj ksjdfksjdf { <- this symbol should be skipped
public SystemBlock()
{ //<- this symbol should be found.
this.producer = "none";
this.motherBoard = "none";
this.processor = "none";
this.ram = "none";
this.gpu = "none";
this.price = 0;
this.eventSupport = null;
}
首先,用正则表达式正确解析c++代码通常是不可能的。想想嵌套注释、行延续、包含"//"
、"/*"
等的字符串!
但是您的特定示例可以用像这样的正则表达式解析:^(?!(//|/*))*{
:
end = text.indexOf(QRegExp("^(?!(//|/\*))*\{"),start);
end2 = text.indexOf(QRegExp("^(?!(//|/\*))*\}"), start);
相关文章:
- 为什么"do while"循环不断退出,即使条件计算结果为 false?
- Qt VTK交互风格的信号到小部件
- QT在错误的班级中寻找空位
- 如何在选项卡视图Qt中设置一个新项目,并保存以前的项目
- 在没有太多条件句的情况下,我如何避免被零除
- 如何在Qt窗口小部件中使用QStringView(或QStringRef)
- 如何在Qt C++中更改光标
- 基于多个条件处理地图中的所有元素
- 如何仅在Qt是用ltcg构建时才有条件地启用ltcg?
- Qt qml - 在没有任何条件的情况下运行一行(while(true))
- QT条件信号发射效率
- QT Pro文件条件液体
- 关于C/C /QT中的条件构建(宏)
- 为什么在使用qt等待条件时不必包装lock/unlock语句
- Q等待条件,手动复位除外?(或者在Qt Concurrent之外创建QFuture对象?)
- 如果满足条件,如何退出Qt脚本
- 正则表达式qt中的条件
- Qt QTreeView -当条件不满足时恢复到以前的选择
- Qt小部件在某些条件下无法重新绘制
- Qt或C++检查NULL条件