dsghpgmay31938863 2017-05-02 14:30
浏览 57
已采纳

如何使用正则表达式从另一个字符串的开头删除多个字符串

I need to remove multiple strings like www, ww2, www3 from the beginning of a string.

For example if the string is www3testme then I need to remove the www3 and get the testme only.

Below is the code I'm using but it's not working somehow.

$str = "www3testabc";
$str = mb_ereg_replace("^(www|www3|ww2)", "", $str);
echo $str;

It gives "3testabc" instead of "testabc".

Not sure what I'm doing wrong.

I want to use mb_ereg_replace() function only.

  • 写回答

3条回答 默认 最新

  • duanqin2026 2017-05-02 14:32
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办
  • ¥15 vue2登录调用后端接口如何实现