Skip to main content
Post Undeleted by RunnyKine
added 592 characters in body
Source Link
RunnyKine
  • 33.3k
  • 3
  • 110
  • 176

Since I'm still new to String Patterns here is another entry using StringCases.

string = "y = 0.97*x1 + 0.521*x2 - 30.21 - 0.07431*x3 - 0.126*x4 - 0.1939*x5 - 0.361*x6";

StringCases[string, s : ({" ", " + ", " - "} ~~ NumberString) ~~ "*" ~~ 
   x : ("x" ~~ DigitCharacter ..) :> {x, ToExpression@s}]

Gives:

{{"x1", 0.97}, {"x2", 0.521}, {"x3", -0.07431}, {"x4", -0.126}, {"x5", -0.1939}, {"x6",-0.361}}

Note the use of DigitCharacter .. for situations with variables like x10, x11 etc.

Kuba pointed out that cases like "y = x2" cannot be handled by the above method, so we do some StringReplacement before proceeding:

string2 = "y = 0.97*x1 + x2 - 30.21 - x3 - 0.126*x4 - 0.1939*x5 - 0.361*x6";

Now

rp = StringReplace[string2, " " ~~ x : ("x" ~~ DigitCharacter ..) :> " 1" ~~ "*" ~~ x]

Then as before:

StringCases[rp, s : ({" ", " + ", " - "} ~~ NumberString) ~~ "*" ~~ 
                x : ("x" ~~ DigitCharacter ..) :> {x, ToExpression@s}]
{{"x1", 0.97}, {"x2", 1}, {"x3", -1}, {"x4", -0.126}, {"x5", -0.1939}, {"x6", -0.361}}

Since I'm still new to String Patterns here is another entry using StringCases.

string = "y = 0.97*x1 + 0.521*x2 - 30.21 - 0.07431*x3 - 0.126*x4 - 0.1939*x5 - 0.361*x6";

StringCases[string, s : ({" ", " + ", " - "} ~~ NumberString) ~~ "*" ~~ 
   x : ("x" ~~ DigitCharacter ..) :> {x, ToExpression@s}]

Gives:

{{"x1", 0.97}, {"x2", 0.521}, {"x3", -0.07431}, {"x4", -0.126}, {"x5", -0.1939}, {"x6",-0.361}}

Note the use of DigitCharacter .. for situations with variables like x10, x11 etc.

Since I'm still new to String Patterns here is another entry using StringCases.

string = "y = 0.97*x1 + 0.521*x2 - 30.21 - 0.07431*x3 - 0.126*x4 - 0.1939*x5 - 0.361*x6";

StringCases[string, s : ({" ", " + ", " - "} ~~ NumberString) ~~ "*" ~~ 
   x : ("x" ~~ DigitCharacter ..) :> {x, ToExpression@s}]

Gives:

{{"x1", 0.97}, {"x2", 0.521}, {"x3", -0.07431}, {"x4", -0.126}, {"x5", -0.1939}, {"x6",-0.361}}

Note the use of DigitCharacter .. for situations with variables like x10, x11 etc.

Kuba pointed out that cases like "y = x2" cannot be handled by the above method, so we do some StringReplacement before proceeding:

string2 = "y = 0.97*x1 + x2 - 30.21 - x3 - 0.126*x4 - 0.1939*x5 - 0.361*x6";

Now

rp = StringReplace[string2, " " ~~ x : ("x" ~~ DigitCharacter ..) :> " 1" ~~ "*" ~~ x]

Then as before:

StringCases[rp, s : ({" ", " + ", " - "} ~~ NumberString) ~~ "*" ~~ 
                x : ("x" ~~ DigitCharacter ..) :> {x, ToExpression@s}]
{{"x1", 0.97}, {"x2", 1}, {"x3", -1}, {"x4", -0.126}, {"x5", -0.1939}, {"x6", -0.361}}
Post Deleted by RunnyKine
Source Link
RunnyKine
  • 33.3k
  • 3
  • 110
  • 176

Since I'm still new to String Patterns here is another entry using StringCases.

string = "y = 0.97*x1 + 0.521*x2 - 30.21 - 0.07431*x3 - 0.126*x4 - 0.1939*x5 - 0.361*x6";

StringCases[string, s : ({" ", " + ", " - "} ~~ NumberString) ~~ "*" ~~ 
   x : ("x" ~~ DigitCharacter ..) :> {x, ToExpression@s}]

Gives:

{{"x1", 0.97}, {"x2", 0.521}, {"x3", -0.07431}, {"x4", -0.126}, {"x5", -0.1939}, {"x6",-0.361}}

Note the use of DigitCharacter .. for situations with variables like x10, x11 etc.