繁体   English   中英

preg_match中的正则表达式,返回3个匹配项

[英]a regular expression in preg_match that returns 3 matches

我的输入如下:

*test*

我想要的输出是两个星号内的内容(测试)。
我的代码如下:

preg_match('/^(\*(.*)\*)$/','*test*',$matches);

它的输出是:

 Array ( [0] => *test* [1] => *test* [2] => test ) 

第三个是我想要的。 我知道它为什么这样做,但我不知道如何解决它。 如何编写一个RE,只返回测试

您可以使用前瞻和后瞻断言

/(?<=^\*).*(?=\*$)/

你可以使用explode函数试试这个:

<?php $str = '*test*';
$str1 = explode('*',  $str);
echo $str1[1]; 
?> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM