Php 获取日期之间的所有文本

Php 获取日期之间的所有文本,php,regex,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. !!!请注意,这是一个单行字符串 所需输出如下: $string

到目前为止,我有一个两难的问题,那就是我如何才能将下面这一行分成两个不同的变量:

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(约翰·多伊9):嗨,请继续施工02/02/12

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

我正在使用的代码:

$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";
}

您可以使用前瞻查找以下日期,而无需将其包含在匹配中。像这样的方法应该会奏效:

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

多么复杂的正则表达式啊

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

这个简单的正则表达式应该会对您有所帮助。

尝试了上述正则表达式,但未能成功捕获日期:结果:06:52:54(JANE.DOE.2):您好,先生,这是请求您批准正在进行的构造。12:19:41(约翰·多伊9):嗨,请继续construnction@Kevin,字符串已拆分。你所需要做的就是自己构造$matches。你能解释一下为什么它是这样建造的吗?
$regex = '/(\d\d\/\d\d\/\d\d)(.*)(\d\d\/\d\d\/\d\d)(.*)/';