正規表示式用於尋找和替換公式中的字串大家好,
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)));
回覆:使用正規表示式尋找並取代公式中的字串是的,正規表示式至少需要轉義模式的括號。