Tag Info

Hot answers tagged

13

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]*"] ]


10

The problem does not lie with WordBoundary, it is due to the use of __. The string pattern WordBoundary ~~ __ ~~ "man" will find a word boundary properly, but then will match a following sequence of characters up to "man" without restriction -- including characters that lie on word boundaries themselves. To exclude that possibility, we should restrict the ...


9

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

Version 10 introduced Interpreter which would seem suited to this task: Interpreter[form] represents an interpreter object that can be applied to a string to try to interpret it as an object of the specified form. Interpreter["Number"]["1.23e-5"] 0.0000123 Unfortunately it seems that like many new-in-10 functions this is far from optimized. ...


5

There is a reason to use WordBoundary, but your example sentence doesn't bring it out. Consider mystring = "I am a demanding fan of Superman, Spiderman and Batman."; StringCases[mystring, LetterCharacter ... ~~ "man"] {"deman", "Superman", "Spiderman", "Batman"} so you might prefer StringCases[mystring, LetterCharacter ... ~~ "man" ~~ WordBoundary] ...


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

With V10 one can also do str = "4.5e2, 0.08, 5, 10"; Interpreter[DelimitedSequence["Number"]][str] // Flatten {450., 0.08, 5, 10} Or even str = "4.5e2, 0.08, 5, 10, one, one divided by forty-eight"; Interpreter[DelimitedSequence["SemanticExpression"]][str] // Flatten //InputForm {450., 0.08, 5, 10, 1, 1/48}


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

This is because Mathematica looks for the largest piece of the expression that matches the pattern. Your pattern is x_, which means every conceivable expression there is will match that pattern. As a result, the entire expression {x,y,z} will be matched as it is the largest matching piece (x, y, and z won't be matched because they are smaller pieces compared ...


3

Here is one approach that does what you want: phoneSpell[n_Integer] := With[{k = StringLength@ToString@n, v = IntegerDigits@n, r = {0 -> "0", 1 -> "1", 2 -> "ABC", 3 -> "DEF", 4 -> "GHI", 5 -> "JKL", 6 -> "MNO", 7 -> "PRS", 8 -> "TUV", 9 -> "WXY"}}, StringJoin @@@ Flatten[Outer[List, Sequence @@ Characters[v /. ...


3

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


3

The two cases are different but here is something you can try: list = {a : horse, b : chicken, c : fish}; list2 = {section 1, section 2, section 3}; list /. x_Pattern :> Last@x {horse, chicken, fish} list2 /. section -> 1 {1, 2, 3}


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

@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

lst1 = {{x -> -(-3)^(1/4)}, {x -> -i (-3)^(1/4)}, {x -> i (-3)^(1/4)}, {x -> (-3)^(1/4)}}; lst2 = {a : horse, b : chicken, c : fish}; lst3 = {section 1, section 2, section 3}; lst4 = {section1, section2, section3}; Last @@@ lst1 (* {-(-3)^(1/4),-(-3)^(1/4) i,(-3)^(1/4) i,(-3)^(1/4)} *) Last /@ lst2 (* {horse,chicken,fish} *) ...


2

What goes wrong The help page of ReplaceAll explains why your code is not doing what you want. ReplaceAll (/.) applies a rule or list of rules in an attempt to transform each subpart of an expression expr In the end it applies the rule to the largest subpart it can match. The expression {x,y,z} has two levels (depth of two). {x, y, z} // FullForm ...


2

Replace[{x, y, z}, x_ :> ("^_^" <> ToString[x]), Infinity]


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", " ...


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"}}


1

For completeness sake, another solution: str = "1,2,3,5,10,12,13,17,26,30,32,41,42,43,113,115,121,125"; FromDigits /@ StringSplit[str, ","]


1

why not simply: sol = Solve[x^4 + 3 == 0, x] x /. sol or #[[2]] & @@@ sol (*{-(-3)^(1/4), -I (-3)^(1/4), I (-3)^(1/4), (-3)^(1/4)}*) for the second Example you can try: list = {a : horse, b : chicken, c : fish} #[[2]] & @@@ Transpose[{list}] (*{horse, chicken, fish}*)


1

In your example StringCases works as expected because by default it searches the Longest substring which matches the pattern __ (by default all repeated string patterns are greedy). Apparently the whole string without ending "." matches your pattern: In[21]:= mystring = "I am a big fan of Superman, Spiderman and Batman."; substr = StringCases[mystring, ...


1

Slight modifications of existing answers of @RunnyKine and @Algohi (thanks guys!) numRules = MapThread[ Rule, {Range[1, 9], {{"1"}}~ Join~(Partition[CharacterRange["A", "Y"] // DeleteCases[#, "Q"] &, 3])}] (* {1 -> {"1"}, 2 -> {"A", "B", "C"}, 3 -> {"D", "E", "F"}, 4 -> {"G", "H", "I"}, 5 -> {"J", "K", "L"}, 6 -> {"M", ...


1

borrowing rules from RunnyKine r = {0 -> "0", 1 -> "1", 2 -> "ABC", 3 -> "DEF", 4 -> "GHI", 5 -> "JKL", 6 -> "MNO", 7 -> "PRS", 8 -> "TUV", 9 -> "WXY"}; n = 652; StringJoin @@@ Tuples[Characters /@ IntegerDigits@n /. r] (*{"MJA", "MJB", "MJC", "MKA", "MKB", "MKC", "MLA", "MLB", "MLC", "NJA", "NJB", "NJC", "NKA", "NKB", ...



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