cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
New to using JMP? Hit the ground running with the Early User Edition of Discovery Summit. Register now, free of charge.
Register for our Discovery Summit 2024 conference, Oct. 21-24, where you’ll learn, connect, and be inspired.
Choose Language Hide Translation Bar
ReliabilityWolf
Level III

search substring from vareity of string

I have a column with name of failure, which contains all variety of strings. I like to search error code with start of "ex". the error code is 10-character string with start of "ex". and put the 1st error code into column of "1st code", all error codes into column of "all codes. what formula is set to both columns? sorry to bother again. I need to quickly make my work in hands closed firstly, then take time to learn JMP help document. 

ReliabilityWolf_0-1724685518572.png

 

1 ACCEPTED SOLUTION

Accepted Solutions
txnelson
Super User

Re: search substring from vareity of string

My error,

The length of the Substr element should be Start, not Start+10.

First Code

start = Contains( :Failure, "0x" );
If( start > 0,
	Substr( :Failure, start, 10 ),
	""
);

All Codes

theCodes = {};
theString = :Failure;
While( Contains( theString, "0x" ) > 0,
	start = Contains( theString, "0x" );
	Insert Into( theCodes, Substr( theString, start, 10 ) );
	theString = Substr( theString, start + 10 );
);
Concat Items( theCodes, "," );
Jim

View solution in original post

12 REPLIES 12
jthi
Super User

Re: search substring from vareity of string

txnelson
Super User

Re: search substring from vareity of string

Here is one way to create these 2 columns

Formula for :fst code

start = contains(:Failure,"0x");
if(start>0,substr(:Failure,start,start+10),"");

Formula for :All Codes

theCodes = {};;
theString = :Failure;
While( Contains( theString, "0x" ) > 0,
	start = Contains( theString, "0x" );
	Insert Into( theCodes, Substr( theString, start, start + 10 ) );
	theString = Substr( theString, start + 10 );
);
concat items(theCodes, ",");
Jim
ReliabilityWolf
Level III

Re: search substring from vareity of string

Really appreciate your timely assistance. with the formula, error codes are picked up but some other characters show up as well. I solved it using left(). but I like to know why the other characters show up over there? 

ReliabilityWolf_0-1724724126047.png

 

txnelson
Super User

Re: search substring from vareity of string

Without having your data table to use to debug the issue, I am not sure what the issue is.  Try this modification and see if it works better.

start = contains(:Failure,"0x");
x="";
if(start>0,x=substr(:Failure,start,start+10),"");
x;
Jim
ReliabilityWolf
Level III

Re: search substring from vareity of string

@txnelson thanks for your time on the topic. I tried it again, it still fails. here is the data table for your debug. 

txnelson
Super User

Re: search substring from vareity of string

My error,

The length of the Substr element should be Start, not Start+10.

First Code

start = Contains( :Failure, "0x" );
If( start > 0,
	Substr( :Failure, start, 10 ),
	""
);

All Codes

theCodes = {};
theString = :Failure;
While( Contains( theString, "0x" ) > 0,
	start = Contains( theString, "0x" );
	Insert Into( theCodes, Substr( theString, start, 10 ) );
	theString = Substr( theString, start + 10 );
);
Concat Items( theCodes, "," );
Jim
ReliabilityWolf
Level III

Re: search substring from vareity of string

thanks a million!

jthi
Super User

Re: search substring from vareity of string

If it is possible that you have messages mixed in which start with 0x but aren't error codes simple Contains might work, but based on the examples you have provided us with, this doesn't seem to be the case (or if you have for example 0x123456 which is shorter than what you defined as errorcode).

If this ends up being the case, I would look at the Regex (you can use similar idea with While loop as Jim did show with contains). But you have to have good knowledge of the allowed characters to build robust pattern for proper matching.

-Jarmo
ReliabilityWolf
Level III

Re: search substring from vareity of string

@jthi thanks for your comments. I am trying to use Regex to search the substring starting with 0x. based on your recommended post, it's a good idea for "New Column by Text Matching". I am thinking how to build the pattern for the error code ( or the error code is not limited to 10 characters). the instruction of Backreferences and Capturing Groups for Regex can help me to understand the related JSL, but it's a little hard for me to write JSL. i think more JSL examples I read, sooner I learn to write JSL. back to the subject,  could you do me a favor to show the Regex pattern of the error code?  for example in your post, it is

failure_pattern = "[A-Z]+\d+[A-Z]?"