Trying to figure out a Regular Expression to match only 2 consecutive "#". The regex should be able to detect a match within the following examples:
Example 1) firstword ##helloworld## lastword
Example 2) ##helloworld##
I currently have ^##.*##$ but it incorrectly matches to:
###helloworld###
Or it doesn't match when there's a string before or after 2 consecutive #:
firstword ##helloworld## lastword
Any feedback welcomed!!
CodePudding user response:
You can use lookarounds like
(?<!#)##(?!#).*?(?<!#)##(?!#)
Details:
(?<!#)##(?!#)
- a##
string not immediately preceded nor followed with another#
char.*?
- any zero or more chars other than line break chars as few as possible(?<!#)##(?!#)
- a##
string not immediately preceded nor followed with another#
char