PDA

View Full Version : Finding whole word in QString ?



rajesh
20th November 2009, 08:17
How to check whole word in QString ?
In Find Files example given in Qt assistance , How to add Match whole word setting.


QFile file(directory.absoluteFilePath(files[i]));

if (file.open(QIODevice::ReadOnly)) {
QString line;
QTextStream in(&file);
while (!in.atEnd()) {
if (progressDialog.wasCanceled())
break;
line = in.readLine();
if (line.contains(text)) -->How to check whole word in QString ?:confused:
{
foundFiles << files[i];
break;
}
}
}



Must Watch::eek: See how we can connect digital world objects to our day to day life…. (http://www.tech2fame.info/2009/11/pranav-mistry-thrilling-potential-of.html)

vieraci
20th November 2009, 08:29
Look at using QRegExp.

spirit
20th November 2009, 08:36
doesn't QString::contains work for you?

rajesh
20th November 2009, 09:22
QString::contains dont check "whole word " condition.

vieraci
20th November 2009, 09:52
QString::contains dont check "whole word " condition.
QRegExp does, take a look.

rajesh
20th November 2009, 11:40
I didn't get any examples of QRegEdit to solve my problem.
how to use
bool QRegExp::exactMatch ( const QString & str ) const

if I use QRegExp then if user uncheck regularExp box then how to use QRegExp.
see the attach dialog.



my problem is solved with following code:

QTextDocument document(line);
cursor = regularExp ? document.find(findText, cursor, myFlags):document.find(text, cursor, myFlags);

JohannesMunk
20th November 2009, 12:26
QRegExp with "\b"+searchStr+"\b" as pattern should do the trick. \b searches for word boundaries.

HIH

Johannes