Reputation: 1672
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?
Upvotes: 0
Views: 131
Reputation: 98901
$result = preg_replace('/\([\d]{4}\)$/', '', 'The Incredible Hulk (2008)');
Upvotes: 0
Reputation: 3712
Just remove the ^
sign (beginning of line).
$x = trim(preg_replace("/\([0-9]{1,4}\)$/", "", "The Incredible Hulk (2008)"));
(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
Upvotes: 1
Reputation: 3395
^
and $
are mark begin and end of the entire string. Remove both.
$x = trim(preg_replace("/\([0-9]{4}\)/", "", "The Incredible Hulk (2008)"));
Upvotes: 1
Reputation: 5475
Take out "^".
$x = trim(preg_replace("/\([0-9]{1,4}\)$/", "", "The Incredible Hulk (2008)"));
The (2008) is not anchored at the start of the string. "^" requires the match to start at the beginning of a line.
Upvotes: 2
Reputation: 33163
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}\)$/
Upvotes: 4