3 replaced http://stackoverflow.com/ with https://stackoverflow.com/
source | link

The following functions will load the expressions and erroneous cells from a notebook:

notebookExpressions[path_, pattern_:_] :=
  Cases[Import[path, "Notebook"] // First
  , c:Cell[_, "Input"|"Output"|"Print", ___] :>
      Module[{v = eval[c]}, v /; MatchQ[v, _$Failed | Hold[pattern]]]
  , Infinity
  ]

eval[cell_] :=
  Quiet @ Check[
    ToExpression[First@cell, StandardForm, Hold]
  , $Failed @ cell
  ]

notebookExpressions is the main function. First, it imports the cell expressions from a file. Then it filters out all but the Input, Output and Print cells. For each such cell, it attempts to convert the cell's contents to an expression. If it succeeds, the expression is wrapped in Hold. If it fails, then the original cell expression is wrapped in $Failed. The function eval performs the actual conversion.

To illustrate the use of this function, let's use the following notebook as input:

notebook screenshot

We can load and view the expressions as follows:

$file = "c:\\some\\path\\to\\se-notebook-expressions-test.nb";

$expressions = notebookExpressions[$file];

Column[$expressions, Frame -> All]

notebook expression list

We can see that the expressions are held, and that the cell with an invalid expression is wrapped in $Failed. The text cell was not read, but the input, output and printed cells were.

The valid expressions are readily singled out based upon their Hold wrapper:

Cases[$expressions, _Hold]

(* { Hold[zot[x_]:=x+1], Hold[zot[3]], Hold[4], Hold[Print[{this,is,a,printed,cell}]],
     Hold[{this,is,a,printed,cell}] } *)

We can also print the failed expressions for diagnostic purposes:

Scan[CellPrint @@ # &, Cases[$expressions, _$Failed]]

invalid cell screenshot

By default, notebookExpressions does not filter out any expressions based upon their content. If a second pattern argument is passed, expressions will only be shown if they match that pattern. Invalid cells are always returned since the pattern-matching is inconclusive. Here, we match all cells containing a SetDelayed expression (:=) or errors:

Column[notebookExpressions[$file, _SetDelayed], Frame -> All]

expression list screenshot

Observe that the specified pattern does not specify the Hold wrapper.

Beware that ToExpression has historically sometimes had trouble interpreting box expressionstrouble interpreting box expressions for unusual forms. The "real" parser appears to be unavailable to user code.

The following functions will load the expressions and erroneous cells from a notebook:

notebookExpressions[path_, pattern_:_] :=
  Cases[Import[path, "Notebook"] // First
  , c:Cell[_, "Input"|"Output"|"Print", ___] :>
      Module[{v = eval[c]}, v /; MatchQ[v, _$Failed | Hold[pattern]]]
  , Infinity
  ]

eval[cell_] :=
  Quiet @ Check[
    ToExpression[First@cell, StandardForm, Hold]
  , $Failed @ cell
  ]

notebookExpressions is the main function. First, it imports the cell expressions from a file. Then it filters out all but the Input, Output and Print cells. For each such cell, it attempts to convert the cell's contents to an expression. If it succeeds, the expression is wrapped in Hold. If it fails, then the original cell expression is wrapped in $Failed. The function eval performs the actual conversion.

To illustrate the use of this function, let's use the following notebook as input:

notebook screenshot

We can load and view the expressions as follows:

$file = "c:\\some\\path\\to\\se-notebook-expressions-test.nb";

$expressions = notebookExpressions[$file];

Column[$expressions, Frame -> All]

notebook expression list

We can see that the expressions are held, and that the cell with an invalid expression is wrapped in $Failed. The text cell was not read, but the input, output and printed cells were.

The valid expressions are readily singled out based upon their Hold wrapper:

Cases[$expressions, _Hold]

(* { Hold[zot[x_]:=x+1], Hold[zot[3]], Hold[4], Hold[Print[{this,is,a,printed,cell}]],
     Hold[{this,is,a,printed,cell}] } *)

We can also print the failed expressions for diagnostic purposes:

Scan[CellPrint @@ # &, Cases[$expressions, _$Failed]]

invalid cell screenshot

By default, notebookExpressions does not filter out any expressions based upon their content. If a second pattern argument is passed, expressions will only be shown if they match that pattern. Invalid cells are always returned since the pattern-matching is inconclusive. Here, we match all cells containing a SetDelayed expression (:=) or errors:

Column[notebookExpressions[$file, _SetDelayed], Frame -> All]

expression list screenshot

Observe that the specified pattern does not specify the Hold wrapper.

Beware that ToExpression has historically sometimes had trouble interpreting box expressions for unusual forms. The "real" parser appears to be unavailable to user code.

The following functions will load the expressions and erroneous cells from a notebook:

notebookExpressions[path_, pattern_:_] :=
  Cases[Import[path, "Notebook"] // First
  , c:Cell[_, "Input"|"Output"|"Print", ___] :>
      Module[{v = eval[c]}, v /; MatchQ[v, _$Failed | Hold[pattern]]]
  , Infinity
  ]

eval[cell_] :=
  Quiet @ Check[
    ToExpression[First@cell, StandardForm, Hold]
  , $Failed @ cell
  ]

notebookExpressions is the main function. First, it imports the cell expressions from a file. Then it filters out all but the Input, Output and Print cells. For each such cell, it attempts to convert the cell's contents to an expression. If it succeeds, the expression is wrapped in Hold. If it fails, then the original cell expression is wrapped in $Failed. The function eval performs the actual conversion.

To illustrate the use of this function, let's use the following notebook as input:

notebook screenshot

We can load and view the expressions as follows:

$file = "c:\\some\\path\\to\\se-notebook-expressions-test.nb";

$expressions = notebookExpressions[$file];

Column[$expressions, Frame -> All]

notebook expression list

We can see that the expressions are held, and that the cell with an invalid expression is wrapped in $Failed. The text cell was not read, but the input, output and printed cells were.

The valid expressions are readily singled out based upon their Hold wrapper:

Cases[$expressions, _Hold]

(* { Hold[zot[x_]:=x+1], Hold[zot[3]], Hold[4], Hold[Print[{this,is,a,printed,cell}]],
     Hold[{this,is,a,printed,cell}] } *)

We can also print the failed expressions for diagnostic purposes:

Scan[CellPrint @@ # &, Cases[$expressions, _$Failed]]

invalid cell screenshot

By default, notebookExpressions does not filter out any expressions based upon their content. If a second pattern argument is passed, expressions will only be shown if they match that pattern. Invalid cells are always returned since the pattern-matching is inconclusive. Here, we match all cells containing a SetDelayed expression (:=) or errors:

Column[notebookExpressions[$file, _SetDelayed], Frame -> All]

expression list screenshot

Observe that the specified pattern does not specify the Hold wrapper.

Beware that ToExpression has historically sometimes had trouble interpreting box expressions for unusual forms. The "real" parser appears to be unavailable to user code.

2 moved the caution about `ToExpression` from a comment to the main body
source | link

The following functions will load the expressions and erroneous cells from a notebook:

notebookExpressions[path_, pattern_:_] :=
  Cases[Import[path, "Notebook"] // First
  , c:Cell[_, "Input"|"Output"|"Print", ___] :>
      Module[{v = eval[c]}, v /; MatchQ[v, _$Failed | Hold[pattern]]]
  , Infinity
  ]

eval[cell_] :=
  Quiet @ Check[
    ToExpression[First@cell, StandardForm, Hold]
  , $Failed @ cell
  ]

notebookExpressions is the main function. First, it imports the cell expressions from a file. Then it filters out all but the Input, Output and Print cells. For each such cell, it attempts to convert the cell's contents to an expression. If it succeeds, the expression is wrapped in Hold. If it fails, then the original cell expression is wrapped in $Failed. The function eval performs the actual conversion.

To illustrate the use of this function, let's use the following notebook as input:

notebook screenshot

We can load and view the expressions as follows:

$file = "c:\\some\\path\\to\\se-notebook-expressions-test.nb";

$expressions = notebookExpressions[$file];

Column[$expressions, Frame -> All]

notebook expression list

We can see that the expressions are held, and that the cell with an invalid expression is wrapped in $Failed. The text cell was not read, but the input, output and printed cells were.

The valid expressions are readily singled out based upon their Hold wrapper:

Cases[$expressions, _Hold]

(* { Hold[zot[x_]:=x+1], Hold[zot[3]], Hold[4], Hold[Print[{this,is,a,printed,cell}]],
     Hold[{this,is,a,printed,cell}] } *)

We can also print the failed expressions for diagnostic purposes:

Scan[CellPrint @@ # &, Cases[$expressions, _$Failed]]

invalid cell screenshot

By default, notebookExpressions does not filter out any expressions based upon their content. If a second pattern argument is passed, expressions will only be shown if they match that pattern. Invalid cells are always returned since the pattern-matching is inconclusive. Here, we match all cells containing a SetDelayed expression (:=) or errors:

Column[notebookExpressions[$file, _SetDelayed], Frame -> All]

expression list screenshot

Observe that the specified pattern does not specify the Hold wrapper.

Beware that ToExpression has historically sometimes had trouble interpreting box expressions for unusual forms. The "real" parser appears to be unavailable to user code.

The following functions will load the expressions and erroneous cells from a notebook:

notebookExpressions[path_, pattern_:_] :=
  Cases[Import[path, "Notebook"] // First
  , c:Cell[_, "Input"|"Output"|"Print", ___] :>
      Module[{v = eval[c]}, v /; MatchQ[v, _$Failed | Hold[pattern]]]
  , Infinity
  ]

eval[cell_] :=
  Quiet @ Check[
    ToExpression[First@cell, StandardForm, Hold]
  , $Failed @ cell
  ]

notebookExpressions is the main function. First, it imports the cell expressions from a file. Then it filters out all but the Input, Output and Print cells. For each such cell, it attempts to convert the cell's contents to an expression. If it succeeds, the expression is wrapped in Hold. If it fails, then the original cell expression is wrapped in $Failed. The function eval performs the actual conversion.

To illustrate the use of this function, let's use the following notebook as input:

notebook screenshot

We can load and view the expressions as follows:

$file = "c:\\some\\path\\to\\se-notebook-expressions-test.nb";

$expressions = notebookExpressions[$file];

Column[$expressions, Frame -> All]

notebook expression list

We can see that the expressions are held, and that the cell with an invalid expression is wrapped in $Failed. The text cell was not read, but the input, output and printed cells were.

The valid expressions are readily singled out based upon their Hold wrapper:

Cases[$expressions, _Hold]

(* { Hold[zot[x_]:=x+1], Hold[zot[3]], Hold[4], Hold[Print[{this,is,a,printed,cell}]],
     Hold[{this,is,a,printed,cell}] } *)

We can also print the failed expressions for diagnostic purposes:

Scan[CellPrint @@ # &, Cases[$expressions, _$Failed]]

invalid cell screenshot

By default, notebookExpressions does not filter out any expressions based upon their content. If a second pattern argument is passed, expressions will only be shown if they match that pattern. Invalid cells are always returned since the pattern-matching is inconclusive. Here, we match all cells containing a SetDelayed expression (:=) or errors:

Column[notebookExpressions[$file, _SetDelayed], Frame -> All]

expression list screenshot

Observe that the specified pattern does not specify the Hold wrapper.

The following functions will load the expressions and erroneous cells from a notebook:

notebookExpressions[path_, pattern_:_] :=
  Cases[Import[path, "Notebook"] // First
  , c:Cell[_, "Input"|"Output"|"Print", ___] :>
      Module[{v = eval[c]}, v /; MatchQ[v, _$Failed | Hold[pattern]]]
  , Infinity
  ]

eval[cell_] :=
  Quiet @ Check[
    ToExpression[First@cell, StandardForm, Hold]
  , $Failed @ cell
  ]

notebookExpressions is the main function. First, it imports the cell expressions from a file. Then it filters out all but the Input, Output and Print cells. For each such cell, it attempts to convert the cell's contents to an expression. If it succeeds, the expression is wrapped in Hold. If it fails, then the original cell expression is wrapped in $Failed. The function eval performs the actual conversion.

To illustrate the use of this function, let's use the following notebook as input:

notebook screenshot

We can load and view the expressions as follows:

$file = "c:\\some\\path\\to\\se-notebook-expressions-test.nb";

$expressions = notebookExpressions[$file];

Column[$expressions, Frame -> All]

notebook expression list

We can see that the expressions are held, and that the cell with an invalid expression is wrapped in $Failed. The text cell was not read, but the input, output and printed cells were.

The valid expressions are readily singled out based upon their Hold wrapper:

Cases[$expressions, _Hold]

(* { Hold[zot[x_]:=x+1], Hold[zot[3]], Hold[4], Hold[Print[{this,is,a,printed,cell}]],
     Hold[{this,is,a,printed,cell}] } *)

We can also print the failed expressions for diagnostic purposes:

Scan[CellPrint @@ # &, Cases[$expressions, _$Failed]]

invalid cell screenshot

By default, notebookExpressions does not filter out any expressions based upon their content. If a second pattern argument is passed, expressions will only be shown if they match that pattern. Invalid cells are always returned since the pattern-matching is inconclusive. Here, we match all cells containing a SetDelayed expression (:=) or errors:

Column[notebookExpressions[$file, _SetDelayed], Frame -> All]

expression list screenshot

Observe that the specified pattern does not specify the Hold wrapper.

Beware that ToExpression has historically sometimes had trouble interpreting box expressions for unusual forms. The "real" parser appears to be unavailable to user code.

1
source | link

The following functions will load the expressions and erroneous cells from a notebook:

notebookExpressions[path_, pattern_:_] :=
  Cases[Import[path, "Notebook"] // First
  , c:Cell[_, "Input"|"Output"|"Print", ___] :>
      Module[{v = eval[c]}, v /; MatchQ[v, _$Failed | Hold[pattern]]]
  , Infinity
  ]

eval[cell_] :=
  Quiet @ Check[
    ToExpression[First@cell, StandardForm, Hold]
  , $Failed @ cell
  ]

notebookExpressions is the main function. First, it imports the cell expressions from a file. Then it filters out all but the Input, Output and Print cells. For each such cell, it attempts to convert the cell's contents to an expression. If it succeeds, the expression is wrapped in Hold. If it fails, then the original cell expression is wrapped in $Failed. The function eval performs the actual conversion.

To illustrate the use of this function, let's use the following notebook as input:

notebook screenshot

We can load and view the expressions as follows:

$file = "c:\\some\\path\\to\\se-notebook-expressions-test.nb";

$expressions = notebookExpressions[$file];

Column[$expressions, Frame -> All]

notebook expression list

We can see that the expressions are held, and that the cell with an invalid expression is wrapped in $Failed. The text cell was not read, but the input, output and printed cells were.

The valid expressions are readily singled out based upon their Hold wrapper:

Cases[$expressions, _Hold]

(* { Hold[zot[x_]:=x+1], Hold[zot[3]], Hold[4], Hold[Print[{this,is,a,printed,cell}]],
     Hold[{this,is,a,printed,cell}] } *)

We can also print the failed expressions for diagnostic purposes:

Scan[CellPrint @@ # &, Cases[$expressions, _$Failed]]

invalid cell screenshot

By default, notebookExpressions does not filter out any expressions based upon their content. If a second pattern argument is passed, expressions will only be shown if they match that pattern. Invalid cells are always returned since the pattern-matching is inconclusive. Here, we match all cells containing a SetDelayed expression (:=) or errors:

Column[notebookExpressions[$file, _SetDelayed], Frame -> All]

expression list screenshot

Observe that the specified pattern does not specify the Hold wrapper.