2011-02-15 164 views
1

我的代码是:PHP字符串的正则表达式

$tt='This is a tomato test'; 
$rr=preg_match('/is(.*)to/',$tt,$match); 
print_r($match); 

从这个我想只得到" a toma"输出...但它给我:

Array 
(
    [0] => is is a tomato 
    [1] => is a toma 
) 

对于这个表达式我怎样才能使它不会在输出字符串的开头显示“is”?

+0

我想你必须拿出一个更具体的规则。 `/ is`将匹配第一个“is”,这是“This”。如果这不是你想要的,你必须改进规则以声明“不是随之而来的是另一个”或“是一个单独的单词”或沿着这些方向的东西。什么是规则? – deceze 2011-02-15 13:03:58

回答

1

最简单的办法是要注意,“这”包括子“是”如此...

$tt='This is a tomato test'; 
$rr=preg_match('/ is(.*)to/',$tt,$match); // add a space before is. 
print_r($match); 

而且[1]将以“托马”

+0

谢谢,以及如何摆脱输出显示“是”? – David19801 2011-02-15 13:00:17

1

另一个技巧是使用相反的断言(?<=其内容将不会是结果匹配的一部分:

preg_match('/(?<=\bis)(.*)to/', $tt, $match);