cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
  • We’re improving the Learn JMP page, and want your feedback! Take the survey
Choose Language Hide Translation Bar
Marco_
Level III

Regular Expression to find and replace a string in a formula

Hi 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) ) )
		) );
);

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
jthi
Super User

Re: Regular Expression to find and replace a string in a formula

You 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)));
-Jarmo

View solution in original post

2 REPLIES 2
jthi
Super User

Re: Regular Expression to find and replace a string in a formula

You 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)));
-Jarmo
Craige_Hales
Super User

Re: Regular Expression to find and replace a string in a formula

yes, regex needs the pattern's parenthesis escaped, at least.

Craige

Recommended Articles