正则表达式用于查找和替换公式中的字符串大家好,
I try to replace one specific part As Constant( t Quantile( 1 - 0.1 / 2, in every selected column formula by Random t((.
但是我在正则表达式部分失败了。我认为这可能与括号的转义有关。我尝试过 \!\,但在这里也失败了……
有人可以调试下面的我的代码吗?
// 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) ) )
) );
);
自动化和脚本视窗回复:使用正则表达式查找并替换公式中的字符串您很可能可以使用 Substitute 或 Substitute Into 代替 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)));
回复:使用正则表达式查找并替换公式中的字符串是的,正则表达式至少需要转义模式的括号。