获取日期之间的所有文本

时间:2012-03-16 07:14:25

标签: php regex

我目前面临的困境是如何将下面的行分成两个不同的变量:

03/07/12 12:19:41 (JOHN.DOE.9): Hi, Kindly proceed with the construnction 02/02/12 06:52:54 (JANE.DOE.2): Hi Sir, this is to ask for your approval in the pending construction.

!!!请注意,这是一个单行字符串

所需的输出如下:

$string1 = '03/07/12 12:19:41 (JOHN.DOE.9) : Hi, Kindly proceed with the construction.'
$string2 = '02/02/12 06:52:54 (JANE.DOE.2): Hi Sir, this is to ask for your approval in the pending construction.'

我的代码的当前结果:

  

03/07/12 12:19:41(JOHN.DOE.9):嗨,请继续施工 02/02/12

我们在这里可以看到,我也得到了应该包含在下一个变量中的前一个日期(Jane Doe's)

我正在使用的代码:

$txt='03/07/12 12:19:41 (JOHN.DOE.9): Hi, Kindly proceed with the construnction 02/02/12 06:52:54 (JANE.DOE.2): Hi Sir, this is to ask for your approval in the pending construction.';

$re1='((?:[0]?[1-9]|[1][012])[-:\\/.](?:(?:[0-2]?\\d{1})|(?:[3][01]{1}))[-:\\/.](?:(?:\\d{1}\\d{1})))(?![\\d])';    # MMDDYY 1
$re2='.*?'; # Non-greedy match on filler
$re3='((?:(?:[0-1][0-9])|(?:[2][0-3])|(?:[0-9])):(?:[0-5][0-9])(?::[0-5][0-9])?(?:\\s?(?:am|AM|pm|PM))?)';  # HourMinuteSec 1
$re4='.*?'; # Non-greedy match on filler
$re5='(\\(.*\\))';  # Round Braces 1
$re6='(:)'; # Any Single Character 1
$re7='( )'; # White Space 1   $re8='((?:[a-z][a-z].+?)((?:[0]?[1-9]|[1][012])[-:\\/.](?:(?:[0-2]?\\d{1})|(?:[3][01]{1}))[-:\\/.](?:(?:\\d{1}\\d{1}))))';    # Word 1

if ($c=preg_match_all ("/".$re1.$re2.$re3.$re4.$re5.$re6.$re7.$re8."/is", $txt, $matches))   {
      $mmddyy1=$matches[1][0];
      $time1=$matches[2][0];
      $rbraces1=$matches[3][0];
      $c1=$matches[4][0];
      $ws1=$matches[5][0];
      $word1=$matches[6][0];
      print "$mmddyy1 $time1 $rbraces1 $c1 $ws1 $word1 \n";
}

2 个答案:

答案 0 :(得分:1)

您可以使用前瞻来查找以下日期,而不会在匹配中包含它。这样的事情应该有效:

$re = '~(?<date> \d{2}/\d{2}/\d{2} \s+ \d{2}:\d{2}:\d{2} ) .*? (?= (?&date) | $)~x';
preg_match_all($re, $txt, $matches);

或者您可以在前瞻中拆分日期,并排除第一个空元素。

$array = preg_split('~(?= \d{2}/\d{2}/\d{2} \s+ \d{2}:\d{2}:\d{2} )~x', $txt, 0, PREG_SPLIT_NO_EMPTY);

答案 1 :(得分:1)

什么是复杂的正则表达式!

$regex = '/(\d\d\/\d\d\/\d\d)(.*)(\d\d\/\d\d\/\d\d)(.*)/';

这个简单的正则表达式可以帮到你。