douyi5822 2012-09-07 00:53
浏览 9
已采纳

RegEx在两种情况下都使用一个?

Okay well, here's the string

$string = "123456789_some_0.png";

i currently use preg_match to get the " 123456789 " using the following pattern :

$pattern = "/[0-9]*/i";

well, there are 2 formats for the string, i want to come up with the same result for this case :

$string = "1234-123456789_some_0.png";

and to come up with " 12345789 " and only from both cases, how to do it ?

  • 写回答

1条回答 默认 最新

  • doutong4088 2012-09-07 01:00
    关注

    Assuming you want to capture all digits that are followed by an underscore, you could use the following:

    $strings = array("1234-123456789_some_0.png", "123456789_some_0.png");
    foreach ($strings as $string) {
      preg_match("/([0-9]+)_/", $string , $matches);
      echo $matches[1], PHP_EOL; // 123456789
    }
    

    DEMO.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题