cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Choose Language Hide Translation Bar
joshua
Level III

Extracting digits before and after the comma

Hi,

 

I'm struggling from extracting digits from a string before and after the comma

how can I get 0.98 and 0.97 from a string?

string = "A_BD_CEEE:0.98,0.975620708";

first_number = Num( Regex( string, "(\d\d?\d?):", "\1" ) );

expected out

first_number = 0.98

second_number = 0.975

 

thanks

 

3 REPLIES 3
txnelson
Super User

Re: Extracting digits before and after the comma

Here is how I would do it

names default to here(1);
string = "A_BD_CEEE:0.98,0.975620708";

first number=num(word(2,string,":,"));
second number = round(num(word(3,string,":,")),3);
Jim
jthi
Super User

Re: Extracting digits before and after the comma

If the numbers cannot be negative this should work:

 

Names Default To Here(1);

string = "A_BD_CEEE:0.98,0.975620708";
numString = Regex Match(string, "(\d+.\d*),(\d+.\d*)");
first_number = Num(numString[2]);
second_number = Num(numString[3]);
Show(first_number, second_number);

 

-Jarmo
Georg
Level VII

Re: Extracting digits before and after the comma

In addition to the solution of @jthi ,

there is quite a good documentation on regex:

https://www.jmp.com/support/help/en/16.0/jmp/regular-expressions.shtml#

The problem of your initial approach was, that there is no number before :, only after.

And you need to include the . into the string to get the first number. See changes below.

Regex is worth to learn because it is really powerful.

 

string = "A_BD_CEEE:0.98,0.975620708";

first_number = Num( Regex( string, ":(\d.\d?\d?)", "\1" ) );

 

Georg