dragon87836215 2019-06-22 05:08
浏览 110
已采纳

在preg_replace()中使用捕获组

I am having a hard time to understand the preg_replace().
This is what I have got so far. preg_replace($patter, $replacement, $string)
But, It confuses me when there is capturing groups in replacement. But still I get some of it. Like below

preg_replace('/(\w+)/', 'hello', 'say hello!')
I know it will result in 'hello hello!'. I can do something more with capturing group. preg_replace('/(\w+)/','\1 Hello', 'Say World!',1)

It will result in 'Say Hello World'.

This is what I don't get.

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

What ${1}1,$3 means?

What's the difference between using $1 \1 and <\1> What does this ${1}1 mean?

  • 写回答

1条回答 默认 最新

  • duanlan2003 2019-06-22 05:13
    关注

    Great question!

    For substitution parts

    My guess is that <\1> and $1 ${1} are pretty much the same, addressing capturing groups, except one of which has an extra <>.

    I think we are using ${1}1 since if we don't, we would have had $11, which does not simply exist, since we don't have that 11 capturing groups, we only have 3 of those, similar story for \11.

    ${1}1,$3 means the first capturing group, ${1}, and the number 1, and third capturing group, $3.

    Inside the Regular Expression

    When we use \1 or \2 inside a regular expression, not as a substitute, it would reference back the prior capturing groups $1 and $2, which is the same as \1 and 2 in the substitute section, which I also think that might create reasonable and valid confusions.

    Please see this demo for back-referencing.

    Reference

    Named Capturing Groups and Backreferences

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

报告相同问题?

悬赏问题

  • ¥15 使用yolov5-7.0目标检测报错
  • ¥15 对于这个问题的解释说明
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备