Regular Expression with +/-
lst = {"Today+100", "Yesterday-200"};
StringSplit
StringSplit[lst, RegularExpression["([+-]\\d+)"] :> ToExpression@"$1"]
{{"Today", 100}, {"Yesterday", -200}}
StringSplit[lst, n : NumberString :> ToExpression@n]
{{"Today", 100}, {"Yesterday", -200}}
StringReplace
List @@@ StringReplace[lst, RegularExpression["([+-]\\d+)"] :> ToExpression@"$1"]
List @@@ StringReplace[lst, n : NumberString :> ToExpression@n]
Sequence @@@ StringReplace[lst,
RegularExpression["(\\w+)([+-]\\d+)"] :> {"$1", ToExpression@"$2"}]
Sequence @@@ StringReplace[lst,
w : WordCharacter .. ~~ n : NumberString :> {w, ToExpression @ n}]
all give
{{"Today", 100}, {"Yesterday", -200}}
Note: My original answer used the regex substring [\\+\\-]
. I learned from @user1066's comment and answer that it can be replaced with much nicer [+-]
.
Maybe this will help:
StringCases[#,
WordCharacter .. | (_ ~~ DigitCharacter ..)] & /@ {"Today+100",
"Yesterday-200"} // Flatten
(* {"Today", "+100", "Yesterday", "-200"} *)
A slight modification of @kglr's first method, (the one containing the neat use of ToExpression
):
StringSplit[lst,
RegularExpression["[+-]\d{3}"]:> ToExpression["$0"]]
{{Today, 100}, {Yesterday, -200}}
(Originally posted as a comment)