Tag Info

Hot answers tagged

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

For me (that is very personal indeed), StringExpressions in Mathematica are much more transparant than regular expressions. Here are two StringExpressions for your strings: p1 = NumberString ~~ ".2" ~~ DigitCharacter ~~ DigitCharacter ~~ "nc"; p2 = NumberString ~~"." ~~ (x : NumberString /; 200 <= ToExpression[x] < 300) ~~ "nc"; teststrings = ...


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


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

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

Replace \. with \\., and wrap your regular expression with quotes "..." and RegularExpression: regex = RegularExpression["[0-9]+\\.2[0-9]{2}nc"] strings = {"1001.200nc", "12345.220nc", "987654.215nc", "nomatch"}; StringMatchQ[strings, regex] (* {True, True, True, False} *) StringCases[strings, regex] (* {{"1001.200nc"}, {"12345.220nc"}, {"987654.215nc"}, ...


3

This returns "11": StringReplace["item_10", "item_" ~~ x__ :> ToString[ToExpression[x] + 1] ] StringReplace["item_10", "item_" ~~ x__ :> ToString[ToExpression[x <> "+1"]] ] This 11: StringCases["item_10", "item_" ~~ x__ :> ToExpression[x] + 1] StringCases["item_10", "item_" ~~ x__ :> ToExpression[x <> "+1"]] And step by step, ...


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


3

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}


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" ~~ __] &]


1

Manipulate version. Manipulate[ Row@string , Column[{Dynamic[pop /@ Range[n] // Row, TrackedSymbols :> {n}], SetterBar[ Dynamic[x, If[# === "+", n++; string = Join[string, {"a"}], n--; string = Most@string] &], {"+", "-"}]}] , {x, None}, {n, None}, {string, None} , Initialization :> ( pop[i_] := With[{j = ...


1

i have a solution that uses the UndocumentedTestFEParserPacket as described by John Fultz! in this question. This funny named Tool parses a String Input into the real unchanged Mathematica BoxForm. This is my code: str0 = Sum::usage str1=StringJoin[ToString[StringReplace[#, "\\\"" -> "\""]]& /@ (Riffle[MathLink`CallFrontEnd[ ...


1

StringReplace just searched from left to right and replaced all the zero-length string for once. Here is a way to visualize the process: i = 0; StringReplace[{"90", "", "20"}, "" :> ToString[Style["0", {Orange, 15 + 5 i++}], StandardForm]] The inserted zero is dyed into orange and becomes bigger every time the replacing rule is applied so you can ...


1

If you increase the `GapPenalty" (default is zero) you can pick up the 3; however, the difference sequences get longer and the total similarity score decreases. SequenceAlignment["-0.999995756767227832383453368554576431549", \ "-0.999995756767227832383346049698547342279", GapPenalty -> 3] {"-0.999995756767227832383", {"453368554", "346049698"}, "5", ...


1

Mathematica took the alignments in the following way -0.99999575676722783238x34 -0.99999575676722783238334


1

Given you already have the filename as a text string you could specify the file name and path separately when you first run, and create the full path & filename using FileNameJoin. FileNameJoin is OS agnostic so puts in forward or backslashes depending on whether you are running Windows, OSX or Linux. Alternatively if you want something a bit more ...


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


1

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


1

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



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