douzhuoxia0587 2008-09-10 13:04
浏览 33
已采纳

这个PHP正则表达式有什么问题?

$output = preg_replace("|(/D)(/s+)(/d+)(;)|", "//1,//3;", $output);

I'm trying to replace all alphabetical character followed by one or more whitespace characters (tabs and/or spaces) followed by one or more numerical characters followed by a semicolon with the alphabetical character followed by a comma followed by the numerical digits and then the semicolon.

I'll provide an example:

Start:

hello world      1007;

End:

hello world,1007;
  • 写回答

3条回答 默认 最新

  • duanfei1268 2008-09-10 13:10
    关注

    The two | at the start and end probably are incorrect - and should both be forward-slashes.

    All other forward slashes should be backward slashes (and need escaping).

    And since PHP 4.04 $n is the preferred way of referring to a capture group.

    $output = preg_replace("/(\\D)\\s+(\\d+;)/", "$1,$2", $output);
    

    If you use single quotes you don't need to escape your backslashes:

    $output = preg_replace('/(\D)\s+(\d+;)/', '$1,$2', $output);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大