Tag Info

1

srF = StringReplacePart[#, ToString[Style[StringTake[#, {#2}], ##3], StandardForm], {#2, #2}] &; str = "CDABOZPVRYXSWQEGNILUTHMKJF"; srF[str, #, Red, Bold, 16] & /@ {3,9} Note: Copy as LateX does not work

0

just for fun..we can define a zfill function zfill[n_, f_String: "0"] := Function[{s}, StringJoin[ConstantArray[f, Max[0, n - StringLength[s]]], s]]; then the operation is quite similar to your python expression: zfill[2] /@ ToString /@ {-6, 1, 3, 23} {"-6", "01", "03", "23"}

3

This question is related to at least: Highlighting text with StringReplacePart but also using Style, Subscript How to join two Style[]d strings Fortunately it is simpler than the first one and we can apply the methods provided in the second one. stringBold[s_String, pos_] := "" <> MapAt[Style[#, Bold] ~ToString~ StandardForm &, Characters@s, ...

2

ClearAll[nF] nF = With[{l = #, p = #2}, NumberForm[l, p, SignPadding -> True, NumberPadding -> {"0", ""}, NumberFormat -> (StringTake[#1, -(p + 1)] &)]] &; nF[{-6, 1, 3, 23}, 1] (* {-6, 01, 03, 23} *) nF[{-6, 1, 3, 23, 123}, 2] (* {-06, 001, 003, 023, 123} *) Note: You can also use PaddedForm instead of NumberForm.

3

Taking under consideration your assumptions: StringTake["0" <> ToString[#], -2] & /@ {-6, 1, 3, 23}

2

One way: If[StringLength@# == 1, "0" <> #, #] &@*ToString /@ {-6, 1, 3, 23} Another way: StringJoin@*(ToString /@ PadLeft[#, 2] &)@*Characters@*ToString /@ {-6, 1, 3, 23}

0

list = ImportString[" lastname1 firstname1 I1 lastname2 first2 I2 lastna3 longfirstname2 I3 ", "Table"] /. {} -> Sequence[]; {#2, #} & @@@ list (* {{"firstname1", "lastname1"}, {"first2", "lastname2"}, {"longfirstname2", "lastna3"}} *) ...

0

I suggest you do it by hand, using a convention something like this: filename[ac] = "gamma_2_at_sin_theta_x_2_y" If you have so many outputs that you can't do it by hand, use some kind of index file as per @rasher. I don't think you should do this automatically, because it will be very difficult to write a safe robust general case that avoids difficult ...

3

The command, StringReplace[newA, Shortest["\\[" ~~ z__ ~~ "]"] :> z] replaces strings like \[string] by string. In particular, it transforms newA to Gamma^2-at-Sin[CapitalTheta]-x^2-y_sub as requested. Of course, this procedure will not work, if the string itself contains \\[ or ].

3

You can use StringMatchQ and HexadecimalCharacter to check the list for non-hex items. First I'll make some hexadecimal strings and insert some non-hex ones. hexStrings = IntegerString[RandomInteger[{10, 30000}, 100], 16]; AppendTo[hexStrings, "zxc2"]; PrependTo[hexStrings, "x34c"]; Now we can Map the StringMatchQ function over the list using a Repeated ...

3

IntegerString[2,10,33141015+1]

4

While I was working on alternative TeX export, I had similar requirement. I wanted to export annotated Mathematica code to TeX, with annotations reflecting front ends syntax highlighting. Since I couldn't find a way to use front end itself to do it, I decided to write my own package. My SyntaxAnnotations package is now available on GitHub. It works by ...

3

PaddedForm[2, 33141015, NumberPadding -> {"0", "0"}] Will do it, well, that's the command to do it. Not responsible for monitor bursting into flames, etc.

5

Source Mathematica makes use of the PCRE library. According to http://www.pcre.org/pcre.txt : Within a compiled pattern, offset values are used to point from one part to another (for example, from an opening parenthesis to an alter- nation metacharacter). By default, in the 8-bit and 16-bit libraries, two-byte values are used for ...

5

If you just want to check if Bold exists in Names, there's no need for string matching: MemberQ[Names["*"], "Bold"] (* True *) or even Names["Bold"] != {} (* True *) Names also takes more elaborate string patterns, just as StringMatchQ does. That being said, even in your example, I don't understand, why you're matching all the names against Bold ...

2

I think this follows from the representation of styles (and other boxes) in strings. For example, this: is really represented like this in a string: template="a \!$$\*\nStyleBox[\"\",\nFontColor->RGBColor[1,0,0]]$$ and a " Then either with StringForm or StringTemplate we get In[10]:= StringTemplate[template]["foo", "bar"] Out[10]= "a ...

2

Mathematica 10 introduces IntegerName: IntegerName[n] gives a string containing the full English name of the integer n. IntegerName[n,"type"] gives a string of the specified type. Possible types include: "DigitsWords" a combination of three-digit numbers and words "Words" using only words "Approximate" the first few digits ...

7

TL;DR Recursive expressions are possible using native string patterns in Mathematica, but can be difficult to write correctly, and might perform very poorly. Difficult To Write? As @Leonid's solution shows, it is possible to express recursive patterns without resorting to regular expressions. However, recursive string patterns can be more difficult to ...

5

NOTE Apparently, the solution below isn't quite right, as demonstrated by WReach in his answer. It is, therefore, better to treat this one as a simple illustration of the idea, while the correct one is given by the answer of WReach. In your approach, you need delayed evaluation of the inner pattern bb, to avoid infinite recursion. Here is one way: bb = ...

3

As confirmed by WRI, it is a bug, which is specific to Linux and which was introduced in version 10.0.2. No workaround known yet except for two possible recommendations, especially when it comes to plot labels: Avoid using commas or other short symbols like ":", ";" or maybe even "|". Try renaming your variables so that string sequences before the short ...

2

newstr = StringReplace[str, Rule @@@ {{", ", ","}, {"{", " "}, {"}", " "}, {"[", "{"}, {"]", "}"}}]; ToExpression@ StringCases[newstr, var : Except[" "] .. ~~ "=" ~~ val : Except[" "] .. -> {var, val}] returns {{n, 7}, {c, {31.233, 36.959, 40.813, 42.268, 36.19, 31.346, 24.133, 15.885, 17.567, 18.853, 25.427, 32.991, 42.495, 43.548, 41.307, ...

0

This is a different approach: StringCases[ str, StartOfString ~~ type : Except["{"] .. ~~ "{" ~~ "n=" ~~ n : DigitCharacter ~~ WhitespaceCharacter .. ~~ vectors : (LetterCharacter .. ~~ "=[" ~~ Except["]"] .. ~~ "]" ~~ WhitespaceCharacter ...) .. ~~ ___ :> {type, n, StringCases[vectors, a : LetterCharacter .. ...

3

I'm not certain how general it is, but works :) Just for fun, I've assumed that FrontEnd should know what is a number and what to split on boxes: StringFreeQ[#, LetterCharacter] && c[#][[1, 1]] === # & /@ { "1.23", "1.23", "1.23*^4", "Print[fail]", "string", "11", "111"} {True, True, True, False, False, True, False} Where c is ...

3

RegularExpression may be helpful, like this In[109]:= mylist = {"1.23", "1.23", "1.23*^4", "1.22*^-2", "Print[fail]"}; numberString = RegularExpression[ "[0-9]*.?[0-9]*?"] | (RegularExpression["[0-9]*.?[0-9]*?"] ~~ "*^" ~~ RegularExpression["-?[0-9]+"]); StringMatchQ[mylist, numberString] Do[StringMatchQ[mylist, NumberString], {10^5}] // ...

3

Edit: I came to realize that my original form was redundant. I now propose this instead: p2 = NumberString ~~ "" | "" | ("" | "" ~~ NumberString) ~~ "" | ("\\*^" | "\\*^-" ~~ DigitCharacter ..); Test: test = {"1.234.56*^-7", "1.23", "1.23", "1.23`5", "1.23*^4", "Print[fail]"}; StringMatchQ[test, p2] {True, True, True, True, True, ...

Top 50 recent answers are included