# Select parts of Input Cell

One of my cells contains some Text and Input:

some text 3+5,


so the expression underlying this looks like

Cell[  TextData[{
"some text ",
StyleBox["3+5", "Output"]
}],   "Text"]


I now wish to select only the "3+5", using commands, as to be able to evaluate that part in place, as if one uses the command "Evaluate in Place" of the Evaluation menu (and using the notebook end to manually select 3+5).

I've tried using Cases[ ... , StyleBox[__]], but this gives an empty set. What am I missing here?

Once again, thanks for all help!

-

## 1 Answer

If I understand correctly you want to replace all output-style boxes with evaluated values. You can do this by

Module[{nb = EvaluationNotebook[], sel = Null},
SelectionMove[nb, Before, Notebook, AutoScroll -> False];
While[sel =!= {},
SelectionMove[nb, Next, Cell, AutoScroll -> False];
sel = NotebookRead[nb];
If[MatchQ[sel, Cell[_, "Text", ___]],
NotebookWrite[nb, sel /. StyleBox[expr_, "Output"] :>
StyleBox[ToString[ToExpression[expr], InputForm], "Output"]
]]]]

-
Thank you for your answer. It is indeed what I was looking for, but I wonder: why aren't you using SelectionEvaluate[]? Thank you for your answer! – Gabriel Oct 20 '13 at 11:50
Second question: Why the need for ToString? Furthermore, this doesn't seem to work when using exponents in symbolic forms... Any help with that? – Gabriel Oct 21 '13 at 8:35
@Gabriel I don't use SelectionEvaluate because I select whole cell and I want to evaluate only output-style boxes in it. Without ToString examples like a+b don't works. I edit my post to correct the problem with exponents. – ybeltukov Oct 21 '13 at 10:27
Thank you for the change! – Gabriel Oct 21 '13 at 14:54