Regular Expression to find and replace a string in a formulaHi everyone,
I try to replace one specific part As Constant( t Quantile( 1 - 0.1 / 2, in every selected column formula by Random t((.
However, I fail in the regex part. I think it could have to do with escaping the brackets. I tried with \!\ but also failed here...
Could someone debug my code below?
// Select some columns
dt = currentdatatable();
dt << Get Selected Columns;
//'fix' each selected column formula by manipulating the formula as a string
for each( {col, c}, dt << Get Column References( dt << Get Selected Columns ),
newf = regex(
char(col << Get Formula),
"As Constant( t Quantile( 1 - 0.1 / 2, ",
"Random t((",
GLOBALREPLACE
);
//show(Parse(newf));
Eval( Eval Expr(
col << Set Formula( Expr( Parse(newf) ) )
) );
);
Automation and ScriptingWindowsRe: Regular Expression to find and replace a string in a formulaYou can most likely use Substitute or Substitute Into instead of Regex
Names Default To Here(1);
dt = open("$SAMPLE_DATA/Big Class.jmp");
new_col = dt << new column("F", Numeric, Continuous, Formula(
As Constant(t Quantile(1 - 0.1 / 2, 1, 0))
));
f_expr = new_col << Get Formula;
f_str = Char(Name Expr(f_expr));
Substitute Into(f_str, "As Constant(t Quantile(1 - 0.1 / 2, ", "Random t((",);
Substitute Into(f_str, "((", "(", "))", ")");
new_col << Set Formula(Eval(Parse(f_str)));
Re: Regular Expression to find and replace a string in a formulayes, regex needs the pattern's parenthesis escaped, at least.