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
SpannerHead
Level IV

Count the numeric passages in a string

This is probably a very specific one but I have a string contained in a JMP cell and I need to be able to isolate a specific passage of numeric characters in the string.  The string contains both characters and numbers and the sequence can be variable.  For example if I have the cell entry

 

aaa1b222c3

 

and I want to zero in on the 222 passage, I considered a means to count the number of numeric passages as being 3 and then isolating the second of those.

 

I came up with a way to do this with Regex but it's too convolved to be worth posting.  I'm sure there's a more adaptive way of getting there.  All ideas appreciated.

 

Slán

 

SpannerHead

 


Slán



SpannerHead
2 ACCEPTED SOLUTIONS

Accepted Solutions
jthi
Super User

Re: Count the numeric passages in a string

This might be enough or you might have to add few extra conditions (check that it is either at the start/end or there is letter before/after)

Names Default To Here(1);

str = "Val1b200X300abcc1";
r = Regex Match(str, "(\d+)X(\d+)");
Show(Num(r[2]), Num(r[3]));
-Jarmo

View solution in original post

SpannerHead
Level IV

Re: Count the numeric passages in a string

Jarmo

 

Great stuff!  Here's what I came up with in the end.  Label is the column containing the string I'm trying to perform surgery on.

 

	New Column("Width", Character, "Nominal", Formula(Regex(:Label, "(\d+)X", "\1")), Set Selected);
	New Column("Length", Character, "Nominal", Formula(Regex(:Label, "X(\d+)", "\1")), Set Selected);

 

Seems to work.

 

Slán

 

SpannerHead


Slán



SpannerHead

View solution in original post

6 REPLIES 6
jthi
Super User

Re: Count the numeric passages in a string

Could you provide more examples of the possible strings and the parts you wish to isolate? Also what do you want to do with those parts? Extract them? Remove them? Check if the exist?

-Jarmo
SpannerHead
Level IV

Re: Count the numeric passages in a string

Jarmo

 

My overall goal is to create a new column that contains the numeric value as a cell input.  This derives from the fact that the original entry contains dimensional information and I'd like that to be automatically available in numeric form.  One of the numeric passages is a length, the other is a width.  There are other numbers included and I need a means to bypass those.  The actual string will look closer to

 

Val1b200X300

 

which is a 200 long by 300 wide device and I need new columns containing cells generated reflecting those values.  The additional numeric characters are not consistent in length or placement which adds complexity to the task.

 

Slán

 

SpannerHead

 


Slán



SpannerHead
jthi
Super User

Re: Count the numeric passages in a string

Are those always three characters long? Are they always the last two numbers? Are they always separated by "X"?

-Jarmo
SpannerHead
Level IV

Re: Count the numeric passages in a string

Jarmo

 

Partial violation of Murphy's law.  Not always the same length, not always the last 2 numbers but always separated by an X.

 

Slán

 

SpannerHead


Slán



SpannerHead
jthi
Super User

Re: Count the numeric passages in a string

This might be enough or you might have to add few extra conditions (check that it is either at the start/end or there is letter before/after)

Names Default To Here(1);

str = "Val1b200X300abcc1";
r = Regex Match(str, "(\d+)X(\d+)");
Show(Num(r[2]), Num(r[3]));
-Jarmo
SpannerHead
Level IV

Re: Count the numeric passages in a string

Jarmo

 

Great stuff!  Here's what I came up with in the end.  Label is the column containing the string I'm trying to perform surgery on.

 

	New Column("Width", Character, "Nominal", Formula(Regex(:Label, "(\d+)X", "\1")), Set Selected);
	New Column("Length", Character, "Nominal", Formula(Regex(:Label, "X(\d+)", "\1")), Set Selected);

 

Seems to work.

 

Slán

 

SpannerHead


Slán



SpannerHead