I'm new to REGEX and I need some help.
I need to match all three types of comments that PHP might have:
# Single line comment
// Single line comment
/* Multi-line comments */
* And all of it's possible variations
echo 'something'; # this is a comment
Parsing a programming language seems too much for regexes to do. You should probably look for a PHP parser.
But these would be the regexes you are looking for. I assume for all of them that you use the DOTALL or SINGLELINE option (although the first two would work without it as well):
~#[^\r\n]*~ ~//[^\r\n]*~ ~/\*.*?\*/~s
Note that any of these will cause problems, if the comment-delimiting characters appear in a string or somewhere else, where they do not actually open a comment.
You can also combine all of these into one regex:
If you use some tool or language that does not require delimiters (like Java or C#), remove those
~. In this case you will also have to apply the DOTALL option differently. But without knowing where you are going to use this, I cannot tell you how.
If you cannot/do not want to set the DOTALL option, this would be equivalent (I also left out the delimiters to give an example):
See here for a working demo.
Now if you also want to capture the contents of the comments in a group, then you could do this
Regardless of the type of comment, the comments content (without the syntax delimiters) will be found in capture 1.
Another working demo.