Provide regexes for comments.
use Regexp::Common qw /comment/; while (<>) { /$RE{comment}{C}/ and print "Contains a C comment\n"; /$RE{comment}{C++}/ and print "Contains a C++ comment\n"; /$RE{comment}{PHP}/ and print "Contains a PHP comment\n"; /$RE{comment}{Java}/ and print "Contains a Java comment\n"; /$RE{comment}{Perl}/ and print "Contains a Perl comment\n"; /$RE{comment}{awk}/ and print "Contains an awk comment\n"; /$RE{comment}{HTML}/ and print "Contains an HTML comment\n"; } use Regexp::Common qw /comment RE_comment_HTML/; while (<>) { $_ =~ RE_comment_HTML() and print "Contains an HTML comment\n"; }
Please consult the manual of Regexp::Common for a general description of the works of this interface.
Do not use this module directly, but load it via Regexp::Common.
This modules gives you regular expressions for comments in various languages.
Below, the comments of each of the languages are described. The patterns are available as $RE{comment}{LANG}, foreach language \s-1LANG\s0. Some languages have variants; it's described at the individual languages how to get the patterns for the variants. Unless mentioned otherwise, \*(C`{-keep}\*(C' sets $1, $2, $3 and $4 to the entire comment, the opening marker, the content of the comment, and the closing marker (for many languages, the latter is a newline) respectively.
Comments in \s-1ABC\s0 start with a backslash (\*(C`\\*(C'), and last till the end of the line. See <http://homepages.cwi.nl/%7Esteven/abc/>.
Comments in Ada start with \*(C`--\*(C', and last till the end of the line.
Advisor is a language used by the \s-1HP\s0 product glance. Comments for this language start with either \*(C`#\*(C' or \*(C`//\*(C', and last till the end of the line.
Comments for the Advsys language start with \*(C`;\*(C' and last till the end of the line. See also <http://www.wurb.com/if/devsys/12>.
Alan comments start with \*(C`--\*(C', and last till the end of the line. See also <http://w1.132.telia.com/~u13207378/alan/manual/alanTOC.html>.
Comments in the Algol 60 language start with the keyword \*(C`comment\*(C', and end with a \*(C`;\*(C'. See <http://www.masswerk.at/algol60/report.htm>.
In Algol 68, comments are either delimited by \*(C`#\*(C', or by one of the keywords \*(C`co\*(C' or \*(C`comment\*(C'. The keywords should not be part of another word. See http://westein.arb-phys.uni-dortmund.de/~wb/a68s.txt <http://westein.arb-phys.uni-dortmund.de/~wb/a68s.txt>. With \*(C`{-keep}\*(C', only $1 will be set, returning the entire comment.
The \s-1ALPACA\s0 language has comments starting with \*(C`/*\*(C' and ending with \*(C`*/\*(C'.
The awk programming language uses comments that start with \*(C`#\*(C' and end at the end of the line.
The B language has comments starting with \*(C`/*\*(C' and ending with \*(C`*/\*(C'.
There are various forms of \s-1BASIC\s0 around. Currently, we only support the variant supported by mvEnterprise, whose pattern is available as $RE{comment}{BASIC}{mvEnterprise}. Comments in this language start with a \*(C`!\*(C', a \*(C`*\*(C' or the keyword \*(C`REM\*(C', and end till the end of the line. See <http://www.rainingdata.com/products/beta/docs/mve/50/ReferenceManual/Basic.pdf>.
The esotoric language Beatnik only uses words consisting of letters. Words are scored according to the rules of Scrabble. Words scoring less than 5 points, or 18 points or more are considered comments (although the compiler might mock at you if you score less than 5 points). Regardless whether \*(C`{-keep}\*(C', $1 will be set, and set to the entire comment. This pattern requires perl 5.8.0 or newer.
The beta-Juliet programming language has comments that start with \*(C`//\*(C' and that continue till the end of the line. See also http://www.catseye.mb.ca/esoteric/b-juliet/index.html <http://www.catseye.mb.ca/esoteric/b-juliet/index.html>.
The esotoric language Befunge-98 uses comments that start and end with a \*(C`;\*(C'. See <http://www.catseye.mb.ca/esoteric/befunge/98/spec98.html>.
\s-1BML\s0, or Better Markup Language is an \s-1HTML\s0 templating language that uses comments starting with \*(C`<?c_\*(C', and ending with \*(C`c_?>\*(C'. See <http://www.livejournal.com/doc/server/bml.index.html>.
The minimal language Brainfuck uses only eight characters, \*(C`<\*(C', \*(C`>\*(C', \*(C`[\*(C', \*(C`]\*(C', \*(C`+\*(C', \*(C`-\*(C', \*(C`.\*(C' and \*(C`,\*(C'. Any other characters are considered comments. With \*(C`{-keep}\*(C', $1 is set to the entire comment.
The C language has comments starting with \*(C`/*\*(C' and ending with \*(C`*/\*(C'.
The C\*(-- language has comments starting with \*(C`/*\*(C' and ending with \*(C`*/\*(C'. See http://cs.uas.arizona.edu/classes/453/programs/C\*(--Spec.html <http://cs.uas.arizona.edu/classes/453/programs/C--Spec.html>.
The \*(C+ language has two forms of comments. Comments that start with \*(C`//\*(C' and last till the end of the line, and comments that start with \*(C`/*\*(C', and end with \*(C`*/\*(C'. If \*(C`{-keep}\*(C' is used, only $1 will be set, and set to the entire comment.
The C# language has two forms of comments. Comments that start with \*(C`//\*(C' and last till the end of the line, and comments that start with \*(C`/*\*(C', and end with \*(C`*/\*(C'. If \*(C`{-keep}\*(C' is used, only $1 will be set, and set to the entire comment. See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csspec/html/vclrfcsharpspec_C.asp <http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csspec/html/vclrfcsharpspec_C.asp>.
Comments in Caml start with \*(C`(*\*(C', end with \*(C`*)\*(C', and can be nested. See <http://www.cs.caltech.edu/courses/cs134/cs134b/book.pdf> and http://pauillac.inria.fr/caml/index-eng.html <http://pauillac.inria.fr/caml/index-eng.html>.
The Cg language has two forms of comments. Comments that start with \*(C`//\*(C' and last till the end of the line, and comments that start with \*(C`/*\*(C', and end with \*(C`*/\*(C'. If \*(C`{-keep}\*(C' is used, only $1 will be set, and set to the entire comment. See <http://developer.nvidia.com/attach/3722>.
In \*(C`CLU\*(C', a comment starts with a procent sign (\*(C`%\*(C'), and ends with the next newline. See ftp://ftp.lcs.mit.edu:/pub/pclu/CLU-syntax.ps <ftp://ftp.lcs.mit.edu:/pub/pclu/CLU-syntax.ps> and <http://www.pmg.lcs.mit.edu/CLU.html>.
Traditionally, comments in \s-1COBOL\s0 are indicated by an asteriks in the seventh column. This is what the pattern matches. Modern compiler may more lenient though. See <http://www.csis.ul.ie/cobol/Course/COBOLIntro.htm>, and <http://www.csis.ul.ie/cobol/default.htm>. Due to a bug in the regexp engine of perl 5.6.x, this regexp is only available in version 5.8.0 and up.
Comments in the chess query language (\s-1CQL\s0) start with a semi colon (\*(C`;\*(C') and last till the end of the line. See <http://www.rbnn.com/cql/>.
The formula editor in Crystal Reports uses comments that start with \*(C`//\*(C', and end with the end of the line.
There are two types of comments in Dylan. They either start with \*(C`//\*(C', or are nested comments, delimited with \*(C`/*\*(C' and \*(C`*/\*(C'. Under \*(C`{-keep}\*(C', only $1 will be set, returning the entire comment. This pattern requires perl 5.6.0 or newer.
The ECMAScript language has two forms of comments. Comments that start with \*(C`//\*(C' and last till the end of the line, and comments that start with \*(C`/*\*(C', and end with \*(C`*/\*(C'. If \*(C`{-keep}\*(C' is used, only $1 will be set, and set to the entire comment. JavaScript is Netscapes implementation of ECMAScript. See http://www.ecma-international.org/publications/files/ecma-st/Ecma-262.pdf <http://www.ecma-international.org/publications/files/ecma-st/Ecma-262.pdf>, and http://www.ecma-international.org/publications/standards/Ecma-262.htm <http://www.ecma-international.org/publications/standards/Ecma-262.htm>.
Eiffel comments start with \*(C`--\*(C', and last till the end of the line.
In False, comments start with \*(C`{\*(C' and end with \*(C`}\*(C'. See <http://wouter.fov120.com/false/false.txt>
The \s-1FPL\s0 language has two forms of comments. Comments that start with \*(C`//\*(C' and last till the end of the line, and comments that start with \*(C`/*\*(C', and end with \*(C`*/\*(C'. If \*(C`{-keep}\*(C' is used, only $1 will be set, and set to the entire comment.
Comments in Forth start with \*(C`\\*(C', and end with the end of the line. See also http://docs.sun.com/sb/doc/806-1377-10 <http://docs.sun.com/sb/doc/806-1377-10>.
There are two forms of Fortran. There's free form Fortran, which has comments that start with \*(C`!\*(C', and end at the end of the line. The pattern for this is given by $RE{Fortran}. Fixed form Fortran, which has been obsoleted, has comments that start with \*(C`C\*(C', \*(C`c\*(C' or \*(C`*\*(C' in the first column, or with \*(C`!\*(C' anywhere, but the sixth column. The pattern for this are given by $RE{Fortran}{fixed}. See also http://www.cray.com/craydoc/manuals/007-3692-005/html-007-3692-005/ <http://www.cray.com/craydoc/manuals/007-3692-005/html-007-3692-005/>.
The esotoric language Funge-98 uses comments that start and end with a \*(C`;\*(C'.
Configuration files for fvwm2 have comments starting with a \*(C`#\*(C' and lasting the rest of the line.
Haifu, an esotoric language using haikus, has comments starting and ending with a \*(C`,\*(C'. See <http://www.dangermouse.net/esoteric/haifu.html>.
There are two types of comments in Haskell. They either start with at least two dashes, or are nested comments, delimited with \*(C`{-\*(C' and \*(C`-}\*(C'. Under \*(C`{-keep}\*(C', only $1 will be set, returning the entire comment. This pattern requires perl 5.6.0 or newer.
In \s-1HTML\s0, comments only appear inside a comment declaration. A comment declaration starts with a \*(C`<!\*(C', and ends with a \*(C`>\*(C'. Inside this declaration, we have zero or more comments. Comments starts with \*(C`--\*(C' and end with \*(C`--\*(C', and are optionally followed by whitespace. The pattern $RE{comment}{HTML} recognizes those comment declarations (and hence more than a comment). Note that this is not the same as something that starts with \*(C`<!--\*(C' and ends with \*(C`-->\*(C', because the following will be matched completely: <!-- First Comment -- --> Second Comment <!-- -- Third Comment --> Do not be fooled by what your favourite browser thinks is an \s-1HTML\s0 comment. If \*(C`{-keep}\*(C' is used, the following are returned:
captures the entire comment declaration. captures the \s-1MDO\s0 (markup declaration open), \*(C`<!\*(C'. captures the content between the \s-1MDO\s0 and the \s-1MDC\s0. captures the (last) comment, without the surrounding dashes. captures the \s-1MDC\s0 (markup declaration close), \*(C`>\*(C'.
There are two types of comments in Hugo. They either start with \*(C`!\*(C' (which cannot be followed by a \*(C`\\*(C'), or are nested comments, delimited with \*(C`!\\*(C' and \*(C`\!\*(C'. Under \*(C`{-keep}\*(C', only $1 will be set, returning the entire comment. This pattern requires perl 5.6.0 or newer.
Icon has comments that start with \*(C`#\*(C' and end at the next new line. See <http://www.toolsofcomputing.com/IconHandbook/IconHandbook.pdf>, <http://www.cs.arizona.edu/icon/index.htm>, and <http://burks.bton.ac.uk/burks/language/icon/index.htm>.
The esotoric language \s-1ILLGOL\s0 uses comments starting with \s-1NB\s0 and lasting till the end of the line. See <http://www.catseye.mb.ca/esoteric/illgol/index.html>.
Comments in \s-1INTERCAL\s0 are single line comments. They start with one of the keywords \*(C`NOT\*(C' or \*(C`N'T\*(C', and can optionally be preceded by the keywords \*(C`DO\*(C' and \*(C`PLEASE\*(C'. If both keywords are used, \*(C`PLEASE\*(C' precedes \*(C`DO\*(C'. Keywords are separated by whitespace.
The language J uses comments that start with \*(C`NB.\*(C', and that last till the end of the line. See <http://www.jsoftware.com/books/help/primer/contents.htm>, and <http://www.jsoftware.com/>.
The Java language has two forms of comments. Comments that start with \*(C`//\*(C' and last till the end of the line, and comments that start with \*(C`/*\*(C', and end with \*(C`*/\*(C'. If \*(C`{-keep}\*(C' is used, only $1 will be set, and set to the entire comment.
The Javadoc documentation syntax is demarked with a subset of ordinary Java comments to separate it from code. Comments start with \*(C`/**\*(C' end with \*(C`*/\*(C'. If \*(C`{-keep}\*(C' is used, only $1 will be set, and set to the entire comment. See http://www.oracle.com/technetwork/java/javase/documentation/index-137868.html#format <http://www.oracle.com/technetwork/java/javase/documentation/index-137868.html#format>.
The JavaScript language has two forms of comments. Comments that start with \*(C`//\*(C' and last till the end of the line, and comments that start with \*(C`/*\*(C', and end with \*(C`*/\*(C'. If \*(C`{-keep}\*(C' is used, only $1 will be set, and set to the entire comment. JavaScript is Netscapes implementation of ECMAScript. See http://www.mozilla.org/js/language/E262-3.pdf <http://www.mozilla.org/js/language/E262-3.pdf>, and <http://www.mozilla.org/js/language/>.
The documentation language LaTeX uses comments starting with \*(C`%\*(C' and ending at the end of the line.
Comments in Lisp start with a semi-colon (\*(C`;\*(C') and last till the end of the line.
The \s-1LPC\s0 language has comments starting with \*(C`/*\*(C' and ending with \*(C`*/\*(C'.
Comments for the language \s-1LOGO\s0 start with \*(C`;\*(C', and last till the end of the line.
Comments for the lua language start with \*(C`--\*(C', and last till the end of the line. See also <http://www.lua.org/manual/manual.html>.
In \*(C`M\*(C' (aka \*(C`MUMPS\*(C'), comments start with a semi-colon, and last till the end of a line. The language specification requires the semi-colon to be preceded by one or more linestart characters. Those characters default to a space, but that's configurable. This requirement, of preceding the comment with linestart characters is not tested for. See <ftp://ftp.intersys.com/pub/openm/ism/ism64docs.zip>, <http://mtechnology.intersys.com/mproducts/openm/index.html>, and <http://mcenter.com/mtrc/index.html>.
By default, the preprocessor language m4 uses single line comments, that start with a \*(C`#\*(C' and continue to the end of the line, including the newline. The pattern \*(C`$RE {comment} {m4}\*(C' matches such comments. In m4, it is possible to change the starting token though. See <http://wolfram.schneider.org/bsd/7thEdManVol2/m4/m4.pdf>, http://www.cs.stir.ac.uk/~kjt/research/pdf/expl-m4.pdf <http://www.cs.stir.ac.uk/~kjt/research/pdf/expl-m4.pdf>, and <http://www.gnu.org/software/m4/manual/>.
In \*(C`Modula-2\*(C', comments start with \*(C`(*\*(C', and end with \*(C`*)\*(C'. Comments may be nested. See <http://www.modula2.org/>.
In \*(C`Modula-3\*(C', comments start with \*(C`(*\*(C', and end with \*(C`*)\*(C'. Comments may be nested. See <http://www.m3.org/>.
Configuration files for mutt have comments starting with a \*(C`#\*(C' and lasting the rest of the line.
The Nickle language has one line comments starting with \*(C`#\*(C' (like Perl), or multiline comments delimited by \*(C`/*\*(C' and \*(C`*/\*(C' (like C). Under \*(C`-keep\*(C', only $1 will be set. See also <http://www.nickle.org>.
Comments in Oberon start with \*(C`(*\*(C' and end with \*(C`*)\*(C'. See <http://www.oberon.ethz.ch/oreport.html>.
There are many implementations of Pascal. This modules provides pattern for comments of several implementations.
This is the pattern that recognizes comments according to the Pascal \s-1ISO\s0 standard. This standard says that comments start with either \*(C`{\*(C', or \*(C`(*\*(C', and end with \*(C`}\*(C' or \*(C`*)\*(C'. This means that \*(C`{*)\*(C' and \*(C`(*}\*(C' are considered to be comments. Many Pascal applications don't allow this. See http://www.pascal-central.com/docs/iso10206.txt <http://www.pascal-central.com/docs/iso10206.txt> The Alice Pascal compiler accepts comments that start with \*(C`{\*(C' and end with \*(C`}\*(C'. Comments are not allowed to contain newlines. See <http://www.templetons.com/brad/alice/language/>. The Delphi Pascal, Free Pascal and the Gnu Pascal Compiler implementations of Pascal all have comments that either start with \*(C`//\*(C' and last till the end of the line, are delimited with \*(C`{\*(C' and \*(C`}\*(C' or are delimited with \*(C`(*\*(C' and \*(C`*)\*(C'. Patterns for those comments are given by $RE{comment}{Pascal}{Delphi}, $RE{comment}{Pascal}{Free} and $RE{comment}{Pascal}{GPC} respectively. These patterns only set $1 when \*(C`{-keep}\*(C' is used, which will then include the entire comment. See <http://info.borland.com/techpubs/delphi5/oplg/>, http://www.freepascal.org/docs-html/ref/ref.html <http://www.freepascal.org/docs-html/ref/ref.html> and http://www.gnu-pascal.de/gpc/ <http://www.gnu-pascal.de/gpc/>. The Workshop Pascal compiler, from \s-1SUN\s0 Microsystems, allows comments that are delimited with either \*(C`{\*(C' and \*(C`}\*(C', delimited with \*(C`(*)\*(C' and \*(C`*\*(C'), delimited with \*(C`/*\*(C', and \*(C`*/\*(C', or starting and ending with a double quote (\*(C`"\*(C'). When \*(C`{-keep}\*(C' is used, only $1 is set, and returns the entire comment. See http://docs.sun.com/db/doc/802-5762 <http://docs.sun.com/db/doc/802-5762>.
Comments in \s-1PEARL\s0 start with a \*(C`!\*(C' and last till the end of the line, or start with \*(C`/*\*(C' and end with \*(C`*/\*(C'. With \*(C`{-keep}\*(C', $1 will be set to the entire comment.
Comments in \s-1PHP\s0 start with either \*(C`#\*(C' or \*(C`//\*(C' and last till the end of the line, or are delimited by \*(C`/*\*(C' and \*(C`*/\*(C'. With \*(C`{-keep}\*(C', $1 will be set to the entire comment.
In \s-1PL/B\s0, comments start with either \*(C`.\*(C' or \*(C`;\*(C', and end with the next newline. See http://www.mmcctech.com/pl-b/plb-0010.htm <http://www.mmcctech.com/pl-b/plb-0010.htm>.
The \s-1PL/I\s0 language has comments starting with \*(C`/*\*(C' and ending with \*(C`*/\*(C'.
In \s-1PL/SQL\s0, comments either start with \*(C`--\*(C' and run till the end of the line, or start with \*(C`/*\*(C' and end with \*(C`*/\*(C'.
Perl uses comments that start with a \*(C`#\*(C', and continue till the end of the line.
The Portia programming language has comments that start with \*(C`//\*(C', and last till the end of the line.
Python uses comments that start with a \*(C`#\*(C', and continue till the end of the line.
Comments in the Q-BAL language start with \*(C``\*(C' (a backtick), and contine till the end of the line.
In \*(C`QML\*(C', comments start with \*(C`#\*(C' and last till the end of the line. See <http://www.questionmark.com/uk/qml/overview.doc>.
The statistical language R uses comments that start with a \*(C`#\*(C' and end with the following new line. See http://www.r-project.org/ <http://www.r-project.org/>.
Comments for the \s-1REBOL\s0 language start with \*(C`;\*(C' and last till the end of the line.
Comments in Ruby start with \*(C`#\*(C' and last till the end of the time.
Scheme comments start with \*(C`;\*(C', and last till the end of the line. See <http://schemers.org/>.
Comments in various shells start with a \*(C`#\*(C' and end at the end of the line.
The esotoric language Shelta uses comments that start and end with a \*(C`;\*(C'. See <http://www.catseye.mb.ca/esoteric/shelta/index.html>.
The \s-1SLIDE\s0 language has two froms of comments. First there is the line comment, which starts with a \*(C`#\*(C' and includes the rest of the line (just like Perl). Second, there is the multiline, nested comment, which are delimited by \*(C`(*\*(C' and \*(C`*)\*(C'. Under C{-keep}>, only $1 is set, and is set to the entire comment. This pattern needs at least Perl version 5.6.0. See <http://www.cs.berkeley.edu/~ug/slide/docs/slide/spec/spec_frame_intro.shtml>.
Configuration files for slrn have comments starting with a \*(C`%\*(C' and lasting the rest of the line.
Smalltalk uses comments that start and end with a double quote, \*(C`"\*(C'.
Comments in the \s-1SMITH\s0 language start with \*(C`;\*(C', and last till the end of the line.
In the Smalltalk variant Squeak, comments start and end with \*(C`"\*(C'. Double quotes can appear inside comments by doubling them.
Standard \s-1SQL\s0 uses comments starting with two or more dashes, and ending at the end of the line. MySQL does not follow the standard. Instead, it allows comments that start with a \*(C`#\*(C' or \*(C`-- \*(C' (that's two dashes and a space) ending with the following newline, and comments starting with \*(C`/*\*(C', and ending with the next \*(C`;\*(C' or \*(C`*/\*(C' that isn't inside single or double quotes. A pattern for this is returned by $RE{comment}{SQL}{MySQL}. With \*(C`{-keep}\*(C', only $1 will be set, and it returns the entire comment.
In Tcl, comments start with \*(C`#\*(C' and continue till the end of the line.
The documentation language TeX uses comments starting with \*(C`%\*(C' and ending at the end of the line.
The document formatting language troff uses comments starting with \*(C`\"\*(C', and continuing till the end of the line.
The Windows programming language Ubercode uses comments that start with \*(C`//\*(C' and continue to the end of the line. See <http://www.ubercode.com>.
In configuration files for the editor vi, one can use comments starting with \*(C`"\*(C', and ending at the end of the line.
In the language *W, comments start with \*(C`||\*(C', and end with \*(C`!!\*(C'.
Comments in \s-1DNS\s0 zonefiles start with \*(C`;\*(C', and continue till the end of the line.
The in-game language ZZT-OOP uses comments that start with a \*(C`'\*(C' character, and end at the following newline. See <http://dave2.rocketjump.org/rad/zzthelp/lang.html>.
Charles F. Goldfarb: The \s-1SGML\s0 Handbook. Oxford: Oxford University Press. 1990. \s-1ISBN\s0 0-19-853737-9. Ch. 10.3, pp 390-391.
Regexp::Common for a general description of how to use this interface.
Damian Conway ([email protected])
This package is maintained by Abigail ([email protected]).
Bound to be plenty.
For a start, there are many common regexes missing. Send them in to [email protected].
This software is Copyright (c) 2001 - 2009, Damian Conway and Abigail.
This module is free software, and maybe used under any of the following licenses:
1) The Perl Artistic License. See the file COPYRIGHT.AL. 2) The Perl Artistic License 2.0. See the file COPYRIGHT.AL2. 3) The BSD Licence. See the file COPYRIGHT.BSD. 4) The MIT Licence. See the file COPYRIGHT.MIT.