正则表达式匹配包含锚中特定单词的所有链接?

I am looking for a regular expression in PHP to extract the links a text that contain the specific words (apple, home, car) in the text of anchor.

Important: the formatting of links is not known in advance.

E.g:

<a href="fruit.html">The Apple red</a>
<a href="Construction.html#one">The big Home</a>
<a href="automotive.html?lang=en">Car for rent</a>

Desired result:

fruit.html
Construction.html#one
automotive.html?lang=en

My pattern:

/<a.*?href="(.*)".*?>apple|car|home<\/a>/i

Update: This pattern works

'/<a.+href=["\'](.*)["\'].*>(.*(?:apple|car|home).*)<\/a>/iU'

You could make use of DOMDocument and use getElementsByTagName to get the <a> elements.

Then you might use preg_match and a regex with an alternation with the words you want to find and add word boundaries to make sure the words are not part of a larger match. To account for case insensitivity you could use the /i flag.

\b(?:apple|big|car)\b

$data = <<<DATA
<a href="fruit.html">The Apple red</a>
<a href="Construction.html#one">The big Home</a>
<a href="automotive.html?lang=en">Car for rent</a>
<a href="fruit.html">The Pineapple red</a>
<a href="Construction.html#one">The biggest Home</a>
<a href="automotive.html?lang=en">Cars for rent</a>
DATA;

$dom = new DOMDocument();
$dom->loadHTML($data);

foreach($dom->getElementsByTagName("a") as $element) {
    if (preg_match('#\b(?:apple|big|car)\b#i', $element->nodeValue)) {
        echo $element->getAttribute("href") . "<br>";
    }
}

Demo

That would give you:

fruit.html
Construction.html#one
automotive.html?lang=en