New answers tagged

1

Here's an equivalent to kglr's solution, using replacement and regex: {{"a (b) 1", "c 2"}, {"d 3", "e f 4"}} /. s_String :> StringCases[s, RegularExpression["(.+) (\\d)$"] :> Sequence["$1", "$2"]] {{{"a (b)", "1"}, {"c", "2"}}, {{"d", "3"}, {"e f", "4"}}}


7

StringSplit[#, WhitespaceCharacter... ~~ n : NumberString :> n] & /@ lis {{{"a (b)", "1"}, {"c", "2"}}, {{"d", "3"}, {"e f", "4"}}}


1

Update: MapIndexed[# -> #2[[1]] &, Characters[sentence]] {"h" -> 1, "e" -> 2, "l" -> 3, "l" -> 4, "o" -> 5, " " -> 6, "w" -> 7, "o" -> 8, "r" -> 9, "l" -> 10, "d" -> 11, "!" -> 12} To get the numbers only the simplest way is what @MassDefect suggested in a comment above: Range @ StringLength @ sentence. Original answer: You can use ...


1

StringCases[s, "<span>" ~~ Shortest[x___] ~~ "</span>" -> x] {"Data I want to extract", "More data I want to extract", "Even more data I want to extract"} Shortest >> Details: If no explicit Shortest or Longest is given, ordinary expression patterns are normally effectively assumed to be Shortest[p], while string patterns are ...


2

rlis = Reverse[lis]; xpos = First[Flatten[Position[StringMatchQ[rlis, "X"], True]]]; Reverse[Drop[rlis, {xpos, xpos + LengthWhile[rlis[[xpos + 1 ;;]], StringMatchQ[#, NumberString] == False &]}]] {"a", "b", "c", "12", "s"}


4

Replace[lis, {a___, b_String?(StringMatchQ[NumberString]), Shortest[c___], "X", d___} :> {a, b, d}] {"a", "b", "c", "12", "s"}


4

You would need something like DeleteSubsequenceCases, but it doesn't exist. I would recommend this instead: SequenceReplace[lis, {d_?(StringMatchQ[NumberString]), ___, "X"} :> d] If X only appears once, you could also use this: First@SequenceCases[lis, {a___, d_?(StringMatchQ[NumberString]), ___, "X", b___} :> {a, "12", b}]


2

Here is a fast approach using Split: lis = {"abc", "(def)", "ghi", "jkl"}; StringJoin /@ Split[lis, StringStartsQ[#2, "("] &] (* {"abc(def)", "ghi", "jkl"} *) lis = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "a", "b", "c", "d", "(ef)", "g", "m"}; StringJoin /@ ...


2

Try ToExpression[Select[Characters[">DAVID123<" ],StringMatchQ[#,LetterCharacter]&]] which will give you a vector of {D,A,V,I,D} as symbols or variables, but C and D and E and I and K have predefined meaning inside of Mathematica and may cause you grief.


1

Use ToExpression with its three arguments form and construct the Set programmatically: varname="test"; value=RandomReal[]; Set @@ Append[ToExpression[varname, InputForm, Hold], value] Can you explain why you want to do this? I see many potential problems when you allow users of your gui to do this, they could e.g. accidentially overwrite any of ...


1

The idea is using StringStartsQ with needed symbol, "(" here, applyed to pairs of adjasent elements: If[StringStartsQ[#2,"("], StringJoin[#1,#2],Sequence@@{#1,#2}]&@@@Partition[lis, 2] EDIT Fail was because I tested my answer with initial example only: second element of pair contains "(". Next is more general answer: any element of pair can contain "("...


2

lis = {"abc", "(def)", "ghi", "jkl"}; SequenceReplace[lis, {a_, strs__?(StringStartsQ@"(")} :> StringJoin@{a, strs}] (* {"abc(def)", "ghi", "jkl"} *) lis = {"abc", "(def)", "ghi", "(jkl"}; SequenceReplace[lis, {a_, strs__?(StringStartsQ@"(")} :> StringJoin@{a, strs}] (* {"abc(def)", "ghi(jkl"} *)


2

Less elegant but fast solution: lis // {#, StringContainsQ[#, CharacterRange["a", "z"]]} & // Transpose // SplitBy[#, Last] & // Map[ If[ Last@First@#, StringJoin@(#[[All, 1]]), Sequence @@ (#[[All, 1]]) ] &]


4

[Edit: Just noticed that @kglr was slightly faster in posting a very similar solution - I'll leave this here since it is at least slightly different, in that it merges arbitrarily many consecutive strings, while @kglr's solution only merges pairs] You can use SequenceReplace: SequenceReplace[ lis, {strs__?(StringContainsQ@CharacterRange["a", "z"])} :&...


5

You can useSequenceReplace: SequenceReplace[lis, {a__} /; And @@ (StringContainsQ[{a}, Alternatives @@ CharacterRange["a", "z"]]) :> StringJoin[a]] % == res True Faster alternatives: SequenceReplace[lis, {a__}/; Nor @@ StringFreeQ[_?LowerCaseQ] @ {a}:> StringJoin[a]] and StringJoin /@ Split[lis, Nor @@ StringFreeQ[_?LowerCaseQ] @ {##}&...


3

@Kuba's approach works using up-values, which works very nicely for simple cases as the examples given in the question. However, it does not work if the want["..."] expression is nested deeper inside a held expression, as noted in the comments. Here is an approach using MakeExpression that processes any expressions of the form want["..."] before any ...


2

Want // ClearAll; x = 111; Want /: SetDelayed[lhs_, Want[s_String]] := ToExpression[ s, InputForm, Function[rhs, SetDelayed @@ Hold[lhs, rhs], HoldAll] ] H[x_, y_] := Want["x^2+y^2"] H[3, 4] 25


1

Instead of using N[] + string trickery, it is better to just use RealDigits[] directly. Using J42161217's two examples: With[{n = 1*^4}, Select[StringJoin /@ Partition[FromCharacterCode[Mod[FromDigits[#], 26] + 65] & /@ Partition[Rest[First[RealDigits[Pi, 10, n + 1]]], 2], 5], DictionaryWordQ]] {"GRAVE"} With[{n = 1*^5}, ...


4

A few additional variations: Pick[lis, StringMatchQ["a*"]@lis[[All, 1]]] {{"ab", 2, "c", 3}, {"ac", 5, "f", 6}, {"ad", 9, "c", 10}} Pick[lis, StringTake[lis[[All, 1]], 1], "a"] {{"ab", 2, "c", 3}, {"ac", 5, "f", 6}, {"ad", 9, "c", 10}} Pick[lis, Order[#, "b"]& /@ lis[[All, 1]], 1] {{"ab", 2, "c", 3}, {"ac", 5, "f", 6}, {"ad", 9, "c", 10}}


4

Alternate solutions using string patterns: Pick[lis, StringMatchQ[lis[[All, 1]], "a" ~~ ___]] {{"ab", 2, "c", 3}, {"ac", 5, "f", 6}, {"ad", 9, "c", 10}} Pick[lis, StringCases[lis[[All, 1]], "a" ~~ ___] // Map[Length], 1] {{"ab", 2, "c", 3}, {"ac", 5, "f", 6}, {"ad", 9, "c", 10}} Pick[lis, StringPosition[lis[[All, 1]], "a" ~~ ___] // Map[MatrixQ]] ...


9

Some options: Pick[ lis, StringStartsQ[lis[[All, 1]], "a"] ] {{"ab", 2, "c", 3}, {"ac", 5, "f", 6}, {"ad", 9, "c", 10}} Select[lis, StringStartsQ[First[#], "a"] &] {{"ab", 2, "c", 3}, {"ac", 5, "f", 6}, {"ad", 9, "c", 10}} Cases[lis, {_?(StringStartsQ["a"]), ___}] {{"ab", 2, "c", 3}, {"ac", 5, "f", 6}, {"ad", 9, "c", 10}}


5

ToExpression is very fast for correct Mathematica syntax input. So the key idea is to create an input string for ToExpression that delivers the expected result for the huge string database in one go: StringData // Extract[{All, 2}] // StringRiffle[#, {"{{", "Nothing},{", "Nothing}}"}] & // ToExpression The odd looking "Nothing}" in ...


4

strtolist2 = Map[FromDigits, StringSplit[StringData[[All, 2]], ","], {-1}] strtolist3 = IntegerPart @ Map[Internal`StringToDouble, StringSplit[StringData[[All, 2]], ","], {-1}]; strtolist3 == strtolist2 == strtolist True Both are about twice as fast as For loop with IntegerPart/@Internal`StringToDouble/@...


2

I think for what you want to achieve the current recommended way would be to use the templates functionality. The closest to your example code would be something like the following: parameters = <|"Y" -> 2*^11|>; inputstr = StringTemplate["/PREP7 /RGB,INDEX,100,100,100, 0 /RGB,INDEX, 80, 80, 80,13 /RGB,INDEX, 60, 60, 60,14 /RGB,INDEX, ...


1

If I understand your question correctly, the following code str = "2+2 \"Hello world\" 3+3"; ToExpression[str, InputForm, Print /@ List[##] &]; does what you want. Alternatively, you can use ToExpression[str, InputForm, List] and decide what to do with the list later. The documentation for ToExpression states: ToExpression[input, form, h] ...


Top 50 recent answers are included