preg_replace问题
我有一个字符串 The Incredible Hulk (2008) 并使用模式
/^\([0-9]{1,4}\)$/
删除 (2008)。 PHP 代码如下所示:
$x = trim(preg_replace("/^\([0-9]{1,4}\)$/", "", "The Incredible Hulk (2008)"));
结果是:
The Incredible Hulk (2008)
我做错了什么?
I have a string The Incredible Hulk (2008) and use pattern
/^\([0-9]{1,4}\)$/
to remove (2008). PHP code looks like this:
$x = trim(preg_replace("/^\([0-9]{1,4}\)$/", "", "The Incredible Hulk (2008)"));
And the result is:
The Incredible Hulk (2008)
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您正在使用与行首匹配的
^
字符。删除它,它应该可以工作。如果您还想删除
(
之前的空格,则正则表达式变为/\s*\([0-9]{1,4}\)$/
You're using the
^
character that matches start of line. Remove that and it should work.If you also want to get rid of the whitespace before the
(
the regex becomes/\s*\([0-9]{1,4}\)$/
取出“^”。
(2008) 未锚定在字符串的开头。 “^”要求匹配从行首开始。
Take out "^".
The (2008) is not anchored at the start of the string. "^" requires the match to start at the beginning of a line.
^
和$
分别标记整个字符串的开始和结束。删除两者。^
and$
are mark begin and end of the entire string. Remove both.只需删除
^
符号(行首)。(您可能还想删除
$
符号(行尾))有关文档中的 PHP 元字符的更多信息:
http://www.php.net/manual/en/regexp.reference .meta.php
Just remove the
^
sign (beginning of line).(you might want to remove the
$
sign as well (end of line))More info about PHP meta characters in the documentation:
http://www.php.net/manual/en/regexp.reference.meta.php