Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect source code in a text?

Tags:

python

Is it possible to detect a programming language source code (primarily Java and C# ) in a text?

For example I want to know whether there is any source code part in this text.

.. text text text text text text text text text
text text text text text text text text text
text text text text text text text text text

public static Person createInstance() { return new Person();}

text text text text text text text text text
text text text text text text text text text
text text text text text text text text text ..

I have been searching this for a while and I couldn't find anything.

A solution with Python would be wonderful.

Regards.

like image 537
Kerem Avatar asked Jul 05 '10 12:07

Kerem


People also ask

How do you find the source code of text?

Press the Ctrl+N keys. Alternatively, right-click in the source code area, and from the pop-up menu, click Find Next. The source code scrolls if necessary, and the matching text is highlighted.

How is source code hidden?

JavaScript Encryption This is by far the most popular way to try to hide one's source code. It involves taking your code, using a custom made function to “encrypt” it somehow, and then putting it in an HTML file along with a function that will decrypt it for the browser.


1 Answers

There are some syntax highlighters around (pygments, google-code-prettify) and they've solved code detection and classification. Studying their sources could give an impression how it is done.

(now that I looked at pygments again - I don't know if they can autodetect the programming language. But google-code-prettify definitly can do it)

like image 97
Andreas Dolk Avatar answered Oct 04 '22 11:10

Andreas Dolk