BookmarkSubscribeRSS Feed
Choose Language Hide Translation Bar
Rajat
Contributor

Use regex to find a sub-string from a string

I have a variable that stores following string and I want to find the red coloured text and replace it with <dt, colList[i]> . I want to use regex as the string can vary as per requirement but string pattern would be similar (not completely identical).

 

String: 

Str = "Graph Builder(
	Size( 1887, 882 ),
	Show Control Panel( 0 ),
	Title Text Color( "Red" ),
	Graph Spacing( 2 ),
	Spacing Borders( 1 ),
	Variables(
		X( :START_TIME ),
		Y( :Sen.win.sta ),
		Group X( :NAME ),
		Group X( :GROUP )
	),
	Elements( Points( X, Y, Legend( 3 ) ), Line( X, Y, Legend( 4 ) ) ),
	SendToReport(
		Dispatch(
			{},
			"Graph Builder",
			OutlineBox,
			{Set Title( "TEST" )}
		),
		Dispatch(
			{},
			"START_TIME",
			ScaleBox,
			{Min( 1 ), Max( 2000 ),
			Interval( "Numeric" ), Inc( 20 ), Minor Ticks( 0 ),
			 Label Row( Label Orientation( "Vertical" ) )}
		),
		Dispatch(
			{},
			"400",
			LegendBox,
			{Legend Position( {3, [-4], 4, [-1]} ), Position( {-4, -1} )}
		)
	)
);  ";

Thanks :)

 

0 Kudos
8 REPLIES 8
ian_jmp
Staff

Re: Use regex to find a sub-string from a string

You could use 'RegEx()' but, as an alternative, 'Eval Insert Into()' - Serach for this in 'Help > Scripting Index' to see an example.

FWIW, I prefer to keep expressions as expressions, because then the editor will still provide colour coding of keywords and so on. To see that approach, search for 'Substitute Into()'. 

Rajat
Contributor

Re: Use regex to find a sub-string from a string

Thanks @ian_jmp for help.
I don't know how color coding approach will work. Also, I have to repeat this task many number of times and regex might be the more robust approach. If possible can you please help to resolve it by using regex() otherwise please explain how color coding will work.
Thanks
0 Kudos
ian_jmp
Staff

Re: Use regex to find a sub-string from a string

I 'think' that you want to replace the Y variable you have in your string. But it's a bit hard to say, since if you look above, all of the string is rendered as purple due to the software used to display the web page.

 

So perhaps you could reiterate exactly what you need to do, please?  

Rajat
Contributor

Re: Use regex to find a sub-string from a string

@ian_jmp 

 

I have a variable "str" that contains the following string. I want to replace <:Sen.Win.sta> with <dt, colList[i]>. I want to use regex as string can get changed based on requirement but pattern would would be same.

Graph Builder(
	Size( 1887, 882 ),
	Show Control Panel( 0 ),
	Title Text Color( "Red" ),
	Graph Spacing( 2 ),
	Spacing Borders( 1 ),
	Variables(
		X( :START_TIME ),
		Y( :Sen.win.sta ),
		Group X( :NAME ),
		Group X( :GROUP )
	),
	Elements( Points( X, Y, Legend( 3 ) ), Line( X, Y, Legend( 4 ) ) ),
	SendToReport(
		Dispatch(
			{},
			"Graph Builder",
			OutlineBox,
			{Set Title( "TEST" )}
		),
		Dispatch(
			{},
			"START_TIME",
			ScaleBox,
			{Min( 1 ), Max( 2000 ),
			Interval( "Numeric" ), Inc( 20 ), Minor Ticks( 0 ),
			 Label Row( Label Orientation( "Vertical" ) )}
		),
		Dispatch(
			{},
			"400",
			LegendBox,
			{Legend Position( {3, [-4], 4, [-1]} ), Position( {-4, -1} )}
		)
	)
);
0 Kudos
ian_jmp
Staff

Re: Use regex to find a sub-string from a string

OK. If that's the requirement, you could do something like this:

NamesDefaultToHere(1);

// Get some data
dt = Open("$SAMPLE_DATA/Probe.jmp");

// Get some columns to loop over
yColList = dt << getColumnNames(Continuous);

// Loop over columns
for (y=1, y<=NItems(yColList), y++,
	gb = dt << Graph Builder(
				Show Control Panel( 0 ),
				Show Legend( 0 ),
				Variables( X( :Lot ID ), Y( yColList[y] ) ),
				Elements( Points( X, Y, Legend( 5 ) ) )
				);
	Wait(1);
	Report(gb) << closeWindow;
	);

In this case the Graph Builder command is an expression (not held within a string), and you don't need to bother with 'SubstituteInto()'.

0 Kudos
Rajat
Contributor

Re: Use regex to find a sub-string from a string

My final requirement is same but to get this every time I have to manually change the value in variable Y to (dt, ColList[i]) each time for new table/new type of graph. So, I want to automate this process to change the Y variable value. I can use any method. I think regex is the best method. So, thats the reason I want to use regex.
My main requirement to automatically find and replace the Y variable.

Thanks @ian_jmp
0 Kudos
ian_jmp
Staff

Re: Use regex to find a sub-string from a string

I've already expressed my own preference for working with expressions directly, rather than munging strings and parsing the result into an expression. I'm sure the method in my previous post is easily adapted to do what you want.

 

But, if you do want to use regular expressions and strings then maybe something like:

txt = "The quick brown fox jumped over the lazy dog";
matchTxt = "the";
replaceTxt = "a";
newTxt = Regex(txt, matchTxt, replaceTxt, GLOBALREPLACE, IGNORE CASE );
Print(txt, newTxt);

Use 'Help > Scripting Index' and search for JSl primitives to see options and examples.

 

Rajat
Contributor

Re: Use regex to find a sub-string from a string

Thanks @ian_jmp
Appreciate your help.
0 Kudos