- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
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!
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
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