Results 1 to 4 of 4

Thread: Two Word QRegExp Help

  1. #1
    Join Date
    Apr 2008
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Two Word QRegExp Help

    I'm having a problem setting up a two word QRegExp. In Example #1 I'm able to pick out USER from str1. However, in Example #2 I'm not able to pick out USER ACTION:
    What am I doing wrong? Thanks!


    EXAMPLE #1:
    Qt Code:
    1. QString str = "User Action:";
    2. QString str1 = "USER ACTION:";
    3.  
    4. QRegExp reg ("^\\bUSER\\b");
    5.  
    6. int ireturn = reg.indexIn(str, 0);
    7. std::cout << ireturn << std::endl;
    8. ireturn = reg.indexIn(str1, 0);
    9. std::cout << ireturn << std::endl;
    10. QString captured = reg.cap( 0 ); // captured == "letter"
    11. std::cout << captured.toStdString() << std::endl;
    To copy to clipboard, switch view to plain text mode 

    EXAMPLE #2:
    Qt Code:
    1. QString str = "User Action:";
    2. QString str1 = "USER ACTION:";
    3.  
    4. QRegExp reg ("^\\bUSER\\b\\s\\bACTION:\\b");
    5.  
    6. int ireturn = reg.indexIn(str, 0);
    7. std::cout << ireturn << std::endl;
    8. ireturn = reg.indexIn(str1, 0);
    9. std::cout << ireturn << std::endl;
    10. QString captured = reg.cap( 0 ); // captured == "letter"
    11. std::cout << captured.toStdString() << std::endl;
    To copy to clipboard, switch view to plain text mode 
    Last edited by jpn; 13th May 2008 at 13:48. Reason: changed [indent] to [code] tags

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Two Word QRegExp Help

    Did you read QRegExp::cap() docs?
    J-P Nurmi

  3. #3
    Join Date
    Apr 2008
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Two Word QRegExp Help

    Thanks for the help! But I have one more question...How do I capture the entire string? I need to be able to capture the entire line. So far I've got it to match the key words I'm looking for. Thanks!

    Qt Code:
    1. QString str = "User Action:";
    2. QString str1 = "USER ACTION: do this";
    3. QString str2 = "USER VERIFY: DO SOMETHING ELSE";
    4. QString str3 = "STEP NUMBER: 1";
    5.  
    6. QRegExp reg ("^((\\bUSER\\b)(\\s)(\\bACTION:\\b?))|((\\bUSER\\b)(\\s)(\\bVERIFY:\\b?))|((\\bSTEP\\b)(\\s)(\\bNUMBER:\\b?))");
    7.  
    8. int ireturn = reg.indexIn(str, 0);
    9. std::cout << ireturn << std::endl;
    10. ireturn = reg.indexIn(str1, 0);
    11. std::cout << ireturn << std::endl;
    12. QString captured = reg.cap( 0 ); // captured == "letter"
    13. std::cout << captured.toStdString() << std::endl;
    14.  
    15. ireturn = reg.indexIn(str2, 0);
    16. std::cout << ireturn << std::endl;
    17. captured = reg.cap( 0 ); // captured == "letter"
    18. std::cout << captured.toStdString() << std::endl;
    19.  
    20. ireturn = reg.indexIn(str3, 0);
    21. std::cout << ireturn << std::endl;
    22. captured = reg.cap( 0 ); // captured == "letter"
    23. std::cout << captured.toStdString() << std::endl;
    To copy to clipboard, switch view to plain text mode 

    Output produced:
    -1
    0
    USER ACTION:
    0
    USER VERIFY:
    0
    STEP NUMBER:
    Last edited by jpn; 13th May 2008 at 19:04. Reason: fixed tags

  4. #4
    Join Date
    Apr 2008
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Two Word QRegExp Help

    I got it...and it captures the entire string. Here's the code and the output.

    Qt Code:
    1. QString str = "User Action:";
    2. QString str1 = "USER ACTION: do this";
    3. QString str2 = "USER VERIFY: DO SOMETHING ELSE";
    4. QString str3 = "STEP NUMBER: 1";
    5.  
    6. QRegExp reg ("^((\\bUSER\\b)(\\s)(\\bACTION:\\b?))(.*$)|((\\bUSER\\b)(\\s)(\\bVERIFY:\\b?))(.*$)|((\\bSTEP\\b)(\\s)(\\bNUMBER:\\b?))(.*$)");
    7.  
    8. int ireturn = reg.indexIn(str, 0);
    9. std::cout << ireturn << std::endl;
    10. ireturn = reg.indexIn(str1, 0);
    11. std::cout << ireturn << std::endl;
    12. QString captured = reg.cap( 0 ); // captured == "letter"
    13. std::cout << captured.toStdString() << std::endl;
    14.  
    15. ireturn = reg.indexIn(str2, 0);
    16. std::cout << ireturn << std::endl;
    17. captured = reg.cap( 0 ); // captured == "letter"
    18. std::cout << captured.toStdString() << std::endl;
    19.  
    20. ireturn = reg.indexIn(str3, 0);
    21. std::cout << ireturn << std::endl;
    22. captured = reg.cap( 0 ); // captured == "letter"
    23. std::cout << captured.toStdString() << std::endl;
    To copy to clipboard, switch view to plain text mode 

    Output Produced:
    -1
    0
    USER ACTION: do this
    0
    USER VERIFY: DO SOMETHING ELSE
    0
    STEP NUMBER: 1
    Last edited by jpn; 13th May 2008 at 19:05. Reason: fixed tags

Similar Threads

  1. invoking microsoft word from qt
    By omprakash in forum Qt Programming
    Replies: 2
    Last Post: 31st October 2007, 20:43
  2. QRegExp Help
    By Ahmad in forum Qt Programming
    Replies: 2
    Last Post: 28th May 2007, 00:13
  3. Word wrapping in a QTableWidget cell
    By jcooperddtd in forum Qt Programming
    Replies: 3
    Last Post: 1st May 2007, 03:57
  4. QRegExp progblem
    By high_flyer in forum Qt Programming
    Replies: 1
    Last Post: 6th September 2006, 12:12
  5. need help for my QRegExp
    By patcito in forum Qt Programming
    Replies: 1
    Last Post: 27th May 2006, 16:29

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.