Results 1 to 7 of 7

Thread: Please help on QRegExp

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

    Default Please help on QRegExp

    Hi,

    I need to create a Qt script dynamically from user input formular using QRegExp. I got completely lose. Please help.

    I have a list of QVector<float> a, aa, b, etc... which I can transfer to QScriptEngine.

    I have a QLineEdit to allow user to enter their own formular to compute a new one, for instance, use can enter the following string (case insensitive):

    math.max(a)/math.min(aa)*b+aa/b-Math.pow(aa)/a/MATH.pow(a,2.14)

    I need to convert this string into QtScript dynamically, so it should become:

    Qt Code:
    1. var max_a = max( a ); // max is a global function somewher in my script library
    2. var min_aa = min( aa ); // min is a global function somewher in my script library
    3.  
    4. var result = new Array;
    5. for ( var idx = 0; idx < a.length; idx++ ) {
    6. result[ idx ] = max_a / min_aa * b[ idx ] + aa[ idx ] / b[ idx ] - Math.pow( aa[ idx ] ) / a[ idx ] / Math.pow( a[ idx ], 2.14 );
    7. }
    To copy to clipboard, switch view to plain text mode 

    I believe I need to use QRegExp, but am unable to get it done.

    Many thanks!!

  2. #2
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 27 Times in 24 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: Please help on QRegExp

    I think you should implement you own algorithm for this task. You can find many samples on the web.

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

    Default Re: Please help on QRegExp

    not really sure why you want to use regex there?

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Please help on QRegExp

    So, you want to parse this:
    Qt Code:
    1. math.max(a)/math.min(aa)*b+aa/b-Math.pow(aa)/a/MATH.pow(a,2.14)
    To copy to clipboard, switch view to plain text mode 
    looking for instances of some arbitrary and ambiguous variable names and substitute something in place of each. At the very least you need some rules to decide the boundary of a variable name (which may be the same as the word boundary (\b) test in regular expressions). Then you need to test for each possible variable name in the string, with its boundary conditions, and replace it when found. Given that this is user input your really should verify that the syntax is good.

    When you have tried to write a simple, standalone program that attempts this come back here with the program and the actual thing the gets you completely lost.

  5. #5
    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: Please help on QRegExp

    To be honest I don't understand what you want to convert here. The script you mention looks like a perfectly valid JavaScript. Why not simply execute it (after assigning proper values to "a", "aa", etc.)?
    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.


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

    Default Re: Please help on QRegExp

    Quote Originally Posted by wysota View Post
    To be honest I don't understand what you want to convert here. The script you mention looks like a perfectly valid JavaScript. Why not simply execute it (after assigning proper values to "a", "aa", etc.)?
    Could you try this code and switch between "if 0" and "if 1"? Thanks!
    Qt Code:
    1. #include <QScriptEngine>
    2. #include <QCoreApplication>
    3. #include <QDebug>
    4.  
    5. static void addToEngine( QScriptEngine& engine, const QString& varName, const QVector<float>& val )
    6. {
    7. QScriptValue result = engine.newArray( val.size() );
    8. for ( int idx = 0; idx < val.size(); idx++ ) {
    9. result.setProperty( idx, val[ idx ] );
    10. }
    11. engine.globalObject().setProperty( varName, result );
    12. }
    13.  
    14. int main( int argc, char** argv )
    15. {
    16. QCoreApplication app( argc, argv );
    17.  
    18. QVector<float> a( 5 ), aa( 5 );
    19. for ( int idx = 0; idx < 5; idx++ ) {
    20. a[ idx ] = idx;
    21. aa[ idx ] = idx + 10;
    22. }
    23.  
    24. QScriptEngine engine;
    25.  
    26. addToEngine( engine, "a", a );
    27. addToEngine( engine, "aa", aa );
    28.  
    29. #if 1
    30. QScriptValue result = engine.evaluate( "d = a * aa;" );
    31. #else
    32. QScriptValue result = engine.evaluate( "\n\
    33. var d = new Array; \n\
    34. for ( var idx=0; idx < a.length; idx++ ) {\n\
    35. d[idx] = a[idx] * aa[idx]; \n\
    36. }" );
    37. #endif
    38.  
    39. qDebug() << result.toString();
    40.  
    41. QScriptValue dVal = engine.globalObject().property( "d" );
    42. qDebug() << dVal.toString();
    43.  
    44. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    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: Please help on QRegExp

    What's your point? JavaScript doesn't define vector operations so obviously trying to do vector multiplication fails. How is that related to your problem? "a" and "aa" in your script should point to values, not vectors.
    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.


Similar Threads

  1. Help on QRegExp please
    By lni in forum Qt Programming
    Replies: 3
    Last Post: 11th August 2010, 06:57
  2. QRegExp help
    By Lele in forum Qt Programming
    Replies: 2
    Last Post: 8th February 2008, 10:07
  3. QRegExp
    By szczav in forum Qt Programming
    Replies: 4
    Last Post: 19th June 2007, 20:07
  4. QRegExp Help
    By Ahmad in forum Qt Programming
    Replies: 2
    Last Post: 28th May 2007, 00:13
  5. QRegExp?
    By Marco812 in forum Qt Programming
    Replies: 3
    Last Post: 4th August 2006, 08:31

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.