doumu1212 2009-11-24 16:19
浏览 21
已采纳

在PHP中基于正则表达式执行子字符串操作

In Python, I can do substring operations based on a regex like this.

rsDate = re.search(r"[0-9]{2}/[0-9]{2}/[0-9]{4}", testString)
filteredDate = rsDate.group()
filteredDate = re.sub(r"([0-9]{2})/([0-9]{2})/([0-9]{4})", r"\3\2\1", filteredDate)

What's the PHP equivalent to this?

  • 写回答

3条回答 默认 最新

  • dou12754 2009-11-24 16:24
    关注

    You could simply use the groups to build your filteredDate :

    $groups = array();
    if (preg_match("([0-9]{2})/([0-9]{2})/([0-9]{4})", $testString, $groups))
        $filteredDate = sprintf('%s%s%s', $groups[3], $groups[2], $groups[1]);
    else
        $filteredDate = 'N/A';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?