Reputation: 17049
Starts with aa or bb. Ends with it (back reference). And has no it in the middle.
It is ok without "or" and back reference -- aa.*?(?!aa).*?aa
But it does not work with them:
(aa|bb).*?(?!\1).*?\1
echo preg_match("@(aa|bb).*?(?!\\1).*?\\1@", 'aaxxaaxaa'); // 1 ??
echo preg_match("@(aa|bb).*?(?!\\1).*?\\1@", 'aaxxxaa'); // 1
What is wrong?
Upvotes: 1
Views: 146
Reputation: 838376
You need to check your negative condition for every character.
(aa|bb)((?!\\1).)*\\1
You also need to add anchors.
^(aa|bb)((?!\\1).)*\\1$
See it working online: ideone
Upvotes: 2