cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Check out the JMP® Marketplace featured Capability Explorer add-in
Choose Language Hide Translation Bar
PhamBao
Level II

How to get characters between brackets within a string??

Hi everyone,

 

I have a string like below " [CDO600705010] [S 88ABCV A B IT] [C23204348IT] "

Do you know how to get characters between open and close bracket

For instance:

CDO600705010

S 88ABCV A B IT

C23204348IT

 

Sincerely

1 ACCEPTED SOLUTION

Accepted Solutions
txnelson
Super User

Re: How to get characters between brackets within a string??

s=" [CDO600705010] [S 88ABCV A B IT] [C23204348IT] ";
one = word(2,s,"[]");
show(one);
/*:

one = "CDO600705010";
Jim

View solution in original post

5 REPLIES 5
txnelson
Super User

Re: How to get characters between brackets within a string??

s=" [CDO600705010] [S 88ABCV A B IT] [C23204348IT] ";
one = word(2,s,"[]");
show(one);
/*:

one = "CDO600705010";
Jim
PhamBao
Level II

Re: How to get characters between brackets within a string??

Amazing! Appreciate It works like miracle

Jeff_Perkinson
Community Manager Community Manager

Re: How to get characters between brackets within a string??

Word() is the function every JMP user should know.

 

If you learn only one Formula Editor function, Word() is the one 

-Jeff
jthi
Super User

Re: How to get characters between brackets within a string??

I would definitely go with Word() or Words() with this one, but there is a Character Pattern thingie in JMP. It is fairly difficult to learn and it might have a bit limited use when compared to more simple methods (I haven't bothered to learn it). Script modified from a script found from  Re: Extract text from string (post also has explanation of the matching) 

Names Default To Here(1);

// see https://community.jmp.com/t5/Discussions/Extract-text-from-string/td-p/450420
txt = " [CDO600705010] [S 88ABCV A B IT] [C23204348IT] ";

sp = Pat Span("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 ");

found_list = {};
Pat Match(
	txt,
	"[" + sp >> match + ("]") + Pat Test(
		Insert Into(found_list, match);
		0;
	)
);

show(found_list); 
// found_list = {"CDO600705010", "S 88ABCV A B IT", "C23204348IT"};

 

But matching between multiple brackets could be one place where I could see myself using Character Patterns.

 

See also Scripting Guide > Types of Data > Pattern Matching 

 

-Jarmo

Re: How to get characters between brackets within a string??

I like @txnelson's approach. I often use it. The character functions are handy!

 

Here is another approach using Regex() instead. It is not a solution. It is an illustration of how you might parse each string. I show two search patterns, but other patterns are possible. I wonder which way is faster, as it might be a consideration with many strings.

 

Names Default to Here( 1 );

string = {  "[CDO600705010]", "[S 88ABCV A B IT]", "[C23204348IT]" };
n = N Items( string );

For( s = 1, s <= n, s++,
	Show(
		Regex( string[s], "([\w\d\s]+)", "\1" );
	);
);

For( s = 1, s <= n, s++,
	Show(
		Regex( string[s], "[^\-[\]]+" );
	);
);