Home > Backend Development > PHP Tutorial > 求一个正则表达式!!!

求一个正则表达式!!!

WBOY
Release: 2016-06-06 20:18:09
Original
1256 people have browsed it

现有如下字符串

<code>(Auth::user()->name,'154555')</code>
Copy after login
Copy after login

需要用正则表达去掉第一个()和所有''
期望结果为

<code>Auth::user()->name,154555</code>
Copy after login
Copy after login

现在倒是有一个,不过,,它把所有的()都去掉了。。。

<code>preg_replace("/[\(\)\\\"\']/", '', $expression)</code>
Copy after login
Copy after login

回复内容:

现有如下字符串

<code>(Auth::user()->name,'154555')</code>
Copy after login
Copy after login

需要用正则表达去掉第一个()和所有''
期望结果为

<code>Auth::user()->name,154555</code>
Copy after login
Copy after login

现在倒是有一个,不过,,它把所有的()都去掉了。。。

<code>preg_replace("/[\(\)\\\"\']/", '', $expression)</code>
Copy after login
Copy after login

<code>$str = "(Auth::user()->name,'154555')";
echo preg_replace("/^\(|\'|\)$/", '', $str);```

括号值针对 开头和结尾 </code>
Copy after login

用 lookahead 和 lookbehind 来捕获由 () 字符 /[\((?!\))(? 。

如果你要捕获的是代码里遇到第一个 () 和所有的 "' 那么只能用两个正则来过滤。

<code>preg_replace("/\'/u", "", preg_replace("/\((.+)\)/", "$1", $str))</code>
Copy after login
Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template