Tag Info

Hot answers tagged

9

The behaviour we see here is due to the semantics of the regular expression pattern-matching process. Notionally, the string is scanned from left to right. At each character position, an attempt is made to match the string by looking ahead from that position. If there is no match, scanning moves onto the next character. If there is a match, then (by ...


9

Sublists These can be generated very concisely with ReplaceList and an appropriate pattern. For lists: sublists1[list_List] := ReplaceList[list, {___, sub___, ___}:>{sub}] list = {1, 2, 3}; sublists1[list] (* {{}, {1}, {1, 2}, {1, 2, 3}, {}, {2}, {2, 3}, {}, {3}, {}} *) To omit empty sublists, simply replace sub___ by sub__. An alternative, is to ...


4

This may be what you want. Be warned that it will include non-printable (control) characters. obfuscate[n_Integer] := FromCharacterCode @ IntegerDigits[n, 256] obfuscate[26728] "hh" Compared to your result: ConvertNumber[469785602403] obfuscate[469785602403] {"m", "a", "g", "i", "c"} "magic"


4

For strings, there is also : string = "abcd"; StringCases[string, _ ~~ LetterCharacter ..., Overlaps -> All] or rather (as @Kuba greatly suggested) StringCases[string, __, Overlaps -> All] {abcd, abc, ab, a, bcd, bc, b, cd, c, d} (5 "" are missing but that's not the point here) It seems almost as fast as substrings1 according to my tests. ...


4

f[l_List] := Take[l, #] & /@ Subsets[Range[Length[l]], {1, 2}]; f[s_String] := StringJoin @@@ f[Characters[s]] string = "abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcab" <> "cabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabca"; a = f[string]; // Timing { 0.015600, Null} Timing[b = substrings1[string];] {0.015600, Null} ...


3

StringCases[test1,"|uniprotkb:" ~~ aa : (WordCharacter ..) ~~ "(gene name)" -> aa] (* {{"Dvl1"}, {"Nxn"}, {"Lrp8"}, {"Reln"}} *)


3

I may have misunderstood the requirement but this seems to work for the example at least: problem = "a { b { c d } e } aa { bb cc }"; Map[Flatten, StringSplit[problem] //. {head___, x_, "{", Shortest[y__], "}", tail___} :> {head, Sequence @@ Thread[{x, {y}}], tail}] (* {{"a", "b", "c"}, {"a", "b", "d"}, {"a", "e"}, {"aa", "bb"}, {"aa", "cc"}} *)


1

MMA, V9: StringCases[test1, __ ~~ "uniprotkb:" ~~ x__ ~~ "(gene name)|" :> x] (*{{"Dvl1"}, {"Nxn"}, {"Lrp8"}, {"Reln"}}*)


1

If you are strictly looking for the shortest string between "|uniprotkb:" and "(gene name)" in each record, this is a way : StringCases[test1, "|uniprotkb:" ~~ x__ ~~ "(gene name)" :> x, Overlaps -> True] // (First@SortBy[#, StringLength] &) /@ # & {Dvl1,Nxn,Lrp8,Reln}


1

The code provided in the Question seems to be selecting the first instance rather than the Shortest, as can be seen by moving |uniprotkb:Lrp8(gene name) earlier in test1[[3]]. However, the following seems to work well. StringCases[test1, "|uniprotkb:" ~~ aa__ ~~ "(gene name)" /; (StringLength[aa] <= 8) :> aa] (* {{"Dvl1"}, {"Nxn"}, {"Lrp8"}, ...


1

Obtaining the box form from FullForm[ToBoxes["t1"]] (that's t subscript 1) - the box form can be used in a function like so:- make[x_, n_] := ToExpression[ StringJoin["\"\\!\\(\\*SubscriptBox[\\(", ToString[x], "\\), \\(", ToString[n], "\\)]\\)\""]] make[t, 1] make[t, 1] == "t1" (* again, t subscript 1 *) True To convert a subscripted ...



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