PDA

View Full Version : QString find Whole word Only



bcteh_98
10th February 2006, 10:17
Hi,

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


Regards
Teh

zlatko
10th February 2006, 10:22
look here......... (http://doc.trolltech.com/4.1/qstring.html#contains)

jpn
10th February 2006, 10:25
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).

Cesar
10th February 2006, 11:04
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".

jpn
10th February 2006, 11:20
Cesar, thanks for correcting. You're exactly right..

Cesar
10th February 2006, 11:23
You're welcome :) I hope that helped

zlatko
10th February 2006, 11:28
hm..i think that bcteh_98 will be confused after read answers:)

bcteh_98
10th February 2006, 15:52
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

wysota
10th February 2006, 16:23
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.

peter-70
7th December 2018, 08:47
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:


styleSheet = styleSheet.replace(QRegExp("[^a-zA-Z\\d_]" + name + "[^a-zA-Z\\d_]", Qt::CaseSensitive), " " + value + "; ");

Maybe it can helps someone!