Regex - Exclude match if it precedes a specific character

I am trying to parse a string and return stuff in between #

, but only if #

not preceded \

.

For example, I messed up with the following test line at regexr.com:

This is a #date # test.

Using #.*#

I can get the test I want. However, give this line:

This is a # # date test.

I don't want it to return as the backslash is before #

. So far I have come up with:

[^\\](#.*.#)

      

However, when the backslash is missing from the test string, it ends capturing the character before the # character. Is there a way to grab only the "# date #" part from the test string, but only if it is not preceded by a black spoon?

+3


source to share


2 answers


You can use regex lookbehind for your case.

You can use this regex:

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

      



Working demo

enter image description here

+3


source


You can use the following regular expression.

\\#|#(.*?)#

      



Note. ... You can access your match result from the capture group#1

Live Demo

+2


source







All Articles