Finding (and possibly extracting) source code in heterogenous text data set
There are many possible ways you might do this.  I'll suggest one.

I suggest you train a classifier to recognize whether a sequence of characters is code or text.  Here, the goal is to build a boolean classifier that either outputs "text" or "code".  You'll train it only on documents that are entirely text or entirely code (no mixed documents).  Build a classifier that not only outputs a classification, but also a confidence (a likelihood value for the chosen classification).

Now, to apply it to mixed documents, run the classifier on all substrings.  If a substring $S$ is classified as "code" with high confidence (high likelihood), above some threshold, and there's no superstring $S'$ of $S$ (of length not too much longer than $S$'s length) that is also classified as "code", then output $S$ as one of the code elements from the mixed document.  You'll need to select a threshold with a little bit of experimentation.

I suspect this might work nicely.  You'll have to try it to see, though.

So, how do you build such a boolean classifier?  One simple approach would be to use a simple linear classifier (e.g., SVM or naive Bayes) with features based on bag-of-words on lexical tokens.  That alone might suffice (e.g., code tends to have many instances of tokens like "{" and "return", but text doesn't).  You could also try bag-of-words on individual characters.  If you want to build a more sophisticated classifier, you could train a character-level recurrent neural network (e.g., a LSTM or a CNN); this might achieve higher accuracy, given a large enough training set, but probably will also require a bit more technical knowledge.