Tag Info

Hot answers tagged

14

I propose: StringCases[ {"Fe3O4", "CO", "MgO", "Uut14AuO6"}, DigitCharacter .. | (_?UpperCaseQ ~~ ___?LowerCaseQ) ] {{"Fe", "3", "O", "4"}, {"C", "O"}, {"Mg", "O"}, {"Uut", "14", "Au", "O", "6"}} Or as a RegularExpression: StringCases[ {"Fe3O4", "CO", "MgO", "Uut14AuO6"}, RegularExpression["\\d+|[A-Z][a-z]*"] ]


12

You asked for shortened, improved, so here it is using RegularExpressions: StringCases[string, RegularExpression["(\\D+)(\\d+)"] :> {"$1", ToExpression["$2"]}] {{"ABC", 123}, {"DEFG", 456}, {"HI", 89}, {"UZXX", 1}} Here's a version using StringSplit: Partition[StringSplit[string, RegularExpression["(\\d+)"] :> FromDigits @ "$1"], 2]


10

Paste is a command that, as a side-effect, inserts the contents of the clipboard into the current notebook selection. The return value is always Null, which means that Paste cannot be used for our purpose without some awkward notebook manipulation. There is an undocumented way to access the clipboard: ClipboardNotebook. NotebookGet[ClipboardNotebook[]] ...


10

Shortest[] is the easiest way to stop the greedy pattern matching algorithm: StringCases[StringJoin[string], Shortest["delay(" ~~ x__ ~~ " " ~~ y__ ~~ ")"] -> {x, y}] (* {{"A35", "17"}, {"A88", "20"}, {"A01", "2"}, {"A27", "24"}} *) Edit: As @Öskå showed in his answer, it can be done without using Shortest[] too. Here's another way that doesn't need ...


7

Another solution would be to use SemanticImportString (new in 10). Borrowing some code from Mr.Wizard so that I can compare my solution to his: strings = ToString @ Row[RandomChoice /@ {{"-", ""}, {#}, {"e"}, {"-", ""}, Range@12}] & /@ RandomReal[{0, 10}, 15000]; Needs["GeneralUtilities`"] Internal`StringToDouble /@ strings // AccurateTiming ...


6

string = "ABC123DEFG456HI89UZXX1"; Partition[StringSplit[string, x : NumberString :> ToExpression@x], 2] or Partition[StringSplit[string, x : NumberString :> FromDigits@x], 2] or Split[StringSplit[string, x : NumberString :> FromDigits@x], Head@#2 === Integer &] (* {{"ABC",123},{"DEFG",456},{"HI",89},{"UZXX",1}} *)


6

StringSplit with Partition works fine for this particular case Partition[#, 2] &@StringSplit[string, x : DigitCharacter .. :> ToExpression@x] (* {{"ABC", 123}, {"DEFG", 456}, {"HI", 89}, {"UZXX", 1}} *)


5

I am seeking a more elegant approach but this gets the job done: stringChunk[str_String, ch : {_Integer?Positive ..}] := Module[{dynP, n = StringLength@str}, dynP[l_, p_] := MapThread[l ~StringTake~ {#, #2} &, {{0} ~Join~ Most@# + 1, #} &@Accumulate@p]; dynP[str, #] & /@ Join @@ Permutations /@ IntegerPartitions[n, n, ch] ] Test: ...


5

Without Reg. Exp.: string = "ABC123DEFG456HI89UZXX1"; StringSplit[string, PatternSequence[x : LetterCharacter .., y : DigitCharacter ..] :> {x, ToExpression@y}][[;; ;; 2]] (* {{"ABC", "123"}, {"DEFG", "456"}, {"HI", "89"}, {"UZXX", "1"}} *)


5

With all the answers we need a timing comparison. Functions: bel[string_] := StringSplit[string, PatternSequence[x : Longest[LetterCharacter ..], y : Longest[DigitCharacter ..]] :> {x, ToExpression@y}][[;; ;; 2]]; RK1[string_] := StringCases[string, RegularExpression["(\\D+)(\\d+)"] :> {"$1", ToExpression["$2"]}]; RK2[string_] := ...


4

nu = ToExpression[StringCases[string, DigitCharacter ..]]; le = StringCases[string, LetterCharacter ..]; Transpose[{le, nu}] using StringSplit: nu = ToExpression[StringSplit[string, __?LetterQ]]; le = StringSplit[string, __?DigitQ]; Transpose[{le, nu}]


4

StringCases[string, x : LetterCharacter .. ~~ y : NumberString :> {x, ToExpression@y}]


4

I find it difficult to parse the runes in Mr.Wizard's answer, but here is a simple functional way: substrs[str_] := substrs[str] = If[StringLength[str] == 2, {Characters[str], {str}}, If[StringLength[str] == 1, {{str}}, Join[Map[Prepend[#, StringTake[str, 1]] &, substrs[StringDrop[str, 1]]], Map[Prepend[#, StringTake[str, 2]] &, ...


4

I prefer one of @Mr.Wizard's solutions based on StringCases, but here is a solution using StringSplit: StringSplit["Fe3O4", RegularExpression["(?=[A-Z]|\\d)"]] (* {"Fe", "3", "O", "4"} *) It splits the string at any position that is followed by an upper case letter or a digit. If multiple digits are possible: StringSplit["Fe23O42", ...


4

Well I just saw your comment that says you want "all strings" so perhaps a different approach: StringReplace["{{a},{b,c,d},{e,{[f],{g}}}}", x : Except["{" | "," | "}"] .. :> "\"" <> x <> "\""] // ToExpression {{"a"}, {"b", "c", "d"}, {"e", {"[f]", {"g"}}}} If that doesn't work consider manipulating the raw box format produced by ...


3

You can use chemSplit[s_String] := Module[{pos = StringPosition[s, {_?UpperCaseQ, NumberString}, Overlaps -> False][[All, 1]]}, StringSplit@StringInsert[s, " ", pos] ] chemSplit["Fe3O4"] {"Fe", "3", "O", "4"}


3

elements = SortBy[ElementData[#, "Abbreviation"] & /@ ElementData[], Minus@*StringLength]; StringCases["Fe3O2", DigitCharacter .. | elements] {"Fe", "3", "O", "2"} (Thanks to Mr.Wizard for syntax improvements.)


2

@Mr.Wizard s = StringReplace["{{a},{b,c,d},{e,{[f],{g}}}}", x : Except["{" | "," | "}"] .. :> "\"" <> x <> "\""] // ToExpression check = If[SyntaxQ@#, ToExpression@#, #] &; ReplaceAll[s, x_String :> check@x] // InputForm (*out*) {{a}, {b, c, d}, {e, {"[f]", {g}}}}


2

StringCases[StringJoin[string], "delay(" ~~ x : Except[")"] .. ~~ ")" :> StringSplit[x]] (* {{"A35","17"},{"A88","20"},{"A01","2"},{"A27","24"}} *) or, StringCases[ToString[string], "delay(" ~~ x:Except[")"] .. ~~ ")" :> StringSplit[x, ","]] (*thanks: eldo *) (* {{"A35", " 17"}, {"A88", " 20"}, {"A01", " ...


2

Just to be different f = Flatten[List @@@ WolframAlpha["formula " <> #, "Result"][[1, 1]]] &; f /@ {"Fe2O3", "MgO"} {{"Fe", 2, "O", 3}, {"Mg", "O"}} This approach seems to be stupid, but it can be easily extended to another chemical data (e.g. molar mass).


2

Here is a reasonably fast approach using the nice SatisfiabilityInstances[]: ClearAll[stringSets]; stringSets[str_String] := Module[{x, len}, len = StringLength@str - 1; StringSplit[StringJoin /@ (Riffle[Characters@str, #] & /@ (SatisfiabilityInstances[And @@ Table[Or[x[n], ...


1

str={"1,2,3,5,10,12,13,17,26,30,32,41,42,43,113,115,121,125"} ToExpression[StringCases[str[[1]], DigitCharacter ..]] ToExpression[StringSplit[str[[1]], ","]] ToExpression["{" ~~ str[[1]] ~~ "}"]


1

stringbreak[str_] := ( StringJoin[#[[All, 1]]] & /@ GatherBy[Transpose[{Characters[str], Accumulate@#}], #[[2]] & ] & /@ Nest[ Flatten[ If[Last@# == 0, {Append[#, 1]}, {Append[#, 0], Append[#, 1]}] & /@ # , 1] &, {{1}},# -1]) &@ StringLength[str] ...


1

It's on my list of things to tackle as part of the Incremental Language Development project. In the meantime, there is a built-in solution that is fairly fast, which is: Needs["MachineLearning`"]; ToUpperCaseUnicode[{"éàÇœßþ\[Sigma]\[FinalSigma]", "ijķnjđӽծ", "ÿ"}] {"ÉÀÇŒSSÞ[CapitalSigma][CapitalSigma]", "IJĶNJĐӼԾ", "Ÿ"} (not sure how to copy-paste the ...


1

Just a solution without Shortest because I simply didn't know about it: StringReplace[#, ")" -> ""] & /@ StringCases[StringJoin[string], "delay(" ~~ x__ ~~ " " ~~ y__ ~~ ")" /; (StringLength@x < 5 && StringLength@y < 5) :> {x, y}] {{"A35", "17"}, {"A88", "20"}, {"A01", "2"}, {"A27", "24"}}



Only top voted, non community-wiki answers of a minimum length are eligible