This example shows how you can generate a regular expression to match comments as used in Java and other programming languages. First we create a regex that matches single-line comments. These start with // and run until the end of the line.
// Single-line comment /* Multi-line comments can span multiple lines */ /* Multi-line comments /* cannot be nested // at all */ Not a comment /* This comment */ // split in two // Single-line comments // cannot be /* nested */ /** Documentation comment **/ // Done!
which matches // literally.
. It also literally matches the text we just marked.
to match anything until the end of the line. On the Match panel, set “pattern to match field” to “match anything”. Then set “match anything except” to “nothing”.
does not include any line breaks.
to zero and tick the “unlimited” checkbox to allow field
to match any number of characters.//.*
Required options: Dot doesn’t match line breaks; Default line breaks.
Unused options: Case sensitive; Exact spacing; ^$ don’t match at line breaks.
// Single-line comment /* Multi-line comments can span multiple lines */ /* Multi-line comments /* cannot be nested // at all */ Not a comment /* This comment */ // split in two // Single-line comments // cannot be /* nested */ /** Documentation comment **/ // Done!
The first example matches only single-line comments. It totally ignores multi-line comments. So it does match any // that occurs within a multi-line comments as a single-line comment. We can extend our regular expression to properly deal with both types of comments.
and the newly marked field, RegexMagic treats the newly marked /* as the start of a new alternative that the generated regular expression should be able to match independently from the previous set of fields. This is correct. We want our regex to be able to separately match single-line and multi-line comments. So RegexMagic adds a new alternation field
to hold our two alternatives. It adds a new sequence field
that holds the original two fields that match the single-line comment. These are now numbered
and
. The second alternative under field
is the newly added field
which matches the /* we marked literally.
to be a sequence field with field
being the old field
matching the /* and field
matching the two lines of text we just marked. We’ll adjust field
soon.
to the sequence. It matches */ literally.
in the “select field” drop-down list.
can never match the text matched by field
. The comment must end at the first */. This would be important if we added more fields to the sequence after field
to match something that follows the comment. If that something is missing, we don’t want the regex engine to try to expand the comment until the next */in the file.
to zero and tick the “unlimited” checkbox to allow it to match any number of characters.
stops before the first */. RegexMagic will refuse to generate the regex if we don’t select “as few times as possible” for a field that has “match anything except” set to “text matched by the next field”.//.*|/\*(?>(?s:.)*?\*/)
Required options: Dot doesn’t match line breaks; Default line breaks.
Unused options: Case sensitive; Exact spacing; ^$ don’t match at line breaks.
// Single-line comment /* Multi-line comments can span multiple lines */ /* Multi-line comments /* cannot be nested // at all */ Not a comment /* This comment */ // split in two // Single-line comments // cannot be /* nested */ /** Documentation comment **/ // Done!
If you don’t have any single-line comments to deal with, you can easily remove that part from the regular expression.
to make the field buttons work on that field.
to indicate that it deletes sequence field
along with fields
and
that the sequence consists of.This is all we really need to do. But it leaves some unnecessary fields. This doesn’t impact the generated regular expression. But it leaves clutter on the Match panel.
.
to delete field
.
that we don’t need either. Click the Delete One Field button
again./\*(?>.*?\*/)
Required options: Dot matches line breaks; Default line breaks.
Unused options: Case sensitive; Exact spacing; ^$ don’t match at line breaks.
// Single-line comment /* Multi-line comments can span multiple lines */ /* Multi-line comments /* cannot be nested // at all */ Not a comment /* This comment */ // split in two // Single-line comments // cannot be /* nested */ /** Documentation comment **/ // Done!
If you wanted to create this regex from scratch, you could proceed as follows after pasting in the sample text:
which matches the /* we marked literally.
matching the two lines of text we just marked. We’ll adjust field
soon.
to the sequence. It matches */ literally.
in the “select field” drop-down list.
can never match the text matched by field
. The comment must end at the first */. This would be important if we added more fields after field
to match something that follows the comment. If that something is missing, we don’t want the regex engine to try to expand the comment until the next */in the file.
to zero and tick the “unlimited” checkbox to allow it to match any number of characters.
stops before the first */. RegexMagic will refuse to generate the regex if we don’t select “as few times as possible” for a field that has “match anything except” set to “text matched by the next field”./\*(?>.*?\*/)
Required options: Dot matches line breaks; Default line breaks.
Unused options: Case sensitive; Exact spacing; ^$ don’t match at line breaks.