Hot answers tagged

11

Simple greedy matching can be done as follows: construct a string pattern with longest words first and find words: StringCases["tableapplechairtablecupboard", Alternatives @@ SortBy[DictionaryLookup[], Minus@*StringLength]] (* {"table", "apple", "chair", "table", "cupboard"} *) Intuitive assumption is that one might use Longest to find longest match ...


11

This works: MapThread[If[EvenQ[#2], ToUpperCase[#1], #1] &, {Characters @ "05eccead24", IntegerDigits @ FromDigits @ "1234567456"}] // StringJoin (* "05eCcEaD24" *)


7

Edit: I first used StringReplacePart but that function is slow when used for many replacements, much as MapAt is. I have rewritten my code using a method from Map a function across a list conditionally and it is now orders of magnitude faster on long strings. f[source_String, target_String] := Module[{new = Characters @ target}, (new[[#]] = ...


6

On the second line, the string that looks like "in" is in fact a three-character string. The third character is a soft-hyphen: it is invisible and it is used to mark a position where a word can be hyphenated. It has character code 173. StringReplace finds no match due to this invisible third character. You can detect this using FullForm or ...


5

list = {"ingenious", "forgotten", "tree", "supercomput", "hundred"}; list /. word_String :> Join[{word}, Characters@word] // Grid[#, Dividers -> All] &


4

This looks like a job for associations: input = "article{a2,author={F1name L1name,F2name M2name,L2name}, title={The title of the work}, journal={The Journal2},year=1994, number=3,pages={204-223},month=6, note={An optional note},volume=2}" type = StringCases[input, Shortest[StartOfString ~~ t : __ ~~ "{"] :> t][[1]] "article" key = ...


4

Just for fun. Here I will use C and Wolfram LibraryLink wrapper to deal with your problem. src = " #include <ctype.h> #include <string.h> #include \"WolframLibrary.h\" void string_change(char *str1, char *str2) { int i, len; len = strlen(str1); for (i = 0; i < len; i++) { if (isdigit(str2[i]) && ...


3

Note that you have a "." in your original expression, "a dog jumped". instead of , and also I do not know why sat is not there, so I made one up for it. But any way, is this what you wanted? list = {{1, "the dog jumped", "Tues : Wed : Sat"}, {2, "the fox ran", "Mon : Tues : Fri"}, {3, "a dog jumped", "Wed : Mon : Tues"}}; rep = {":" -> "+", "Mon" -&...


3

I suspect that your test doesn't work because, according to the docs for PatternTest, "In a form such as __?test, every element in the sequence matched by __ must yield True when test is applied." Instead, a conditional pattern using /; will work as I think you intended with your definition of the words wordlist: StringCases[ToLowerCase@string, word__ /; ...


3

When working with text, you must be aware of character encodings. Today it is best practice to always use UTF-8, which is an international standard and supported by all modern software, all over the world. Be aware of what encoding your text editor uses, and try to set it to UTF-8. What happened here is that you copied the text from the website, and ...


2

I found the problem is with my original data; there are some unexpected Line Feed characters in my data which were causing me to misinterpret the result of "Import" using type CSV. The comments by Yode and JasonB prompted me to look at the file with notepad++ so I could see the LF characters. Sorry for the confusion.


2

As the promp of Sascha's answer,I simplify this code be list={"ingenuous","forgotten","tree","supercomput","hundred"}; Grid[Join[List/@list,Characters/@list,2],Frame->All,Spacings->{2.6,0.5}]


1

Should be quick for large strings if you want to keep it native MMA: upem[s_, t_] := Module[{ss = Pick[Range@StringLength@s, EvenQ@ToCharacterCode[s]], tc = ToCharacterCode@t}, tc[[Intersection[ss, Pick[Range@StringLength@t, Unitize@Clip[tc, {97, 122}, {0, 0}],1]]]] -= 32; FromCharacterCode@tc]; E.g. upem["1234567456", "05eccead24"]


1

Your code contains numerous syntax errors. The first formula has no sense in Mma. I tried to rewrite its part that seems reasonable: expr = 10* Log[10, Re[(1/Subscript[R, p] + 1/(2 \[Nu]*\[Pi]*L*I) + 2 \[Pi]*\[Nu]*Subscript[C, p]*I)^-1]] + Subscript[P, 0]; Then you may do the following: TraditionalForm[expr] There are also multiple other ways ...



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