Results 1 to 5 of 5

Thread: QTextDocument find and regular expresion

  1. #1
    Join Date
    Dec 2010
    Posts
    28
    Thanks
    6
    Thanked 10 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question QTextDocument find and regular expresion

    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

    Qt Code:
    1. QTextCursor lOrgPosition = aEditor->textCursor();
    2. //QRegExp lRe(QString("(?: |\\t)+") + QChar(0x2029));
    3. QRegExp lRe("(?: |\\t)+\\n");
    4. lRe.setMinimal(true);
    5.  
    6. QTextDocument* lDoc = aEditor->textCursor().document();
    7. QTextCursor lCursor = lDoc->find(lRe, 0);
    8. while(!lCursor.isNull())
    9. {
    10. lCursor.removeSelectedText();
    11. lCursor = lDoc->find(lRe, lCursor.position());
    12. }
    13. aEditor->setTextCursor(lOrgPosition);
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: QTextDocument find and regular expresion

    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/.

    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:

    Qt Code:
    1. (^\s)*(\s+)$
    To copy to clipboard, switch view to plain text mode 

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

    Qt Code:
    1. QRegExp re("(^\\s)*(\\s+)$");
    To copy to clipboard, switch view to plain text mode 

    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:


    1. Extract text out of QTextDocument
    2. Use QString::replace() which does work with the QRegExp above
    3. 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
    Last edited by jefftee; 11th December 2014 at 23:12.

  3. The following user says thank you to jefftee for this useful post:

    oficjalne100 (11th December 2014)

  4. #3
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: QTextDocument find and regular expresion

    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():

    Qt Code:
    1. QRegExp re("([^\\s]*)(\\s+)\\n");
    2. QString text = "Line with white space \t \nLine w/o trailing white space\nLine with trailing spaces \n";
    3. text.replace(re, "\\1\\n");
    To copy to clipboard, switch view to plain text mode 

    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

  5. #4
    Join Date
    Dec 2010
    Posts
    28
    Thanks
    6
    Thanked 10 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTextDocument find and regular expresion

    Quote Originally Posted by jthomps View Post
    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!

  6. #5
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: QTextDocument find and regular expresion

    Quote Originally Posted by oficjalne100 View Post
    How ever your first proposal seems to work fine. So I use it. Thanks!
    Great, glad to hear it!

Similar Threads

  1. Regular expression
    By aguleo in forum Newbie
    Replies: 0
    Last Post: 25th January 2013, 16:00
  2. Replies: 1
    Last Post: 28th May 2012, 16:06
  3. Regular expression
    By QFreeCamellia in forum Newbie
    Replies: 8
    Last Post: 30th December 2011, 23:34
  4. Regular expression help!
    By ConkerX in forum Qt Programming
    Replies: 10
    Last Post: 31st August 2011, 16:47
  5. Find with Regular Expression?
    By vishal.chauhan in forum Qt Programming
    Replies: 1
    Last Post: 1st August 2007, 15:44

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.