Tag Info

Hot answers tagged

14

This is a bug in version 10.1.0. We decided it was serious enough to warrant a fix via an automatic paclet update. The paclet has been pushed live and Mathematica should install it automatically once it does a periodic check with the paclet server. It should take about a week or so. To install it right away, you can do PacletInstall["StringPatternFix"]. You ...


10

I think you'll find this much faster than answers so far... strnumsrt = Module[{tc = ToCharacterCode[#], tcc, tcr}, tcc = Unitize@Clip[tc, {48, 57}, {0, 0}]; tcr = Pick[Range@Length@tcc, tcc, 1]; tc[[tcr]] = Sort[tc[[tcr]]]; FromCharacterCode[tc]] &;


10

if the keys are unique, then this seems to be rather elegant, short and still clear: Pick[ln, nm, "tat"] probably look at the documentation of Pick...


9

StringReplacePart gets very slow when there are many replacements. A more direct approach proves to have far better complexity. My proposal: subSort[s_String] := Module[{p, ch}, p = StringPosition[s, DigitCharacter]; If[p === {}, Return[s], p = p[[All, 1]]]; ch = Characters[s]; ch[[p]] = Sort @ ch[[p]]; StringJoin[ch] ] rasher took this idea ...


8

As others have indicated this result is not surprising at all. What you actually need is not StringReplace but ReplaceAll (/.): {"90", "", "20"} /. "" -> "0" {"90", "0", "20"}


7

The illustrated behavior seems reasonable to me, even if it may not seem "natural." For position or replacement a given pattern should match when it appears between any two sequences of characters, or it is at the beginning and end of a string. A zero-length string effectively is between every pair of characters. Why should it not match? To keep it from ...


6

Workaround for the two-argument Except in string patterns issue until it is fixed: StringCases["104702", DigitCharacter?(! StringMatchQ[#, "0"] &)] Match the second argument directly, then use PatternTest to check that it also doesn't match the first argument.


6

This is a slight rewrite of the code in the question, rather than anything elegant or clever. You can use the DigitCharacter pattern in StringPosition instead of ToString /@ Range[0, 9], and having found the positions you can use them in StringTake instead of searching the string again with StringCases: With[{p = StringPosition[#, DigitCharacter]}, ...


6

Another option: AssociationThread[nm -> ln]["tat"] If you store AssociationThread[lm -> ln] in a symbol you can use it for many quick lookups without having to recreate the association every time. Without Pick and AssociationThread you might do something like Identity @@ Cases[Transpose[{nm, ln}], {"tat", v_} :> v] Note that I'm using ...


5

That is a rather slippery replacement, "Find nothing and replace it with something" Try telling the replacement that there nothing between the beginning and end StringReplace[{"90", "", "20"}, {StartOfString ~~ "" ~~ EndOfString -> "0"}]


5

You have a couple of options that might get your creative juice flowing: 1) Make a ListPlot songnotes = {"d5", "d5", "e5", "d5", "d5", "c5", "c5", "a4", "a4", "c5"}; timing = {1/4, 1/4, 1/4, 1/4, 1/2, 1/2, 1/2, 1/2, 1/2, 1/2}; noteletters = {"c4", "d4", "e4", "g4", "a4", "c5", "d5", "e5"} Make a substitution rule to help our gathering and sorting. I want ...


5

I am not sure if this is the best way of doing it, so the following is not exactly an answer to the question. Here is how I would do it assuming that I have padding. 0 Preparation data={1,2,3};(*some test data*) (*exporting to some files*) Table[Export["myData" <> IntegerString[i, 10, 5] <> ".txt", data], {i,10}]; (*and deleting one to make it ...


4

This is known to be inefficient because using patterns like this gives a poor complexity, but it works: str = "95uge678r3gi89hgfe30kgh063d51"; FixedPoint[StringReplace[ #, a___ ~~ b : DigitCharacter ~~ c___ ~~ d : DigitCharacter ~~ e___ /; !OrderedQ[{b, d}] :> StringJoin[{a, d, c, b, e}] ] &, str] (Thanks to Simon Woods for ...


4

I thought it worth mentioning that the documentation for StringMatchQ gives the following solution to your problem: Verbatim["p"] specifies the verbatim string "p", with * and @ treated literally. So you'd be able to do something like: StringMatchQ["*", Verbatim["*"]] (* True *)


4

Use StringReplace["Xabcde", "X" ~~ e__ -> e]. Replace, et al are for lists/expressions... Notice that AtomQ@"Xabcde" is True, so regular (non-string) replace operations only "see" it as a singular entity: "Xabcde" /. "Xabcde" -> 1 (* 1 *) From the docs for ReplaceAll: "... to transform each subpart..." - but there is no "subpart" for atoms, so ...


4

I would love this to be uniform for both integer and rational numbers a2 = {2/3, 4/5, 9/7, 3/7, 1.5, 3, 1/9}; StringTrim@StringJoin[" " <> ToString[#, InputForm] & /@ a2] (* 2/3 4/5 9/7 3/7 1.5 3 1/9 *) Row[ToString[#, InputForm] & /@ a2, " "] (* 2/3 4/5 9/7 3/7 1.5 3 1/9 *) StringReplace[ToString[a, InputForm], {"{" | "}" -> ...


3

strng = "95uge678r3gi89hgfe30kgh063d51"; Block[{c = Sort@StringCases[#, DigitCharacter], j = 0}, StringReplace[#, DigitCharacter :> c[[++j]]]] &@strng (* "00uge133r3gi55hgfe66kgh788d99" *)


3

For performance fiends: I had an application that had this very need for some huge sets of long integer strings. kguler's solution is certainly the canonical way, and quite quick, as is Felix's version (I was actually surprised on that one). eldo's solution is probably what most would come up with, but in performance-intensive scenarios the StringSplit is ...


3

Perhaps the closest thing to what you want is to use HoldForm to prevent the sorting behavior of Times due to Orderless: str = "one Test String to see"; HoldForm @@ MakeExpression @ str one Test String to see However it is important to realize that the HoldForm head is still present, only that it is not shown in standard formatted output. Also know ...


3

fixed in 10.1 (windows): code: StringCases["abcadcacb", "a" ~~ x_ ~~ "c"] StringCases["a" ~~ x_ ~~ "c"]["abcadcacb"]


3

I don't know if I understand you correctly. but here is what I got for you: FileNameTake["C:\\Users\\Name\\Folder\\test2.txt"] (*"test2.txt"*) If you want without extension, then: FileBaseName["C:\\Users\\Name\\Folder\\test2.txt"] (*test2*)


3

How about this? SelectFirst[ln, StringMatchQ[#, "tat" ~~ __] &] Admittedly my method is not as elegant as others, but it's fast if your ln and nm are large: ln = ConstantArray[t, 10^5] /. t :> StringJoin@RandomSample[CharacterRange["A", "z"], 10]; nm = StringTake[#, 3] & /@ ln; The following Timing[SelectFirst[ln, StringMatchQ[#, nm[[1]] ~~ ...


2

It does what it is designt to, so either put there a stronger pattern: Flatten @ StringCases[list, StartOfString ~~ "H" ~~ __ ~~ EndOfString] or use more suited functions: Select[list, StringMatchQ[#, "H" ~~ __] &]


2

My main goal here is to provide an example for other people who want to experiment with LibraryLink and strings, as well as to test how fast all of this is (and to become the "bottom line" in the fancy plot of course >:D ). Anyway I made the following functions in C. Note that you have to have a C-compiler set up in such a way that Mathematica knows about ...


2

Just because I wanted to join the party and with no redeeming features (...just like playing with Reap and Sow): f[str_] := Module[{ss = StringSplit[str, ""], a, b, rul}, {a, b} = Reap[MapIndexed[Sow[w[First@#2, #1], DigitQ[#1]] &, ss]]; rul = Join[Thread[b[[1]] -> Sort[b[[1, All, 2]]]], Thread[b[[2]] -> b[[2, All, 2]]]]; StringJoin[a ...


2

FromDigits /@ StringSplit["1 2 3 4 5 6"] ToExpression@StringSplit["1 2 3 4 5 6"] StringCases["1 2 3 4 5 6", ns : NumberString :> FromDigits[ns]] ToExpression@StringCases["1 2 3 4 5 6", NumberString] all give (* {1, 2, 3, 4, 5, 6} *)


2

res = ToExpression@StringSplit["1 2 3 4 5 6", " "] {1, 2, 3, 4, 5, 6} Head /@ res {Integer, Integer, Integer, Integer, Integer, Integer}


2

Flatten[ImportString["1 2 3 4 5 6", "Table"]] {1,2,3,4,5,6} Head /@ % {Integer, Integer, Integer, Integer, Integer, Integer}


2

You can also use a combination of StringSplit, SyntaxQ and Pick: str = "{Aaaa -> a, Bbbb- > b, Cccc -> , Ddddd -> c, Eeeee -> , Fffff -> e}"; str2 = Pick[#, SyntaxQ /@ #] &@StringSplit[str, "," | "{" | "}"] (* {"Aaaa -> a", " Ddddd -> c", " Fffff -> e"} *) ToExpression@str2 (* {Aaaa -> a, Ddddd -> c, Fffff -> e} *) ...


2

You can use StringCases. str = "{Aaaa -> a, Bbbb -> b, Cccc -> , Ddddd -> c, Eeeee -> , Fffff -> e}"; ToExpression@StringCases[str, WordCharacter .. ~~ " -> " ~~ WordCharacter ..] {Aaaa -> a, Bbbb -> b, Ddddd -> c, Fffff -> e}



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