0
$\begingroup$
str = "CL-5{n=7 c=[31.233, 36.959, 40.813, 42.268, 36.190, 31.346, 

24.133, 15.885, 17.567, 18.853, 25.427, 32.991, 42.495, 43.548, 

41.307, 35.565, 27.053, 18.885, 18.584, 19.133, 25.058, 32.958, 

39.679, 42.313, 41.398, 37.590, 33.047, 21.156, 18.761, 17.984, 

20.603, 26.838, 34.474, 41.208, 42.480, 40.667, 33.434, 27.221, 

18.512, 17.914, 22.630, 24.177, 32.080, 36.726, 40.738, 40.612, 

37.544, 27.513, 22.225, 17.954, 16.265, 22.359, 28.801, 36.117, 

40.782, 40.727, 40.712, 34.092, 24.581, 16.896] r=[4.174, 1.649, 

3.674, 3.395, 3.642, 3.388, 2.293, 1.811, 4.130, 3.326, 4.033, 3.042, 

3.044, 3.900, 4.717, 4.310, 3.330, 2.370, 3.668, 2.358, 3.215, 4.126, 

1.910, 3.815, 5.071, 4.863, 3.767, 3.745, 2.670, 2.684, 2.591, 3.784, 

3.605, 3.499, 3.758, 4.463, 5.747, 4.874, 3.369, 4.010, 3.400, 5.349, 

3.227, 5.278, 3.202, 4.485, 5.267, 4.921, 2.205, 3.467, 3.560, 2.147, 

2.989, 3.637, 3.299, 3.088, 3.388, 4.747, 4.802, 2.888]}";

One of my solution

data = Association[ToExpression /@ (StringReplace[#, ":" -> ","] & /@ (StringSplit[

StringReplace[  StringCases[#, "{" ~~ d__ ~~ "}" :> d] &@#, {"[" -> "{", 

   "]" -> "}", ", " -> ":", "=" -> "->", " " -> ","}][[1]], 

","]))] &@str;

data

(*
    <|n->7,c->{31.233,36.959,40.813,42.268,36.19,31.346,24.133,15.885,17.567,18.853,25.427,32.
991,42.495,43.548,41.307,35.565,27.053,18.885,18.584,19.133,25.058,32.958,39.679,42.313,41
.398,37.59,33.047,21.156,18.761,17.984,20.603,26.838,34.474,41.208,42.48,40.667,33.434,27.
221,18.512,17.914,22.63,24.177,32.08,36.726,40.738,40.612,37.544,27.513,22.225,17.954,16.2
65,22.359,28.801,36.117,40.782,40.727,40.712,34.092,24.581,16.896},r->{4.174,1.649,3.674,3
.395,3.642,3.388,2.293,1.811,4.13,3.326,4.033,3.042,3.044,3.9,4.717,4.31,3.33,2.37,3.668,2
.358,3.215,4.126,1.91,3.815,5.071,4.863,3.767,3.745,2.67,2.684,2.591,3.784,3.605,3.499,3.7
58,4.463,5.747,4.874,3.369,4.01,3.4,5.349,3.227,5.278,3.202,4.485,5.267,4.921,2.205,3.467,
3.56,2.147,2.989,3.637,3.299,3.088,3.388,4.747,4.802,2.888}|>
*)

Any simpler way to parse the string?

note:there is a space after the comma in c[31.223, ...

$\endgroup$
2
$\begingroup$
newstr = StringReplace[str,  
   Rule @@@ {{", ", ","}, {"{", " "}, {"}", " "}, {"[", "{"}, {"]", "}"}}];

ToExpression@
 StringCases[newstr, var : Except[" "] .. ~~ "=" ~~ val : Except[" "] .. -> {var, val}]

returns

{{n, 7}, 
{c, {31.233, 36.959, 40.813, 42.268, 36.19, 31.346, 24.133, 
   15.885, 17.567, 18.853, 25.427, 32.991, 42.495, 43.548, 41.307, 
   35.565, 27.053, 18.885, 18.584, 19.133, 25.058, 32.958, 39.679, 
   42.313, 41.398, 37.59, 33.047, 21.156, 18.761, 17.984, 20.603, 
   26.838, 34.474, 41.208, 42.48, 40.667, 33.434, 27.221, 18.512, 
   17.914, 22.63, 24.177, 32.08, 36.726, 40.738, 40.612, 37.544, 
   27.513, 22.225, 17.954, 16.265, 22.359, 28.801, 36.117, 40.782, 
   40.727, 40.712, 34.092, 24.581, 16.896}}, 
{r, {4.174, 1.649, 3.674,
    3.395, 3.642, 3.388, 2.293, 1.811, 4.13, 3.326, 4.033, 3.042, 
   3.044, 3.9, 4.717, 4.31, 3.33, 2.37, 3.668, 2.358, 3.215, 4.126, 
   1.91, 3.815, 5.071, 4.863, 3.767, 3.745, 2.67, 2.684, 2.591, 3.784,
    3.605, 3.499, 3.758, 4.463, 5.747, 4.874, 3.369, 4.01, 3.4, 5.349,
    3.227, 5.278, 3.202, 4.485, 5.267, 4.921, 2.205, 3.467, 3.56, 
   2.147, 2.989, 3.637, 3.299, 3.088, 3.388, 4.747, 4.802, 2.888}}}
| improve this answer | |
$\endgroup$
1
$\begingroup$

This is a different approach:

StringCases[
  str, 
  StartOfString ~~ type : Except["{"] .. ~~ "{" ~~ "n=" ~~ n : DigitCharacter 
    ~~ WhitespaceCharacter .. 
    ~~ vectors : (LetterCharacter .. ~~ "=[" ~~ Except["]"] .. ~~ "]" ~~ 
                  WhitespaceCharacter ...) .. ~~ ___ :> 
    {type, n, StringCases[vectors, a : LetterCharacter .. 
                                   ~~ "=[" ~~ b : Except["]"] .. ~~ "]"
                                   ~~ WhitespaceCharacter ... :>
      {a, ToExpression /@ StringSplit[b, ","]}]}]

{{"CL-5", "7", {{"c", {31.233, 36.959, 40.813, 42.268, 36.19, 31.346, 24.133, 15.885, 17.567, 18.853, 25.427, 32.991, 42.495, 43.548, 41.307, 35.565, 27.053, 18.885, 18.584, 19.133, 25.058, 32.958, 39.679, 42.313, 41.398, 37.59, 33.047, 21.156, 18.761, 17.984, 20.603, 26.838, 34.474, 41.208, 42.48, 40.667, 33.434, 27.221, 18.512, 17.914, 22.63, 24.177, 32.08, 36.726, 40.738, 40.612, 37.544, 27.513, 22.225, 17.954, 16.265, 22.359, 28.801, 36.117, 40.782, 40.727, 40.712, 34.092, 24.581, 16.896}}, {"r", {4.174, 1.649, 3.674, 3.395, 3.642, 3.388, 2.293, 1.811, 4.13, 3.326, 4.033, 3.042, 3.044, 3.9, 4.717, 4.31, 3.33, 2.37, 3.668, 2.358, 3.215, 4.126, 1.91, 3.815, 5.071, 4.863, 3.767, 3.745, 2.67, 2.684, 2.591, 3.784, 3.605, 3.499, 3.758, 4.463, 5.747, 4.874, 3.369, 4.01, 3.4, 5.349, 3.227, 5.278, 3.202, 4.485, 5.267, 4.921, 2.205, 3.467, 3.56, 2.147, 2.989, 3.637, 3.299, 3.088, 3.388, 4.747, 4.802, 2.888}}}}}

| improve this answer | |
$\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.