Tag Info

Hot answers tagged

10

StringTrim["ahfiehfke$jfwfjejf0", "$" ~~ __]; StringDelete["ahfiehfke$jfwfjejf0", "$" ~~ __]; (*V10.1*)


6

Table[k1 + k2, Evaluate@{ToExpression[list[[1]]], 1, 2}, Evaluate@{ToExpression[list[[2]]], 1, 2}] (* {{2, 3}, {3, 4}} *) Or Table[k1 + k2, {#, 1, 2}, {#2, 1, 2}] & @@ ToExpression[list] (* {{2, 3}, {3, 4}} *)


5

You could measure the similarity between two strings. One simple approach is to convert all strings into the bag-of-words model and then compare the resulting vectors. This could work well if the strings contain the same words, but not in the same order. Nearest[vectors, x, DistanceFunction -> CosineDistance] Will give you a measure of how close ...


5

You should take a look at: New in the Wolfram Language: GrammarRules Programmable Linguistic Interface Sequence Alignment & Comparison You can also use Machine Learning to help classify strings. Here is how to train a classifier to understand if a string talks about a cat a or a dog. cat = ToLowerCase[Import["http://en.wikipedia.org/wiki/Cat"]]; ...


5

Just another way, with Templates so only for V10+: The following can be done without FileNameJoin but it is a good habit to use it: file[i] = 1; TemplateApply @ FileNameJoin[{$HomeDirectory, "Desktop", "<*file[i]*>.doc"}] ...Desktop\1.doc "<**>" is TemplateExpression


4

Here's a simple fix/workaround. It appears that all you need to do to make your code work as intended is to make the following change to your $dateFormat assignment: $dateFormat = {"Year", "", "Month", "", "Day", "", "Hour"} This generates an identical string to your test example, but it appears that the empty strings are necessary to correctly parse the ...


4

In the example you give, your Query function asks if the value at element "a" is a string match for "hey". This generates the message because the value for a is not a string in the second row of your dataset. For this particular case, one solution would be to require an exact match: data = Dataset[{<|"a" -> "hey"|>, <|"a" -> ...


4

Something like this words = DictionaryLookup[s__ /; StringFreeQ[s, "ar" | "h"]]; Length[words] 70781 sorted = SortBy[words, StringLength]; Short[sorted] {"a", "I", "AC", "ad", "AD", <<70772>>, "Andrianampoinimerina", "buckminsterfullerene", "institutionalization", "internationalization"}


3

Have a look at the documentation for CSV. The first issue you have is that your file extension is .txt so Mma imports it as text file instead of a CSV file. Your second issue is that "Table" is not a supported element for either CSV or TXT so I think it is just being ignored. Even though your file does not have the .csv file type you can still tell Mma ...


3

I'm sure this is a duplicate but to give you a quick answer here's a community wiki post: Use ToString and StringJoin to form your argument: file[i] = 1; "/Users/Home/Desktop/" <> ToString @ file[i] <> ".doc" "/Users/Home/Desktop/1.doc" Also look at FileNameJoin.


2

At the risk of betraying how long I've been using Mathematica, the method I'm accustomed to uses StringForm[] (in conjunction with ToString[] since the output of that function, nonintuitively, is not a string): file[i] = 1; OpenWrite[ToString[StringForm["/Users/Home/Desktop/`1`.doc", file[i]]]]


1

I think this matches what you want it to: str11 = "<strong style=\"font-size:20px\" style=\"color:#dfa57c\" style=\"font-size:20px\" style=\"color:#dfa57c\" class=\"new\">"; StringCases[str11, ("style=\"" ~~ Except["\""] .. ~~ "\"" ~~ Whitespace | "") ..] {"style=\"font-size:20px\" style=\"color:#dfa57c\" style=\"font-size:20px\" ...


1

Another method: StringTake[str, StringPosition[str, "$", 1][[1, 1]]] A comparison of performance of different methods which give identical results: Needs["GeneralUtilities`"] str = StringJoin@{Table["a", {1000}], Table["ahfieh\nfke$jfw\nfje$jf0", {1000}]}; Replace[Hold[{ StringTrim[str, "$" ~~ __] <> "$", StringDelete[str, "$" ~~ __] ...


1

A bunch of regex methods: str = "ahfiehfke$jfwfjejf0"; (* Coolwater *) StringCases[str, RegularExpression["(?s).+?\\$", 1]][[1]] StringReplace[str, RegularExpression["(?s)\\$.*"] :> "$", 1] These use look-behind regexes: StringReplace[str, RegularExpression["(?s)(?<=\\$).+"] :> "", 1] StringTrim[str, RegularExpression["(?<=\\$).+"]] ...


1

Here is another alternative, in which the values of the variables are temporarily cleared using Block and an injector pattern. ClearAll[getSymbolNames]; SetAttributes[getSymbolNames, HoldAll]; getSymbolNames[list_Symbol] := Hold[list] /. OwnValues[list] /. Hold[{vars__Symbol}] :> Block[{vars}, SymbolName /@ {vars}] With some planning one might ...


1

Update: I haven't written any C in a long while, so I forgot its syntax for multidimensional arrays, i.e. array[first][second]! Here is the amended code: ToString[CForm[t[12, 156] + ht u[5, 3]]] StringReplace[%, { "t(" ~~ Shortest[a__] ~~ "," ~~ Shortest[b__] ~~ ")" -> "t[" ~~ a ~~ "][" ~~ b ~~ "]", "u(" ~~ Shortest[a__] ~~ "," ~~ Shortest[b__] ~~ ...


1

Doing this is pretty straightforward; you'll want to take a look at the documentation for Mathematica's string patterns primarily. As an example, to use DictionaryLookup with the "ar" and "h" instance you mention, the following are basically equivalent: matches1 = DictionaryLookup[s__ /; !StringContainsQ[s, "ar"|"h"]]; matches2 = ...



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