Regular match #Comment not working correctly

I am trying to write a regex that will detect the following type of comment:

  • Starts with one #and continues to the end of the line.
  • Will treat any escaped #( \#) as plain text (this way it won’t do anything after it if it wasn’t already)

Using this tool , I came up with the following regular expression for this:

(?!\\)(#(.*)\n)

Using it in global mode, I checked it for the following introductory text, which is pretty clear:

#This comment should be caught, and any embedded #s or escaped \#s should also be
While none of this line this should be \#\#\#

Now matches for this regex:

  • Whole first row
  • Everything on the second line, starting from the first #, but not including \before

, - . , , ?

+4
1

:

(?<!\\)#.*

  • Lookbehind (?<!\\) , .
  • #
  • .*

. Regex Demo

+3

All Articles