为什么我的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.

我的PHP代码是: p>

  $ pattern ='/  (太阳|星期一|星期二|星期三|星期四|星期五|星期六)(,|)[\ d] {1,2} [AZ] {1,1} [az] {2,2} /'; 
if(  preg_match($ pattern,$ content,$ matches,PREG_OFFSET_CAPTURE)){
 $ name = str_replace(“(M1)”,“”,substr($ content,0,$ matches [0] [1])); \  n $ date = substr($ content,$ matches [0] [1],15); 
} 
  code>  pre> 
 
 

它适用于一个匹配子模式。 喜欢: p>

 
B'Meadow Alarm Tue,17Sep 19:48 Ur001一般用户关闭用户
名称:B'Meadow Alarm 
命令:星期二,17Sep 19:48 
   pre> 
 
 

我的字符串是: p>

 
(M1)B'Meadow Alarm Tue,17Sep 19:48 Ur001一般用户关闭用户(M2)B'  Meadow Alarm Tue,18Nov 09:18 Ur001一般用户关闭用户
  pre> 
 
 

它应该是: p>

 
name:B'Meadow 警报
date:Tue,17Sep 19:48 
  pre> 
 
 

但实际结果是: p>

 
name:B'Meadow Alarm Tue,17Sep  19:48 Ur001一般用户关闭用户(M2)B'Meadow Alarm 
date  :星期二,18Nov 09:18 
  pre> 
 
 

问题:我的模式有什么问题? 或代码? p>

---------- strong> p>

编辑 h2> \ 对不起,伙计们! 我的一位同事将Server的存储库中的这个正则表达式更改为以下一个:
'/(。*)(Sun | Mon | Tue | Wed | Thu | Fri | Sat)(,|)[\ d] { 1,2} [AZ] {1,1} [az] {2,2}(。*)/'; p>

但他不承诺git。 这就是我无法在本地存储库中看到这些更改的原因。 (。*)导致问题。 p> div>

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