# Tag Info

7

Here is some code I have for entering a 8 digit id number. So this is "out of the box" as is. If you enter more than 8 digits the extra characters are immediately deleted. You can modify to suit your purpose: InputField[Dynamic[id, (id = Which[ StringMatchQ[#, DigitCharacter ..] && StringLength[#] >= 8,StringTake[#, 8], ...

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 = ...

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

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

4

There is nothing special about ToString. In this kind of replacement you need to use RuleDelayed, :>. Subscript[A, 1] /. Subscript[A, x_] :> ToString[x] Try applying Trace to both forms of the expression and compare the order things are evaluated.

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]

3

I decided to edit the title and answer this question as wiki because I think it is not easy for new users to find this kind of information. The more if they are not aware of special handling of ? ! in search fields. So the explanation of things like: \!$$TraditionalForm\ c (1 + x)$$ is in the tutorial String Representation of Boxes. Here are ...

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

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.

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.235", "1.23*^4", "Print[fail]"}; StringMatchQ[test, p2] {True, True, True, True, True, ...

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

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

1

Subscript[A, 1] /. Subscript[A, x_] -> x // ToString

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