IP-Info is a website where you can find out what your IP is as well as your user agent and information about your browser and OS. The site also has tools for looking up information about a particular IP and reverse DNS lookups. There is also a tool where you can check "is this site down" and numerous informative articles.

Regex comment matching

Not too long ago I ran into a situation where I would've liked to strip the contents of a file from its comments and strings. Actually, it wasn't just one file, it was a shitload of them as part of an automated process.

This isn't the first time I've had to strip out comments and/or strings. So I figured that it would be nice to solve it with regular expressions and not have to do a lot of manual if checking and such, just a nice, concise, easily reusable regex.

Unfortunately, the suggestions I found while googling were inadequate, not taking into account things like comments in strings, strings in comments, escaping of closing quotes, etc.
So I wrote my own.

The only requirement on the regexes here is that the code isn't interpreted as multiline, but one long string and that the regex engine used is up to date enough to handle magic like \K.

PHP is a mean bastard of a language and will probably require two parsings since it can turn PHP on or off at will.
The regex is certain to work if and only if the code you're matching against is entirely PHP (and not mixed):

These regexes should match any comments (and strings, with some very minor modification) and will allow easy removal of them with a regex replace.

Remember that you will probably have to escape these regexes again if you're using them, for instance, in PHP or a similar language.
As mentioned, they are currently set to leave strings alone and only match comments, but that is easily modified (just remove whichever \K you're particularly not fond of).

Feel free to add regex for other languages or tell me if I've missed something, things may have slipped by my testing after all.

Not too long ago I ran into a situation where I would've liked to strip the contents of a file from its comments and strings. Actually, it wasn't just one file, it was a shitload of them as part of an automated process.

This isn't the first time I've had to strip out comments and/or strings. So I figured that it would be nice to solve it with regular expressions and not have to do a lot of manual if checking and such, just a nice, concise, easily reusable regex.

Unfortunately, the suggestions I found while googling were inadequate, not taking into account things like comments in strings, strings in comments, escaping of closing quotes, etc.
So I wrote my own.

The only requirement on the regexes here is that the code isn't interpreted as multiline, but one long string and that the regex engine used is up to date enough to handle magic like \K.

PHP is a mean bastard of a language and will probably require two parsings since it can turn PHP on or off at will.
The regex is certain to work if and only if the code you're matching against is entirely PHP (and not mixed):

These regexes should match any comments (and strings, with some very minor modification) and will allow easy removal of them with a regex replace.

Remember that you will probably have to escape these regexes again if you're using them, for instance, in PHP or a similar language.
As mentioned, they are currently set to leave strings alone and only match comments, but that is easily modified (just remove whichever \K you're particularly not fond of).

Feel free to add regex for other languages or tell me if I've missed something, things may have slipped by my testing after all.

About

Valhalla is a place for sharing knowledge and ideas. Users can submit code, as well as science, technology, and engineering-oriented news and articles. We also have an assortment of fun and educational challenges intended to help users learn more about programming, mathematics, encryption, hacking and more.

We also affiliate with other IT-related sites. We provide a biography and tags for each affiliate so users can easily find the content they are interested in.