Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- JMP User Community
- :
- Discussions
- :
- Discussions
- :
- Extract numbers from string in JSL

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

Dec 16, 2015 3:40 AM
(7258 views)

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

Dec 16, 2015 5:56 AM
(10387 views)
| Posted in reply to message from percevalsondag0 12/16/2015 06:40 AM

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

1 REPLY 1

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

Dec 16, 2015 5:56 AM
(10388 views)
| Posted in reply to message from percevalsondag0 12/16/2015 06:40 AM

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