Results 1 to 8 of 8

Thread: Is it possible to use QString::contains(QRegExp) in a switch statement?

  1. #1
    Join Date
    Nov 2009
    Posts
    14
    Thanks
    4

    Post Is it possible to use QString::contains(QRegExp) in a switch statement?

    Say I'm reading a file, line by line, and I want to check if the line QString satisfies any QRegExp I have defined.

    I could use a bunch of if statements such as :
    Qt Code:
    1. while(!in.EOF)
    2. {
    3. line = in.readLine();
    4.  
    5. if(line.contains(RegExp1)){...}
    6. else if(line.contains(RegExp2){...}
    7. ...
    8. else if(line.contains(RegExpN){...}
    9. }
    To copy to clipboard, switch view to plain text mode 

    But I thought it would be neater to write it in the format of a switch. Can this be done. I've googled, but it gives no relevant results.

    I thought I could have done something like this:

    Qt Code:
    1. while(!in.EOF)
    2. {
    3. switch(true)
    4. {
    5. case line.contains(RegExp1): {...}
    6. ...
    7. case line.contains(RegExpN): {...}
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 
    Doesn't work though.

  2. #2
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Is it possible to use QString::contains(QRegExp) in a switch statement?

    HI,

    in C++ the case argument MUST be an INTEGER (char, short, int, ...)
    A camel can go 14 days without drink,
    I can't!!!

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Is it possible to use QString::contains(QRegExp) in a switch statement?

    If you are sure each line will satisfy only one regular expression, you can do:
    Qt Code:
    1. int mask = 0;
    2. mask |= line.contains(rx1) << 1;
    3. mask |= line.contains(rx2) << 2;
    4. mask |= line.contains(rx3) << 3;
    5. // etc.
    6. switch(mask) {
    7. case 1: ...
    8. case 2: ...
    9. case 4: ...
    10. };
    To copy to clipboard, switch view to plain text mode 
    Just be aware this code is suboptimal - every regular expression has to be evaluated. With a bunch of if statements only those are evaluated that are before the one that matches.

    If you are worried about your code looking like spaghetti, you can simplify the syntax with macro expansion.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. The following user says thank you to wysota for this useful post:

    positive_4_life (2nd March 2011)

  5. #4
    Join Date
    Feb 2008
    Posts
    10
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Windows

    Default Re: Is it possible to use QString::contains(QRegExp) in a switch statement?

    Qt Code:
    1. mask |= line.contains(rx1) << 1;
    2. mask |= line.contains(rx2) << 2;
    3. mask |= line.contains(rx3) << 3;
    4. // etc.
    5. switch(mask) {
    6. case 1: ...
    7. case 2: ...
    8. case 4: ...
    9. };
    To copy to clipboard, switch view to plain text mode 
    If you use this structure, you should also add a default clause to the switch, with an assertion failure or exception. This will protect you against the inevitable day when something in the code changes and more than one of the regular expressions succeeds.

  6. #5
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    131
    Thanks
    11
    Thanked 16 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Is it possible to use QString::contains(QRegExp) in a switch statement?

    Quote Originally Posted by jdiewald View Post
    If you use this structure, you should also add a default clause to the switch, with an assertion failure or exception. This will protect you against the inevitable day when something in the code changes and more than one of the regular expressions succeeds.
    He's reading the data from a (external) file. Adding assertions/exceptions can be quite dangerous. The programm should not crash every time the file is messed up.

  7. #6
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Is it possible to use QString::contains(QRegExp) in a switch statement?

    Can you be more specific what you are parsing?
    I'm quite sure that this issue can be solved in better way.
    One of possibilities is union of regular expretions:
    Qt Code:
    1. QRegExp regExp("(regExp1)|(regExp2)|(regExp3)|(regExp4)")
    2.  
    3. while(1) // this (!in.EOF) was wrong!
    4. {
    5. line = in.readLine();
    6. if (line.isNull()) {
    7. break;
    8. }
    9. if(line.contains(regExp)){
    10. const QStringList result = regExp.capturedTexts();
    11. int i;
    12. for (i=0; i<result.count(); ++i) {
    13. if (!result[i].isEmpty()) {
    14. break; // or some action to take
    15. }
    16. }
    17. // process i value
    18. }
    19. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by MarekR22; 3rd March 2011 at 10:41.

  8. #7
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    131
    Thanks
    11
    Thanked 16 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Is it possible to use QString::contains(QRegExp) in a switch statement?

    @MarekR22: Could you explain why a while(1) should be better than a check of the end of the buffer/file/whatever?

    But another idea:

    Qt Code:
    1. // base class
    2. struct Matcher {
    3. virtual bool matches(line);
    4. virtual void doStuff();
    5. }
    6.  
    7. // impl
    8. struct Matcher1 : public Matcher {...}
    9. struct Matcher2 : public Matcher {...}
    10.  
    11. // algorithm
    12. // 1.) make a list of the matcher
    13.  
    14. foreach(const QString& line, lines) {
    15.  
    16. foreach(Matcher* matcher, list_of_matchers) {
    17.  
    18. if(matcher->matches(line)) {
    19. matcher->doStuff();
    20. break;
    21. }
    22. }
    23.  
    24. }
    To copy to clipboard, switch view to plain text mode 

  9. #8
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Is it possible to use QString::contains(QRegExp) in a switch statement?

    this is more like STL habit where this kind construction is obligatory since in STL istream::eof() returns true only when attempt of reading bound end of file happened.
    In Qt you there is no EOF method, but method QTextStream::atEnd and this is why I confused things (sorry). This different names indicates that it works in different way.

Similar Threads

  1. Search for QRegExp in a QString
    By Abc in forum Qt Programming
    Replies: 6
    Last Post: 13th August 2008, 09:31
  2. confusion with a STATEMENT used frequently
    By salmanmanekia in forum Newbie
    Replies: 3
    Last Post: 11th June 2008, 20:54
  3. QString manipulation - QRegExp
    By mattia in forum Newbie
    Replies: 1
    Last Post: 18th March 2008, 11:21
  4. QString::replace() with QRegExp capture modification
    By Lykurg in forum Qt Programming
    Replies: 1
    Last Post: 4th March 2008, 09:50
  5. Replies: 4
    Last Post: 31st January 2008, 20:44

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.