正则表达式 - 排除子域(preg_match)

I'm want to match a domain (with preg_match) but I want to exclude a subdomain.

Example I want to match all subdomains on example.org except boon.example.org:

I've tried this:

$test = "boon.example.org";
$test2 = "null";
if(preg_match('#(?!boon)(\w+\.)?example\.org#', $test, $output)) {
    $test2 = $output[2] .'example.org';
}

But the output of test2 is: oon.example.org and not example.org

Somebody has an answer?

If you're looking for only that exact subdomain, couldn't you just check if the string boon.example.org is present? Seems a bit overkill with regex for this.

Regardless, the following regex should do what you want:

.*(?<!\bboon\.|^.)example.org

Would return subdomain.example.org for all sub domains except boon.example.org or any sub domains of boon.example.org.

Try This:

echo '<pre>';
$test = "boon.example.org";
$test2 = "null";
preg_match('/^(?!boon\.)([\w]+\.)?example\.org$/', $test, $output);
print_r($output);

This will match all sub-domain except boon.

This regex will works for you :

^(((?!boon).)+\.)?example\.org$

RegExr link