Results 1 to 10 of 10

Thread: How do I use QRegExp to split an expression

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Dec 2006
    Posts
    426
    Thanks
    8
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default How do I use QRegExp to split an expression

    Hi,

    I need to split string such as "Stage1 <= 4.4e-05 || Stage == 1.2 && Comp >= 1.4e+03 || A+e-C > D" to get all variables in the expression.

    In the example string, the result should be "Stage1", "4.4e-05", "Stage", "1.2", "Comp", "1.4e+03", "A", "e", "C", "D"

    I am using QRegExp rx( "[+\\-*/(),<>&=| ]" ) to split it.

    However, it also split "4.4e-05" and "1.4e+03". How can I write the QRegExp to split it without breaking scientific notation.

    Thanks!


    Here is sample code

    Qt Code:
    1. #include <QStringList>
    2. #include <QRegExp>
    3. #include <QDebug>
    4.  
    5. int main()
    6. {
    7. QString str( "Stage1 <= 4.4e-05 || Stage == 1.2 && Comp >= 1.4e+03 || A+e-C > D" );
    8. qDebug() << "str =" << str;
    9.  
    10. QRegExp rx( "[+\\-*/(),<>&=| ]" );
    11. QStringList strList = str.split( rx, QString::SkipEmptyParts );
    12.  
    13. qDebug() << "strList =" << strList;
    14. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How do I use QRegExp to split an expression

    You could try making two passes. In the first pass, do not use "+" or "-" in your reg exp. Take the string list result from pass 1 and examine each entry to see if it matches a reg exp for a number (you can search online for suitable regular expressions). If it matches, keep it. If not, then submit the substring to a second pass that splits on your original reg exp in Line 10.

    It is hard to write a single regular expression that will match arbitrary string expressions in a single pass. This is why the lex / yacc and flex / bison tools exist.

  3. #3
    Join Date
    Dec 2006
    Posts
    426
    Thanks
    8
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How do I use QRegExp to split an expression

    Quote Originally Posted by d_stranz View Post
    You could try making two passes. In the first pass, do not use "+" or "-" in your reg exp. Take the string list result from pass 1 and examine each entry to see if it matches a reg exp for a number (you can search online for suitable regular expressions). If it matches, keep it. If not, then submit the substring to a second pass that splits on your original reg exp in Line 10.

    It is hard to write a single regular expression that will match arbitrary string expressions in a single pass. This is why the lex / yacc and flex / bison tools exist.
    I did use two passes, but still fail. I use the following string for test
    "a-aa+bb+4.4e-05-1.2e+2"

    It should split into "a", "aa", "bb", "4.4e-05", "1.2e+2", but it doesn't. Please help.

    Here is my code
    Qt Code:
    1. #include <QStringList>
    2. #include <QRegExp>
    3. #include <QDebug>
    4.  
    5. static QStringList getFormulaVarList( const QString& formula )
    6. {
    7. QString::SplitBehavior behavior = QString::SkipEmptyParts;
    8.  
    9. QRegExp opRx( "[*/()<>&=| ]" );
    10. QRegExp plusMinus( "[+\\-]" );
    11.  
    12. QStringList strList;
    13. foreach( const QString& str, formula.split( opRx, behavior ) ) {
    14. bool ok;
    15. str.toDouble( &ok );
    16. if ( !ok ) {
    17. strList << str.split( plusMinus, behavior );
    18. }
    19. }
    20. strList.removeDuplicates();
    21.  
    22. QStringList result;
    23. foreach( const QString& str, strList ) {
    24. bool ok;
    25. str.toDouble( &ok );
    26. if ( !ok && !str.startsWith( "math.", Qt::CaseInsensitive ) ) {
    27. result << str;
    28. }
    29. }
    30. result.removeDuplicates();
    31.  
    32. return result;
    33.  
    34. } // getFormulaVarList
    35.  
    36. int main( int argc, char** argv )
    37. {
    38. //QString formula( "Stage1 <= 4.4e-05 || Stage == 1.2 && Comp >= 1.4e+03 || A+e-C > D" );
    39. //QString formula( "a * aa+4.4e-05 + math.log( b )" );
    40. //QString formula( "a * aa + 4.4e-05 + math.log( b )" );
    41. QString formula( argv[ 1 ] );
    42.  
    43. qDebug() << "formula =" << formula;
    44.  
    45. qDebug() << "items =" << getFormulaVarList( formula );
    46. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How do I use QRegExp to split an expression

    I think you should read the documentation on QRegularExpression and especially read the Perl tutorial linked to in that doc. Your regular expressions are much too simple to match the kind of strings you have as input, and I do not think you can do it with a single regular expression.

    "a-aa+bb+4.4e-05-1.2e+2"
    You might also read this.
    You can see that even parsing simple expressions like this one takes a lot of code to recognize the symbols, operators, and constants and convert that into tokens.

Similar Threads

  1. Replies: 7
    Last Post: 6th February 2017, 19:10
  2. Split layout
    By akiross in forum Qt Programming
    Replies: 0
    Last Post: 19th August 2011, 20:26
  3. Replies: 1
    Last Post: 8th June 2011, 06:44
  4. split QByteArray
    By xproch13 in forum Newbie
    Replies: 2
    Last Post: 29th October 2010, 21:50
  5. Expression in QRegExp
    By lyucs in forum Qt Programming
    Replies: 4
    Last Post: 28th May 2009, 13:53

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.