简体   繁体   English

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

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

My input is as below: 我的输入如下:

*test*

The output I want is content inside two asterisks (test). 我想要的输出是两个星号内的内容(测试)。
My code is as below: 我的代码如下:

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

Its output is: 它的输出是:

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

The third one is the one I want. 第三个是我想要的。 I know why it does this, but I don't know how to solve it. 我知道它为什么这样做,但我不知道如何解决它。 How to write an RE that returns just test nothing else. 如何编写一个RE,只返回测试

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

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

you can try this by using explode function : 你可以使用explode函数试试这个:

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

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

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