Problem: I wrote a regular expression that looks for '{' and looks back to see if there is a sign of comments, and when it finds a given character that is not preceded by a comment, it replaces it with another sequence of characters. But alas, it does not work already what time it is. Be kind, friends, tell me where was wrong:

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; } 

let's say there is a text like:

 //dfsdkfj ksjdfksjdf { <- этот символ должен быть пропущен public SystemBlock() { <- этот должен быть найден this.producer = "none"; this.motherBoard = "none"; this.processor = "none"; this.ram = "none"; this.gpu = "none"; this.price = 0; this.eventSupport = null; } 
  • Give the minimum fragment, which does not work. - PinkTux
  • @ixSci version 5.7 - Max

1 answer 1

I do not think that you will be able to do this with regular expressions, since PCRE does not support negative lookbehind of arbitrary size, and how else to do here is not entirely clear. On the other hand, your task is solved in an elementary way: you divide the text into lines and search for each line { if you find, search for this line // (or another comment character): if you have, then skip, if not, replace .

  • I had an idea to split into lines, but then I decided to play with regular expressions. Apparently, yes, you have to either cut into lines, or use \ n and indexOf || lastIndexOf. Anyway, thanks for the feedback.) - Max
  • @Max, if you agree with the answer, please mark it as accepted. Often, the simplest code based on the same indexOf() and lastIndexOf() is also the most effective. In Qt, also, for good reason, there are classes, such as QStringMatcher and QByteArrayMatcher , which in some cases allow, without any regulars, to significantly increase the efficiency of character search. - alexis031182