Tag Info

Hot answers tagged

10

You can test whether braces are balanced with something like (version simplified by Guess who it is.): ClearAll[balancedBracesQ] balancedBracesQ[str_String] := StringCount[str, "{"] === StringCount[str, "}"] And use it in string pattern in following way: StringCases[ "a\\newcommand{ABC{asdas}{asdsad}}{DEF{x}y}asdkashdkj\ ...


9

I suppose that the set of answers won't be complete without the contribution of a cryptic RegularExpression... $pattern = RegularExpression["\\\\newcommand({([^{}]|(?1))*})(?1)"]; The pattern works for simple cases: StringCases[#, $pattern]& @ "XXX\\newcommand{ABC}{DEF}XXX\\newcommand{GHI}{JKL}XXX\\newcommand{MNO}{PQR}" (* { ...


8

You have too many backslashes. ImportString["\"Applications num\303\251riques\"", "Text", CharacterEncoding -> "UTF8"] does what you want. If you already have the string imported into Mathematica incorrectly, you can use FromCharacterCode with ImportString, with its second option. In[1]:= "Applications num\303\251riques" Out[1]= "Applications ...


8

Here's an approach for any two multi-dimensional lists of strings which have arbitrary, but matching structures: stringJoin[x__String] := StringJoin[x] SetAttributes[stringJoin, Listable] stringJoin[La, Lb] EDIT Short explanation of listability: Listable functions are effectively applied separately to each element in a list, or to corresponding ...


5

Here are two ways. SemanticInterpretation is sort of fun but extremely slow: SemanticInterpretation["{26.2417,-98.432},{26.2407,-98.4247}", List[_, _] ..] Fixing up your data to fit Yves Klett's suggestion is much faster: ToExpression["{" <> "{26.2417,-98.432},{26.2407,-98.4247}" <> "}"] Both yield (* {{26.2417`, -98.432`}, {26.2407`, ...


5

You also need InputForm if you want to convert π to Pi. If you want parentheses, then 2 π/5 // ToString[#, InputForm] & // StringReplace[#, "/" :> "--"] & "(2*Pi)--5" or else 2 π/5 // ToString[#, InputForm] & // StringReplace[#, {"/" :> "--", "(" :> "", ")" :> ""}] & "2*Pi--5"


5

DeleteCases[Association@KeyValueMap[#1->DeleteCases[#2,_?(StringMatchQ[ToString@#,Whitespace..]&)]&,acs],_?(#==<||>&)]


4

Here is a solution, based on pattern matching in the list of rules of the association: acs //. x_Association :> Normal[x] DeleteCases[ %, (key_->val_String) /; StringMatchQ[val, " "..], Infinity] DeleteCases[ %, _->{}] % /. List->Association (* {A->{a-> ,aa->asd},B->{bb-> }} {A->{aa->asd},B->{}} {A->{aa->asd}} ...


4

sLa = Map[ToString, La, {2}]; sLb = Map[ToString, Lb, {2}]; MapThread[StringJoin, #] & /@ Transpose[{sLa, sLb}] also Thread[j @@ #] & /@ Transpose[{sLa, sLb}] /. j -> StringJoin


4

This is a pretty ugly hack, but maybe it will inspire you to something better. leastCJK = ToCharacterCode["⺀"][[1]]; StringMatchQ[#, _?(ToCharacterCode[#][[1]] < leastCJK &)] & /@ {"a", "1", ".", " ", "中", "あ"} {True, True, True, True, False, False} Note: "⺀" is unicode character U+2E80, CJK RADICAL REPEAT This hack can be used with ...


3

If there is always one space at a time as shown, or if the number of spaces is irrelevant: str = "1a 789 4/7 123/7 asff %$#7 478 9/4"; StringSplit[str] /. s_String /; StringMatchQ[s, DigitCharacter ..] :> s <> "/1" // StringRiffle "1a 789/1 4/7 123/7 asff %$#7 478/1 9/4" Or adjusting J.M.'s comment code to work: StringReplace[str, w1 : ...


3

a = 1; b = 5; c = 12; (* version 10 *) StringTemplate["`1`-`2`-`3`.mx"][a, b, c] (* "1-5-12.mx" *) (* 10 and lower *) StringJoin @@ Riffle[ToString /@ {a, b, c}, "-"] <> ".mx" (* "1-5-12.mx" *)


3

Yes, it is possible to get Mathematica to accept TeXForm input in raw form without quotes, using a method similar to the one described in this answer. Using $PreRead to intercept the input before it is interpreted by Mathematica and then convert it to a string, we can define the latextoma function as a replacement rule with $PreRead = (# /. ...


3

I think this is easier to do by working with strings. First write a function that will expand strings of the form "Power(x,k)" where k is an integer in "xx...x" with k - 1 ""s. f[x_, k_] := Module[{i = Abs[ToExpression[k]] - 1}, Nest[StringJoin[#, "*" <> x] &, x, i]] A couple of tests for f. f["s", 2] "s*s" f["ab", "-3"] ...


3

Accepting that in Mathematica -c/y is automatically converted to -1*c*y^-1 and permitting the result shown in Andy's answer I believe we can use a simpler approach, at least for the kind of expression given in example. Define rules that determine how a Times or Power expression should be counted, then use Cases to find all instances in you expression and ...


2

Here is my possibly over-engineered, potentially incomplete approach: string = "asdkashdkj\\newcommand{ABC}{DEF}asdahgsjagsd\\newcommand{ABC}{DwewEF}ahgdajhdgj\\newcommand{ABC}{DEF}\\newcom\\newcommand{ABC{asdas}{asds{}ad}}{DEF}"; step1 = Characters[string] //. { {a___, "{", inside : Except["{" | "}"] ..., "}", b___} :> {a, ...


2

Please try: StringReplace[in, d1 : DigitCharacter .. ~~ "/" ~~ d2 : DigitCharacter .. :> "\!\(\*FractionBox[\(" ~~ d1 ~~ "\), \(" ~~ d2 ~~ "\)]\)"]


1

After a bit, I hit upon a solution involving a round-trip through the import/export subsystem. First, it is helpful to have everything in the same representation, so we need to put the mathml into XMLElement form, e.g. mathml = ImportString[ExportString[1/2, "MathML"], {"MathML", "XMLElement"}] (* XMLElement["math", {"xmlns" -> ...


1

Without having the .csv file to work on, as guess who it is said, this should get you where your going. cord=Import["mydrive/file.csv","Data"]//ToExpression Again Please give a sample of what the data is in the .csv file.


1

Moving it to a Manipulate sounds like a better idea: Manipulate[ makes = Import[ StringJoin[ "http://www.fueleconomy.gov/ws/rest/vehicle/menu/make?year=", ToString@year], "XML"]; Row[{"Loaded car makes for the year ", Style[ToString[year], {FontFamily -> "Arial", Large, Red}]}], {year, Reverse@years} ]


1

This doesn't give the result you are looking for exactly because it uses the full form of the expression you give it. SetAttributes[countTimes, HoldAll]; countTimes[expr_] := Block[{Times, Power, power, times}, power[a_, b_ /; b > 0] := Nest[times[a, #] &, a, b - 1]; power[a_, b_ /; b < 0] := 1/power[a, -b]; power[a_, 0] := 1; times[a___, ...



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