我的代码是:
$tt = 'This is a tomato test';
preg_match('/is(.*)to/', $tt, $match);
print_r($match);
从此我试图仅获得
" a toma"
输出......但它给了我:
Array
(
[0] => is is a tomato
[1] => is a toma
)
为此,正则表达式如何使其不在输出字符串开头显示额外的“is”(
this
的尾随字母)?
最简单的解决方案是注意“this”包含子字符串“is”,所以...
$tt='This is a tomato test';
$rr=preg_match('/ is(.*)to/',$tt,$match); // add a space before is.
print_r($match);
并且
[1]
将是“a toma”
另一个技巧是使用后向断言
(?<=
,其内容不会成为结果匹配的一部分:
preg_match('/(?<=\bis)(.*)to/', $tt, $match);