我正在尝试调试一个正则表达式,对于我的一生,我无法弄清楚为什么它在测试文件中有效,但在其他地方却不起作用。
我收到一个原始的HTTP响应,并解析了感兴趣的部分:
UID:5F12F7DA-10B0-4EE3-820D-B56F0B2FC153
DTSTAMP:20120408T041113Z
CLASS:PUBLIC
CREATED:20120408T041113Z
DESCRIPTION:Testfghfghfghfghfghfghfghfghfghfghfghfghfghfghfgh
DTSTART;TZID=America/New_York:20120410T000000
DTEND;TZID=America/New_York:20120419T010000
LAST-MODIFIED:20120408T041228Z
LOCATION:Philadelphia', PA
SEQUENCE:1
SUMMARY:Test
TRANSP:OPAQUE
每行以 (hex:0D0A) 结尾,应为"''r'"。
以下是摘要的呼吁:
$this->摘要 = ''提取''提取::获取摘要($vevent);
这是函数:
public static function GetSummary($String) {
$re1='.*?'; # Non-greedy match on filler
$re2='(SUMMARY)'; # Word 1
$re3='(:)'; # Any Single Character 1
$re4='(.*?)'; # Non-greedy match on filler
$re5='('r'n)'; # Any Single Character 2
if ($c=preg_match_all ("/".$re1.$re2.$re3.$re4.$re5."/is", $String, $matches))
{
$word1=$matches[1][0];
$c1=$matches[2][0];
$word2=$matches[3][0];
return $word2;
}
}
Preg_match_all在我的测试文件中评估为 true,但在程序中为 false。有什么想法吗?关于它不喜欢的原始字符串的某些内容,或者没有被复制到我的测试文件中(所以我找不到它)?
尝试仅使用换行符作为最终匹配项:
function GetSummary($String) {
$re1='.*?'; // Non-greedy match on filler
$re2='(SUMMARY)'; // Token
$re3='(:)'; // Separator
$re4='(.*?)'; // Content
$re5=''n'; // End of line
if ($c=preg_match_all ("/".$re1.$re2.$re3.$re4.$re5."/is", $String, $matches))
{
$word1=$matches[1][0];
$c1=$matches[2][0];
$word2=$matches[3][0];
return $word2;
}
}
演示:http://ideone.com/zHr2X
如果这仍然不起作用,您可以尝试使用行尾令牌$
,或者在调用正则表达式之前清理行尾。