The pattern is matching more than intended. If any one could explain why 'address' is matched when it's part of the lookbehind and how to prevent that. Thanks in advance for any help on this.
Pattern:
(?<=@address|)[a-zA-Z]+(?=[^\]\[]*\])
String:
test [@address|singleline second] test
Results:
address singleline second
You need to escape the |
:
(?<=@address\|)[a-zA-Z]+(?=[^\]\[]*\])
since (?<=@address|)
asserts that the matched-string is preceded either by @address
or by the empty string. (And since everything is always preceded by the empty string, that has no effect.)
By the way, a small terminological note: (?<=@address|)
is called lookbehind, not lookahead. A lookahead assertion, such as your (?=[^\]\[]*\])
, asserts that a given point in the regex is (or is not) followed by a specified pattern.