Results 1 to 6 of 6

Thread: QRegExp not reading literal character '-'

  1. #1
    Join Date
    Dec 2010
    Posts
    76
    Thanks
    13
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QRegExp not reading literal character '-'

    I am having problems getting a QRegExp to read the character '-'. I have a program that reads in QString and that string has to be in a specific order for the program to be able to label it correclty. Here is what I have so far:

    QString itemReadIn = "SFPP-----------";

    QRegExp exp("S?F?------?????");
    exp.setPatternSyntax(QRegExp::Wildcard);

    if(exp.exactMatch(itemReadIn))
    {
    // label item correctly
    return;
    }

    So my probelm is that the if statement is never entered into so the labeling never happens correctly. I thought my expression was correct but I haven't been able to figure out why it won't work. I have also trie \\- for - and that doesn't seem to do anything to help . It is my understanding that the ? means any character which is exactly what I want and the '-' should be literally that character nothing else.

    Any help would be greatly appreciated. Thanks!!

  2. #2
    Join Date
    Jan 2012
    Posts
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QRegExp not reading literal character '-'

    Your wildcard match is trying to match the letter S, followed by any character, followed by a letter F. but in your match, the letter F is right after S

    I think you want S?P?

    Sorry for my bad English, hope you understood what I meant.

  3. #3
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QRegExp not reading literal character '-'

    I suppose you meant "SF??------?????" in your regexp. If so my box spits out a warning "warning: trigraph ??- converted to ~ [-Wtrigraphs]"

    I had to google trigraphs.

    Try escaping one of the "?" or the first "-": "SF\??------?????"

  4. #4
    Join Date
    Dec 2010
    Posts
    76
    Thanks
    13
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QRegExp not reading literal character '-'

    I actually have S?P?------????? in my code but wrote it down wrong on here, but to norobro ~ I will try escaping it to see if that helps.

  5. #5
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QRegExp not reading literal character '-'

    Works fine here with "S?P?------?????":
    Qt Code:
    1. #include <QtGui>
    2.  
    3. int main(){
    4. QString itemReadIn = "SFPP-----------";
    5. QRegExp exp("S?P?------?????");
    6. exp.setPatternSyntax(QRegExp::Wildcard);
    7. if(exp.exactMatch(itemReadIn)) qDebug() << "match";
    8. else qDebug() << "no match";
    9. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Dec 2010
    Posts
    76
    Thanks
    13
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QRegExp not reading literal character '-'

    Grrr I must have done something wrong with my application... Can using a QString that is appended by chunks effect the program?

    e.g. QString itemReadIn;
    itemReadIn.append("S?P?);
    itemReadIn.append("------?????");

Similar Threads

  1. Reading all from a file that contains "\0" character
    By ^NyAw^ in forum Qt Programming
    Replies: 7
    Last Post: 28th November 2011, 15:54
  2. QRegExp not matching the { character !!
    By gontham in forum Newbie
    Replies: 3
    Last Post: 4th June 2010, 00:20
  3. QRegExp Unicode Character Classes
    By Delagen in forum Qt Programming
    Replies: 2
    Last Post: 4th March 2010, 12:17
  4. Character by Character (Unicode?) File Reading
    By mclark in forum Qt Programming
    Replies: 4
    Last Post: 22nd April 2009, 15:28
  5. QRegExp: too big for character
    By ucomesdag in forum Qt Programming
    Replies: 2
    Last Post: 15th March 2007, 19:52

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.