PDA

View Full Version : QTextDocument find and regular expresion



oficjalne100
11th December 2014, 10:41
Hello
I write text editor and I want to remove trailing spaces from document before save. I think the right way is to use
QTextCursor QTextDocument::find(const QRegExp & expr, int position = 0, FindFlags options = 0) const
for that. So I write regular expression and I tested it in separated program (based on regexp example from qt framework). But sadly it not works and I don't known why. It seems that problem lies in \n character. My code is below (both regexps don't work). Please tell me what I am doing wrong.

best regards
Szyk Cech



QTextCursor lOrgPosition = aEditor->textCursor();
//QRegExp lRe(QString("(?: |\\t)+") + QChar(0x2029));
QRegExp lRe("(?: |\\t)+\\n");
lRe.setMinimal(true);

QTextDocument* lDoc = aEditor->textCursor().document();
QTextCursor lCursor = lDoc->find(lRe, 0);
while(!lCursor.isNull())
{
lCursor.removeSelectedText();
lCursor = lDoc->find(lRe, lCursor.position());
}
aEditor->setTextCursor(lOrgPosition);

jefftee
11th December 2014, 19:48
For new development, you should be using QRegularExpression instead of QRegExp IMHO. QRegularExpression is more robust and since it's compatible with perl regular expressions, you can easily test your regular expressions at the site https://regex101.com/ (https://regex101.com).

That said, I don't see where QTextDocument::find() has an overload that accepts a QRegularExpression, so you may well be stuck using QRegExp. Give this a try, I know it works with QRegularExpression and it's simple enough that hopefully it works with QRegExp too:



(^\s)*(\s+)$


Because you need to use this in a QString, you have to escape the backslashes as follows to use in QRegularExpression or QRegExp:



QRegExp re("(^\\s)*(\\s+)$");


For this regular expression, the first matching group contains everything from the beginning of the line up to the end of that string that is comprised of white space only. The second matching group contains only the trailing white space values.

Give that a try and see if it helps.

Edit: Removing incorrect statement about QRegExp not operating on multi-line strings. Even though I can use QRegExp to remove white space at the end of lines, the QTextDocument::find() method does not appear to work with the QRegExp for some reason. A couple of approaches I can think of would be to:



Extract text out of QTextDocument
Use QString::replace() which does work with the QRegExp above
Replace contents of QTextDocument using the new string


Obviously for large documents, this would not be ideal, but may be an option for your use case. If you are saving the file when shutting down, you would not need to replace the QTextDocument contents obviously. Also, if you take this approach, once you extract the string out of QTextDocument, you might as well use QRegularExpression.

Hope that helps.

Regards,

jthomps

jefftee
11th December 2014, 23:01
I finally got around to actually testing some code. I can't make the QTextDocument::find() method work with the regular expression, however the same QRegExp works for QString::replace():



QRegExp re("([^\\s]*)(\\s+)\\n");
QString text = "Line with white space \t \nLine w/o trailing white space\nLine with trailing spaces \n";
text.replace(re, "\\1\\n");


Unless someone else has any ideas, you may just have to extract text from your QTextDocument when you're saving it, use QString::replace() with the QRegExp above, then save new string to file, etc.

Regards,

jthomps

oficjalne100
12th December 2014, 10:27
extract text from your QTextDocument when you're saving it, use QString::replace() with the QRegExp above, then save new string to file, etc.

I did that way at first time, but then undo history is lost and it is not acceptable. How ever your first proposal seems to work fine. So I use it. Thanks!

jefftee
12th December 2014, 23:15
How ever your first proposal seems to work fine. So I use it. Thanks!
Great, glad to hear it!