我寫了一個用於查找範圍的正則表達式('{'|'}')。我想找到沒有評論的範圍,但我不明白我犯的錯誤。下面是代碼片段:正則表達式中的條件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;
}
起初,逃避花括號:'\\ {'和 '\\}'。他們可能被解釋爲量化者。 – ilotXXI