1
$\begingroup$

Consider the following string:

string = "Displacement_0.5_ToDecVol_38_FidLength_50";

I would like to extract the three numbers from it: {0.5,38,50}. This is an attempt:

parameters = 
 StringCases[string, 
   "Displacement_" ~~ displacement : NumberString ~~ "_ToDecVol_" ~~ 
     ToDecVol : NumberString ~~ "_FidLength_" ~~ 
     FidLength : NumberString ~~ _ :> {displacement, ToDecVol, 
     FidLength}][[1]]//ToExpression

However, it returns only the first digit from the last number:

{0.5, 38, 5}

The same situation would happen if setting any other number, say 511, instead of 50: it drops the last digit. Could you please tell me how to fix the code?

$\endgroup$

1 Answer 1

4
$\begingroup$

The smallest fix would be to change the final _ to ___. I.e. the pattern would look like this:

"Displacement_" ~~ displacement : NumberString ~~ "_ToDecVol_" ~~ 
  ToDecVol : NumberString ~~ "_FidLength_" ~~ 
  FidLength : NumberString ~~ ___

But you could just do this:

StringCases[string, NumberString] // ToExpression
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.