vladozar
5th December 2012, 19:57
I'm writing a small search function for my program.
I am able to run a search that will bring results for any word in the search string. Here is what i have
QString search_text = ui->lineEditSearch->text();
QRegExp rx;
rx.setCaseSensitivity(Qt::CaseInsensitive);
search_text.replace(" ","|"); // to make it possible to look up all words given for the search
rx.setPattern("\\b("+search_text+")\\b");
QString searchable_string;
bool doesContain = searchable_string.contains(rx); // just to check if it contains, I have other code that does what I need. this is just for clearity what I try to say.
If the search text will be "this that",
the end result QRegExp pattern will be "\\b("this|that")\\b"
"doesContain" will be true if the searchable_string contains either "this" or "that".
My question is how do I write the QRegExp pattern that will return true if searchable text contain all search words in any order not just and return false if contain some of the search words.
for example if search_text is "stop this that"
it needs to return true if searchable_string is "when this car and that bike stop"
and will return false if searchable_string is "when this car stops"
my current code will return true for both.
I am able to run a search that will bring results for any word in the search string. Here is what i have
QString search_text = ui->lineEditSearch->text();
QRegExp rx;
rx.setCaseSensitivity(Qt::CaseInsensitive);
search_text.replace(" ","|"); // to make it possible to look up all words given for the search
rx.setPattern("\\b("+search_text+")\\b");
QString searchable_string;
bool doesContain = searchable_string.contains(rx); // just to check if it contains, I have other code that does what I need. this is just for clearity what I try to say.
If the search text will be "this that",
the end result QRegExp pattern will be "\\b("this|that")\\b"
"doesContain" will be true if the searchable_string contains either "this" or "that".
My question is how do I write the QRegExp pattern that will return true if searchable text contain all search words in any order not just and return false if contain some of the search words.
for example if search_text is "stop this that"
it needs to return true if searchable_string is "when this car and that bike stop"
and will return false if searchable_string is "when this car stops"
my current code will return true for both.