Results 1 to 3 of 3

Thread: multiple word QRegExp

  1. #1
    Join Date
    Apr 2009
    Posts
    17
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question multiple word QRegExp

    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

    Qt Code:
    1. QString search_text = ui->lineEditSearch->text();
    2. rx.setCaseSensitivity(Qt::CaseInsensitive);
    3. search_text.replace(" ","|"); // to make it possible to look up all words given for the search
    4. rx.setPattern("\\b("+search_text+")\\b");
    5.  
    6. QString searchable_string;
    7. 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.
    To copy to clipboard, switch view to plain text mode 

    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.

  2. #2
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: multiple word QRegExp

    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.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  3. The following user says thank you to amleto for this useful post:

    vladozar (5th December 2012)

  4. #3
    Join Date
    Apr 2009
    Posts
    17
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: multiple word QRegExp

    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.
    Qt Code:
    1. QStringList stl = search_text.split("|");
    2. bool hasAll = false;
    3. for(int j(0);j<stl.count();++j)
    4. {
    5. hasAll = searchable_string.contains(QRegExp("\\b"+stl.at(j)+"\\b"));
    6. if(!hasAll)
    7. break;
    8. }
    To copy to clipboard, switch view to plain text mode 

    --------------
    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
    Qt Code:
    1. QString search_text = ui->lineEditSearch->text();
    2. rx.setCaseSensitivity(Qt::CaseInsensitive);
    3. rx.setPattern(search_text);
    4.  
    5. searchable_string.replace(QRegExp("[+:;\\]\\[{}().,?/\\\"!'*-]")," "); // replace non alphanumeric with white space
    6. searchable_string= searchable_string.simplified(); //remove extra white spaces
    7. bool doesContain = searchable_string.contains(rx);
    To copy to clipboard, switch view to plain text mode 
    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.

Similar Threads

  1. Help needed to find a word in multiple file
    By aurora in forum Qt Programming
    Replies: 7
    Last Post: 10th October 2011, 08:47
  2. how to get the word when i highlight a word with mouse?
    By saleh.hi.62 in forum Qt Programming
    Replies: 0
    Last Post: 4th July 2010, 08:24
  3. MS Word file
    By Arpan in forum Qt Programming
    Replies: 1
    Last Post: 30th July 2008, 20:26
  4. Two Word QRegExp Help
    By mabeeh in forum Qt Programming
    Replies: 3
    Last Post: 13th May 2008, 14:29
  5. Word wrapping
    By bruccutler in forum Qt Programming
    Replies: 4
    Last Post: 26th February 2007, 16:33

Tags for this Thread

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.