cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Try the Materials Informatics Toolkit, which is designed to easily handle SMILES data. This and other helpful add-ins are available in the JMP® Marketplace
Choose Language Hide Translation Bar

Extract numbers from string in JSL

Hi all,

I have string containing numbers, letters and symbols. I would like to extract only numbers.

For example "48mg/l" becomes 48, "24" remains 24, etc.

It has to be done in JSL.

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
Craige_Hales
Super User

Re: Extract numbers from string in JSL

This could be simpler if you know the numbers are unsigned integers and there is only one number in the string.  This example assumes there isn't any scientific notation but there might be commas between numbers.  It also assumes there are no numbers in a unit, like sec^2.  the regex function is used to remove all the characters that are not (the ^ is "not") in the set 0-9, minus, period, and comma.  the backslash before the minus is an escape to make not be part of a range.  The "" is the replacement value for each character not in the set (deleting them).  The words function turns the result into a list of strings using the comma as a delimiter.

teststring = "weight=-4.8gm,speed=13m/h";

words(regex(teststring, "[^0-9\-.,]","",GLOBALREPLACE),",");

{"-4.8", "13"}

for the simple case (with lots of potential problems) where \D means "not a digit" and the num() function turns the string into a number,

teststring = "48gm";

num(regex(teststring, "\D","",GLOBALREPLACE));

48

Craige

View solution in original post

1 REPLY 1
Craige_Hales
Super User

Re: Extract numbers from string in JSL

This could be simpler if you know the numbers are unsigned integers and there is only one number in the string.  This example assumes there isn't any scientific notation but there might be commas between numbers.  It also assumes there are no numbers in a unit, like sec^2.  the regex function is used to remove all the characters that are not (the ^ is "not") in the set 0-9, minus, period, and comma.  the backslash before the minus is an escape to make not be part of a range.  The "" is the replacement value for each character not in the set (deleting them).  The words function turns the result into a list of strings using the comma as a delimiter.

teststring = "weight=-4.8gm,speed=13m/h";

words(regex(teststring, "[^0-9\-.,]","",GLOBALREPLACE),",");

{"-4.8", "13"}

for the simple case (with lots of potential problems) where \D means "not a digit" and the num() function turns the string into a number,

teststring = "48gm";

num(regex(teststring, "\D","",GLOBALREPLACE));

48

Craige