1
$\begingroup$

Consider the string

string="Tau_distribution_mass=547.0_theta=6.87982e-05.txt";

I would like to make a row

{distr,massvalue, thetavalue};

I write

StringCases[string, 
 mixing__ ~~ "_distribution_mass=" ~~ mass : NumberString ~~ 
   "_theta=" ~~ angle : NumberString ~~ ".txt" :> {mixing, 
   ToExpression[mass], ToExpression[angle]}]

but the problem is that NumberString does not recognize the scientific form of numbers, because of which the code returns empty. Could you please tell me what is an analog of NumberString in this example?

$\endgroup$
1
  • 2
    $\begingroup$ Rather than StringCases, I would use: MapAt[Interpreter["Number"],StringSplit[string,{"_distribution_mass=","_theta=",".txt"}] ,{2;;3}] $\endgroup$
    – chuy
    May 4, 2021 at 16:37

1 Answer 1

3
$\begingroup$

This is a case for RegularExpression:

s = "Tau_distribution_mass=547.0_theta=6.87982e-05.txt";
regex = RegularExpression[
   "^([^_]+).*?(\\d+\\.+\\d*).*?(\\d+\\.+\\d*e+-+\\d*)"];

StringCases[s, regex -> "$1,$2,$3"]

{"Tau,547.0,6.87982e-05"}
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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