Re: Please help on QRegExp
I think you should implement you own algorithm for this task. You can find many samples on the web.
Re: Please help on QRegExp
not really sure why you want to use regex there?
Re: Please help on QRegExp
So, you want to parse this:
Code:
math.max(a)/math.min(aa)*b+aa/b-Math.pow(aa)/a/MATH.pow(a,2.14)
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.
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.)?
Re: Please help on QRegExp
Quote:
Originally Posted by
wysota
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!
Code:
#include <QScriptEngine>
#include <QCoreApplication>
#include <QDebug>
static void addToEngine( QScriptEngine& engine, const QString& varName, const QVector<float>& val )
{
QScriptValue result = engine.newArray( val.size() );
for ( int idx = 0; idx < val.size(); idx++ ) {
result.setProperty( idx, val[ idx ] );
}
engine.globalObject().setProperty( varName, result );
}
int main( int argc, char** argv )
{
QVector<float> a( 5 ), aa( 5 );
for ( int idx = 0; idx < 5; idx++ ) {
a[ idx ] = idx;
aa[ idx ] = idx + 10;
}
QScriptEngine engine;
addToEngine( engine, "a", a );
addToEngine( engine, "aa", aa );
#if 1
QScriptValue result = engine.evaluate( "d = a * aa;" );
#else
QScriptValue result = engine.evaluate( "\n\
var d = new Array; \n\
for ( var idx=0; idx < a.length; idx++ ) {\n\
d[idx] = a[idx] * aa[idx]; \n\
}" );
#endif
qDebug() << result.toString();
QScriptValue dVal = engine.globalObject().property( "d" );
qDebug() << dVal.toString();
}
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.