Results 1 to 5 of 5

Thread: Calling QRegExp pattern from a variable gets -1

  1. #1
    Join Date
    Sep 2011
    Posts
    19
    Qt products
    Qt4
    Platforms
    Windows

    Default Calling QRegExp pattern from a variable gets -1

    Here's the code from a music program I'm writing. The regexp works. If I copy and paste it in (removing only the two extra \\ slashed for \\\\d*) it works great. But no matter what I do, and I've tried every combination I can think of or look up, pos returns -1. I'm stumped. Why won't this work when called from a variable? It is just text, right? I've tried various quoting methods, various escape sequences, etc. but no luck. Here's the code. Once again, it works when copied and pasted but not when sent as an escaped OR unescaped variable. Thank you for your help.

    Qt Code:
    1. /// excerpt declaring the variable
    2. else if ( key == "Ab Major") {
    3. k->keyRegExp = (QString("^([b|e|a|d])(\\\\d*)"));
    4. k->keyAccidental = "b";
    5. k->kNum = 4;
    6. k->kType = "Flats";
    7.  
    8. /// routine calling the variable and the regexp
    9. KeySignature* ks = new KeySignature;
    10. ks = ks->getCurrKeySigData();
    11. QString key = ks->key;
    12. QString keyRegExp = ks->keyRegExp;
    13. QString acc = ks->keyAccidental;
    14. QString nn = iNote->noteName;
    15. QList<QString>resultList;
    16. QRegExp kNotes(keyRegExp);
    17. bool ok = kNotes.isValid();
    18. int pos = kNotes.indexIn(nn);
    19. QString pat = kNotes.pattern();
    20. if (pos != -1) {
    21. resultList = kNotes.capturedTexts();
    22. nn = resultList.value(1) + acc + resultList.value(2);
    23. stop++;
    24.  
    25. }
    26.  
    27. iNote->noteName = nn;
    28. return iNote;
    29. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: Calling QRegExp pattern from a variable gets -1

    how do you know it works if yo havent tested it with code/variables? What do you mean by copy/paste - copy what to where.

    Why dont you just set up a small unit test for regex?
    Last edited by amleto; 22nd October 2011 at 16:57.

  3. #3
    Join Date
    Sep 2011
    Posts
    19
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Calling QRegExp pattern from a variable gets -1

    This code is from the program. I've run it successfully using the regexp from the excerpt, copying and pasting the excerpt into the space where the keyRegExp variable is. But when I use the variable instead of the copy/pasted literal value the QString pat = kNotes.pattern(); shows that the regexp pattern is correct, the bool ok = kNotes.isValid(); shows that the pattern is valid, but int pos = kNotes.indexIn(nn); ALWAYS returns -1.

    That's the problem.

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

    Default Re: Calling QRegExp pattern from a variable gets -1

    From the docs:
    To include a \ in a regexp, enter it twice, i.e. \\. To match the backslash character itself, enter it four times, i.e. \\\\
    So wouldn't your regexp be looking for b,e,a or d, followed by a "\", followed by zero or more occurances of "d"?

    Does it not work with two backslashes?
    Qt Code:
    1. QString("^([b|e|a|d])(\\d*)");
    To copy to clipboard, switch view to plain text mode 
    Last edited by norobro; 23rd October 2011 at 00:32. Reason: brain fart - changed forwardslash to backslash

  5. #5
    Join Date
    Sep 2011
    Posts
    19
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Calling QRegExp pattern from a variable gets -1

    Sorry, should have included that at first. It is looking for a note name followed by a number, such as e2. It will then take it apart and reassemble it as eb2. When I use a single \d in qt I get an "unknown token" warning. I have to use two \\d to make it work. I'm used to one \d in perl.


    Added after 4 minutes:


    If I use two slashes (unescaped) it gets translated to one and that won't work. I need to escape the slashed for it to end up in one piece at the other end. This is why this is so frustrating. The patten is ending up at the other end in one piece and is declared valid by the routine but doesn't ever match the simple target. But it does work if it doesn't come from the variable. Is there some trick to executing it from the variable?


    Added after 20 minutes:


    OK -- I have played with the slashed and got it to work. I had too many in operation at once and once I reduced their number everything worked ok. It only takes one slash just like in perl. I wonder why I though differently? I don't know. Thank you for your very useful help.
    Last edited by devdon; 22nd October 2011 at 20:46.

Similar Threads

  1. QRegeXp - searching a pattern given in QLineEdit
    By mattis16 in forum Qt Programming
    Replies: 2
    Last Post: 10th June 2011, 16:06
  2. Replies: 2
    Last Post: 9th December 2010, 16:49
  3. Is there a web search pattern?
    By thewooferbbk in forum Qt Programming
    Replies: 1
    Last Post: 18th October 2009, 16:02
  4. GIS design pattern
    By olosie in forum Qt Programming
    Replies: 2
    Last Post: 19th May 2009, 16:19
  5. pattern
    By mickey in forum General Discussion
    Replies: 3
    Last Post: 14th February 2009, 17:12

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
  •  
Qt is a trademark of The Qt Company.