到目前为止,我在如何将下面的行划分为两个不同的变量时遇到了一个两难的问题:
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 :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";
}发布于 2012-03-16 07:30:32
您可以使用前瞻性来查找下面的日期,而不需要将其包括在匹配中。像这样的事情应该有效:
$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);发布于 2012-03-16 07:34:22
什么是复杂的判断力!
$regex = '/(\d\d\/\d\d\/\d\d)(.*)(\d\d\/\d\d\/\d\d)(.*)/';这个简单的正则表达式应该对你有帮助。
https://stackoverflow.com/questions/9733334
复制相似问题