PDA

View Full Version : multiple word QRegExp



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.

amleto
5th December 2012, 20:24
you need to use backreference to do it properly (as far as I can see), and even then I'm not sure if/how it would work. Much simpler to just tokenise the search string and search for the tokens individually.

vladozar
6th December 2012, 00:07
Thanks Amleto


you need to use backreference to do it properly (as far as I can see), and even then I'm not sure if/how it would work.
I have not found much information about backreference so was not able to do it that.


Much simpler to just tokenise the search string and search for the tokens individually.
This is probably the easiest way to make it work. Here is how I made it work. The outcome will be that if searchable_string contains all search words, then hasAll will be true, else if searchable_string will have only some of the words, then hasAll will be false. Basically what it does, if one of the search words are not in searchable_text, then it stop looking for others and brings the result false.

QStringList stl = search_text.split("|");
bool hasAll = false;
for(int j(0);j<stl.count();++j)
{
hasAll = searchable_string.contains(QRegExp("\\b"+stl.at(j)+"\\b"));
if(!hasAll)
break;
}


--------------
I have another question with QRegExp when searching, how do I set the reg expression to ignore non alphanumeric characters like these ,.<>;:'"\|[]{}!_-=+?/ ,
for example, if search text phrase is "this and"
it will return true if searchable_string is "you must get this, and not cry"

I can run

QString search_text = ui->lineEditSearch->text();
QRegExp rx;
rx.setCaseSensitivity(Qt::CaseInsensitive);
rx.setPattern(search_text);

searchable_string.replace(QRegExp("[+:;\\]\\[{}().,?/\\\"!'*-]")," "); // replace non alphanumeric with white space
searchable_string= searchable_string.simplified(); //remove extra white spaces
bool doesContain = searchable_string.contains(rx);
this will return true, but the problem is that when I send rx to highlighter, it will not highlight the result(the results must contain original string, not the modifies that does not contain non alphanumeric charterers.

I need a way to ignore non alphanumeric characters without running replace() function.