1
$\begingroup$

Consider a few files with filename structure

data/Neutrino distribution functions/NAME_distribution_mass=XX.0_theta=0.YY.txt 

where XX/YY may have an arbitrary number of digits.

I use

files= FileNames["*.txt", FileNameJoin[{NotebookDirectory[], "data/Neutrino distribution functions/"}]]; Filenames = Table[Last@FileNameSplit@files[[i]], {i, 1, Length[files], 1}]; 

say,

 filenames = {"Electron_distribution_mass=100.0_theta=0.009238898.txt", "Electron_distribution_mass=100.0_theta=0.01600224.txt", "Electron_distribution_mass=10.0_theta=0.5074418.txt"} 

Could you please tell me how to obtain the output in the form

{NAME,XX.0, 0.YY} 

Edit.

It seems that I have solved this problem.

This is my current attempt:

 FilenameParameters[i_] := StringCases[filenames[[i]], mixing__ ~~ "_distribution_mass=" ~~ mass : NumberString ~~ "_theta=" ~~ angle : NumberString ~~ ".txt" :> {mixing, mass, angle}] 

E.g.,

FilenameParameters[1] 

returns

{{"Electron", 100.0, 0.009238898}}

$\endgroup$

1 Answer 1

5
$\begingroup$

This can be done using StringCases:

filename = "data/Neutrino distribution functions/NAME_distribution_mass=XX.0_theta=0.YY.txt"; First@StringCases[ path__ ~~ "/" ~~ name__ ~~ "_distribution_mass=" ~~ mass__ ~~ "_theta=" ~~ theta__ ~~ ".txt" :> {name, mass, theta} ]@filename (* {"NAME", "XX.0", "0.YY"} *) 
$\endgroup$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.