Results 1 to 10 of 10

Thread: QString find Whole word Only

  1. #1
    Join Date
    Jan 2006
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QString find Whole word Only

    Hi,

    What is the equalvalent function of
    bool QTextEdit::find("TEST",QTextDocument::FindWholeWor ds)
    in QString ?


    Regards
    Teh

  2. #2
    Join Date
    Jan 2006
    Location
    Ukraine,Lviv
    Posts
    454
    Thanks
    9
    Thanked 27 Times in 27 Posts
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: QString find Whole word Only

    a life without programming is like an empty bottle

  3. #3
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QString find Whole word Only

    One approach could be to use

    int QString::indexOf ( const QRegExp & rx, int from = 0 ) const

    and define the regexp to begin and end with a whitespace (\s).

  4. #4
    Join Date
    Feb 2006
    Location
    Kirovohrad, Ukraine
    Posts
    72
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QString find Whole word Only

    Quote Originally Posted by jpn
    One approach could be to use

    int QString::indexOf ( const QRegExp & rx, int from = 0 ) const

    and define the regexp to begin and end with a whitespace (\s).
    To my mind you should use "\\b" instead of "\\s".
    \b
    A word boundary. For example the regexp \bOK\b means match immediately after a word boundary (e.g. start of string or whitespace) the letter 'O' then the letter 'K' immediately before another word boundary (e.g. end of string or whitespace). But note that the assertion does not actually match any whitespace so if we write (\bOK\b) and we have a match it will only contain 'OK' even if the string is "It's OK now".

  5. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QString find Whole word Only

    Cesar, thanks for correcting. You're exactly right..

  6. #6
    Join Date
    Feb 2006
    Location
    Kirovohrad, Ukraine
    Posts
    72
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QString find Whole word Only

    You're welcome I hope that helped

  7. #7
    Join Date
    Jan 2006
    Location
    Ukraine,Lviv
    Posts
    454
    Thanks
    9
    Thanked 27 Times in 27 Posts
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: QString find Whole word Only

    hm..i think that bcteh_98 will be confused after read answers
    a life without programming is like an empty bottle

  8. #8
    Join Date
    Jan 2006
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QString find Whole word Only

    Thank it work fine. But I still have a small problem.

    regexp = \bOK\b

    "I am OK_Now" Index of Match -1 ok fine
    "I am (OK) Now" Index of Match 7 ok fine
    "I am OK@Now" Index of Match 5 ok fine
    =================================
    "I am NOT-OK Now" Index of Match 9
    "I am OK-Now" Index of Match 5
    I don't want the last two result to match,
    how to write a regular expressions to make it not Match -1.

    Regards
    Teh

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QString find Whole word Only

    Use a regexp of "[ \t\n]OK[ \t\n]". Or: "[^ \t\n]+" which will match a sequence of characters not containing a space, tab or newline in general.

  10. #10
    Join Date
    Jun 2018
    Posts
    2
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: QString find Whole word Only

    I had to find words like @name_of_variable in css files (exactly qss file for a styleSheet setting). That's why I did not want to replace the wrong member! And so I had to find the members as whole words. For that I used:

    Qt Code:
    1. styleSheet = styleSheet.replace(QRegExp("[^a-zA-Z\\d_]" + name + "[^a-zA-Z\\d_]", Qt::CaseSensitive), " " + value + "; ");
    To copy to clipboard, switch view to plain text mode 

    Maybe it can helps someone!
    Last edited by peter-70; 7th December 2018 at 10:05.

Similar Threads

  1. QString - no member function called 'find'
    By pitterb in forum Newbie
    Replies: 1
    Last Post: 13th January 2009, 12:31
  2. How to fhind whole word only in QString
    By sawerset in forum Qt Programming
    Replies: 2
    Last Post: 12th December 2008, 06:18
  3. problems installing Qt opensource with msvc2008 support
    By odin1985 in forum Installation and Deployment
    Replies: 6
    Last Post: 24th May 2008, 10:06
  4. Replies: 11
    Last Post: 11th October 2007, 17:34
  5. Convert from iso-8859-1 to... Something else :-)
    By Nyphel in forum Qt Programming
    Replies: 4
    Last Post: 7th March 2007, 18:59

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.