Reputation: 1401
I'm trying to make a regex to identify a comment. It has to start with //
and end with a new line or a *)
pattern.
For now, I manage to get this (\/\/)([^\n\r]+)
, but I am unsuccessful to add the *)
pattern.
Any tips?
Upvotes: 5
Views: 17295
Reputation: 161
extendind the answer of @the-fourth-bird if you need to find a block of single lines of comments, something like this changing 3 for the number of lines, should help to find a bigger blocks
^(\/\/.*[\r\n]){3}$
And if trying to find a block of comment with /** */ here explain a few ways.
Upvotes: 0
Reputation: 163277
Try it like this:
^\/\/[^\n\r]+(?:[\n\r]|\*\))$
Matches
^
Beginning of the string\/\/
Match two forward slashes[^\n\r]+
Match not a newline or a carriage return 1 or more times(?:
Non capturing group
[\n\r]|\*\)
Match a newline or a carriage return or *))
Close non capturing group$
The end of the stringEdit:
Updated according to the comments, this is the final regex:
Upvotes: 7
Reputation: 13040
You can use (\/\/)(.+?)(?=[\n\r]|\*\))
.
?=
means the last group is a positive lookahead. It only assert the following characters can match the new-line-or-*)
pattern. If you want to match the new-line-or-*)
pattern as well, just remove ?=
.
.+?
means lazy matching, i.e. matching characters as few as possible. So for string such as // something *) something *)
, it will stop matching before the first *)
.
Note this pattern does not match //\n
(your previous regex does not as well) because +
means at least one characters. If you want to match such string, use *
instead of +
in the regex.
Finally, although you can use regex to parse such single line comments, as Jerry Coffin said in comment, don't try to parse programming source codes using regexes, because the language constituted by all legal source codes is commonly not a regular language.
Upvotes: 2