douwu7563 2014-01-19 07:36
浏览 72
已采纳

美元符号后的数字正则表达式

I have found out a code below

<?php $string = 'April 15, 2003'; 
$pattern = '/(\w+) (\d+), (\d+)/i'; 
$replacement = '${1}1,$3';

I just want to know why this 1 after ${1} is declaired and why the value 3 is assigned before the end of the string symbol ?

  • 写回答

1条回答 默认 最新

  • dongsisui7562 2014-01-19 08:51
    关注

    If you execute the code as it is, you get:

    April1,2003
    

    This is because you are delimiting the variable $1 to separate it from what would be $11. Here's more on that.

    If you change it, you get:

    ,2003
    

    ...because backreference $11 doesn't exist.

    Why is $3 declared before the ending string symbol? Because it's part of the preg_replace backreference. If you move it outside of the replacement string, you'll see it crash and burn.

    Footnote: why you're replacing this with <variable>1,<variable> seems really odd. That 1 is static - in that it never changes. I'd be more inclined to think you'd want a replacement like ${1}${2},$3 which would return April15,2003.

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

报告相同问题?

悬赏问题

  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线