dougan6982 2013-09-17 07:28
浏览 129
已采纳

为什么我的PHP preg_match匹配第二个子模式而不是第一个?

My PHP code is:

$pattern = '/(Sun|Mon|Tue|Wed|Thu|Fri|Sat)(,| )[\d]{1,2}[A-Z]{1,1}[a-z]{2,2}/';
if (preg_match($pattern, $content, $matches, PREG_OFFSET_CAPTURE)){
    $name = str_replace("(M1) ", "", substr($content, 0, $matches[0][1]));
    $date = substr($content, $matches[0][1], 15);
}

It works fine with one match sub pattern.Like:

  
B'Meadow Alarm Tue,17Sep 19:48 Ur001 General User Closing By User 
name:  B'Meadow Alarm  
date:  Tue,17Sep 19:48

My string is:

(M1) B'Meadow Alarm Tue,17Sep 19:48 Ur001 General User Closing By User (M2) B'Meadow Alarm Tue,18Nov 09:18 Ur001 General User Closing By User

it is supposed to be:

name:  B'Meadow Alarm  
date:  Tue,17Sep 19:48

But the reality result is:

name:  B'Meadow Alarm Tue,17Sep 19:48 Ur001 General User Closing By User (M2) B'Meadow Alarm  
date:  Tue,18Nov 09:18  

Question: What is wrong in my pattern? or code?

----------

Edit

Sorry, guys! One of my colleague changed this regular expression on Server's repository to following one:
'/(.*)(Sun|Mon|Tue|Wed|Thu|Fri|Sat)(,| )[\d]{1,2}[A-Z]{1,1}[a-z]{2,2}(.*)/';

But he doesn't commit to git. That is why I can't see these changes in my local repository. the (.*) cause the problem.

  • 写回答

1条回答 默认 最新

  • duanjiwu0324 2013-09-17 07:38
    关注

    Neither of your pattern or code is wrong. Your code output exactly as supposed to be.

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

报告相同问题?

悬赏问题

  • ¥15 MATLAB怎么通过柱坐标变换画开口是圆形的旋转抛物面?
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥30 用arduino开发esp32控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题