Results 1 to 3 of 3

Thread: Having a brain cramp on a regex

  1. #1
    Join Date
    Apr 2006
    Posts
    40
    Thanks
    4
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Having a brain cramp on a regex

    I should probably just go home and get some sleep, but for the life of me I can't figure out why this regular expression isn't working.

    Basically, what I want to do is take a list of strings and search through it looking for any string where any word in the string starts with the filter text. I thought I was pretty sure I have the regex correct, but no matter what I do I can't get it to work properly.


    Here's my little filter function:

    Qt Code:
    1. void MyDialog::filter(QString filterText)
    2. {
    3. // clear out the gui list
    4. ui.stringList->clear(); // a combo box on my form holding a list of strings
    5. // set up the regular expression
    6. QRegExp regexp(QString("^.*\\b%1.*$").arg(filterText),Qt::CaseInsensitive,QRegExp::Wildcard);
    7. // strings is just a QStringList defined elsewhere
    8. ui.stringList->addItems(strings.filter(regexp));
    9. }
    To copy to clipboard, switch view to plain text mode 


    Now, if I make the QRegExp look like:
    Qt Code:
    1. QRegExp regexp(QString("%1").arg(filterText),Qt::CaseInsensitive,QRegExp::Wildcard);
    To copy to clipboard, switch view to plain text mode 
    It works just like I expect, filling the gui box with a list with lines from the full list where the filter text appears anywhere in any word from that string.

    Admittedly, my knowledge on regular expressions is a bit rusty, but and maybe I'm just not forming the regular expression correctly, but I've been searching google for a while trying to remember this stuff and if I have something wrong I just can't see it.

    I mean, even doing something simple like QString("^%1").arg(filterText) doesn't work. So I'm at a bit of a loss.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Having a brain cramp on a regex

    Quote Originally Posted by Spockmeat View Post
    QRegExp regexp(QString("%1").arg(filterText),Qt::CaseInsen sitive,QRegExp::Wildcard);
    The last parameter tells Qt to interpret your expression as a wildcard, not a regular expression. Either change it to QRegExp::RegExp or drop it.

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

    Spockmeat (16th July 2007)

  4. #3
    Join Date
    Apr 2006
    Posts
    40
    Thanks
    4
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Having a brain cramp on a regex

    Wow, I feel dumb now

    Thanks for that tip, works fine now.

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.