Imagine you have the word Test and you want the regex to match only if certain characters are at the begininng or the end of the match.
Example: Lets say you want the one or more of the following (Including space) characters (),&=<> to be allowed at the beginning and end of the sentence and you have the following test cases:
Test[MATCH]
Test [MATCH]
This is a Test sentence [MATCH]
This >Test is a sentence [MATCH]
This Test) is a sentence [MATCH]
This Test&Hello is a sentence [MATCH]
This is a _Test sentence [NO MATCH]
This sentence is ?Test [NO MATCH]
Is it possible please?
>Solution :
I think, something like this should work:
(?<![^\(\)\,\&\=\<\>\s])Test(?![^\(\)\,\&\=\<\>\s])
[^\(\)\,\&\=\<\>\s] represents a character class matching any character except for (),&=<> and whitespaces
([ab] matches either a or b, thus [^ab] matches anything except for a and b)
(?<!) is negative lookbehind
(?!) is negative lookahead
