On a recent project, I needed to build a regular expression to read what are known as single line comments. You know the type in C-based languages:
// this is a basic comment
My source for my original expression was slightly misleading (as it was the one expression I did not personally write, and in haste did not bother to read carefully). Of course, in hindsight, I realize that wasn’t going to be enough.
In my system, my regular expression always needed to match from the beginning. This is what I came up with:
^(//)[^\n\r]*[\n\r]. My explanation:
- always starting with
- zero or more non-newline characters,
- some ending termination in the form of a newline character,
This works quite well to find simple single line comment in code, but it also may interfere eventually with reading a block comment beginning marker, so there might need to be revisions.
I’m not a regular expression master, I leave that to Sam.