The (www|www3|ww2)
pattern contains an unanchored alternation group. Since the www
, the first branch, matches www
in www3testabc
, the www3
is not even tested against, the regex grabs www
and removes it. Thus, the number remains.
See Remember That The Regex Engine Is Eager.
You need to sort the alternatives from the longest to shortest (like ^(www3|www2|www)
), or, in your case, it is much more convenient to match www
at the start of the string with 0+ digits and use
$str = "www3testabc";
$str = mb_ereg_replace("^www[0-9]*", "", $str);
echo $str;
See the PHP demo.
NOTE You may use a preg_replace
, too:
$str = preg_replace("/^www[0-9]*/u", "", $str);
The /^www[0-9]*/u
regex will remove www
and then any 0+ digits and will correctly handle Unicode input due to /u
UNICODE modifier.
Note that if you have no control over the www
, www2
etc. strings, and you build the pattern dynamically, you need to sort the strings by length in a descending order and then implode
.