正则去除括号
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
$str = "你好<我>(爱)[北京]{天安门}" ;
echo f1( $str ); //返回你好
echo f2( $str ); //返回我
echo f3( $str ); //返回爱
echo f4( $str ); //返回北京
echo f5( $str ); //返回天安门
function f1( $str )
{
$result =
array ();
preg_match_all( "/^(.*)(?:<)/i" , $str , $result );
return $result [1][0];
}
function f2( $str )
{
$result =
array ();
preg_match_all( "/(?:<)(.*)(?:>)/i" , $str , $result );
return $result [1][0];
}
function f3( $str )
{
$result =
array ();
preg_match_all( "/(?:()(.*)(?:))/i" , $str , $result );
return $result [1][0];
}
function f4( $str )
{
$result =
array ();
preg_match_all( "/(?:[)(.*)(?:])/i" , $str , $result );
return $result [1][0];
}
function f5( $str )
{
$result =
array ();
preg_match_all( "/(?:{)(.*)(?:})/i" , $str , $result );
return $result [1][0];
}
|
PS: (?:字符) 表示不捕获这个字符。貌似PHP不支持将字符换成括号。
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。
- 上一篇: 正则表达式(括号)、[中括号]、{大括号}的区别小结
- 下一篇: 正则去除包裹的大括号