Php preg_match与html标记的子模式不匹配

Php preg_match与html标记的子模式不匹配,php,regex,html-parsing,preg-match,Php,Regex,Html Parsing,Preg Match,我有一个正则表达式: $reg = '/<a class="title".*>(.*)<\/a>/'; 这是回报 Array ( [0] => <a class="title" href="/first-link/">Some text<br /><span class="title-highlight">with a span</span></a> [1] => ) 数组( [

我有一个正则表达式:

$reg = '/<a class="title".*>(.*)<\/a>/';
这是回报

Array (
    [0] => <a class="title" href="/first-link/">Some text<br /><span class="title-highlight">with a span</span></a>
    [1] => 
)
数组(
[0] => 
[1] => 
)
鉴于

$text2 = '<h3 class="carousel-post-title"><a class="title" href="/second-link/">Some text here</a></h3>';

preg_match($reg, $text2, $matches);
$text2='';
预匹配($reg,$text2,$matches);
返回

Array
(
    [0] => <a class="title" href="/second-link/">Some text here</a>
    [1] => Some text here
)
数组
(
[0] => 
[1] =>这里有一些文字
)

为什么呢?为什么子模式“(.*)”与“带跨度”的”不匹配?

将您的模式更改为

$reg = '/<a class="title"[^>]*>([^<]*)<\/a>/';

*
是贪婪的,它能吃多少就吃多少(ᗧ•••). 使用
*?
^或
]*>(.*)
@splash58本质上与非贪婪相同,
*?
@Sverri M.Olsen我明白了,只是我写了评论,决定不清除它
Array
(
    [0] => <a class="title" href="/second-link/">Some text here</a>
    [1] => Some text here
)
$reg = '/<a class="title"[^>]*>([^<]*)<\/a>/';
<a class="title"[^>]*> //Get the opening tag
([^<]*) //match anything until you reach a closing tag
<\/a> // your closing tag