编辑
这篇文章让我想到了 iCal 格式。
昨天之前,我不知道 iCal 格式是什么。但是,在阅读了 1998 年的规范之后,很明显,这个页面上的任何答案都不足以解析内容。而且,即使对于我下面的一般正则表达式,它也确实太复杂了。
考虑到这一点,这是一个仅解析行内容的解决方案,正如从一般行内容解析的规范中收集的那样。这是朝着正确方向迈出的一步,希望有人能从中受益。它不做续行,也不验证。
C# 代码
Regex iCalMainRx = new Regex(
@" ^ (?<name> [^[:cntrl:]"";:,\n]+ )
(?<parameter>
;
(?<param_name> [^[:cntrl:]"";:,\n]+ )
=
(?<param_value>
(?: (?:[^\S\n]|[^[:cntrl:]"";:,])* | "" (?:[^\S\n]|[^[:cntrl:]""])* "" )
(?: , (?: (?:[^\S\n]|[^[:cntrl:]"";:,])* | "" (?:[^\S\n]|[^[:cntrl:]""])* "" ) )*
)
)*
:
(?<value> (?:[^\S\n]|[^[:cntrl:]])* )
$ ", RegexOptions.IgnorePatternWhitespace);
Regex iCalPvalRx = new Regex(
@" ^ (?<pvals> (?:[^\S\n]|[^[:cntrl:]"";:,])* | "" (?:[^\S\n]|[^[:cntrl:]""])* "" )
(?: ,+ (?<pvals> (?:[^\S\n]|[^[:cntrl:]"";:,])* | "" (?:[^\S\n]|[^[:cntrl:]""])* "" ) )*
$ ", RegexOptions.IgnorePatternWhitespace);
string[] lines = {
"BEGIN:VEVENT",
"CREATED:20120504T163940Z",
"DTEND;TZID=America/Chicago:20120504T130000",
"DTSTAMP:20120504T164000Z",
"DTSTART;TZID=,,,America/Chicago;Next=;last=\"this:;;;:=\";final=:20120504T120000",
"LAST-MODIFIED:20120504T163940Z",
"SEQUENCE:0",
"SUMMARY:Test 1",
"TRANSP:OPAQUE",
"UID:21F61281-FB76-467F-A2CC-A666688BD9B5",
"X-RADICALE-NAME:21F61281-FB76-467F-A2CC-A666688BD9B5.ics",
"END:VEVENT",
};
foreach (string str in lines)
{
Match m_content = iCalMainRx.Match( str );
if (m_content.Success)
{
Console.WriteLine("Key = " + m_content.Groups["name"].Value);
Console.WriteLine("Value = " + m_content.Groups["value"].Value);
CaptureCollection cc_pname = m_content.Groups["param_name"].Captures;
CaptureCollection cc_pvalue = m_content.Groups["param_value"].Captures;
if (cc_pname.Count > 0)
{
Console.WriteLine("Parameters: ");
for (int i = 0; i < cc_pname.Count; i++)
{
// Console.WriteLine("\t'" + cc_pname[i].Value + "' = '" + cc_pvalue[i].Value + "'");
Console.WriteLine("\t'" + cc_pname[i].Value + "' =");
Match m_vals = iCalPvalRx.Match( cc_pvalue[i].Value );
if (m_vals.Success)
{
CaptureCollection cc_vals = m_vals.Groups["pvals"].Captures;
for (int j = 0; j < cc_vals.Count; j++)
{
Console.WriteLine("\t\t'" + cc_vals[j].Value + "'");
}
}
}
}
Console.WriteLine("-------------------------");
}
}
输出
Key = BEGIN
Value = VEVENT
-------------------------
Key = CREATED
Value = 20120504T163940Z
-------------------------
Key = DTEND
Value = 20120504T130000
Parameters:
'TZID' =
'America/Chicago'
-------------------------
Key = DTSTAMP
Value = 20120504T164000Z
-------------------------
Key = DTSTART
Value = 20120504T120000
Parameters:
'TZID' =
''
'America/Chicago'
'Next' =
''
'last' =
'"this:;;;:="'
'final' =
''
-------------------------
Key = LAST-MODIFIED
Value = 20120504T163940Z
-------------------------
Key = SEQUENCE
Value = 0
-------------------------
Key = SUMMARY
Value = Test 1
-------------------------
Key = TRANSP
Value = OPAQUE
-------------------------
Key = UID
Value = 21F61281-FB76-467F-A2CC-A666688BD9B5
-------------------------
Key = X-RADICALE-NAME
Value = 21F61281-FB76-467F-A2CC-A666688BD9B5.ics
-------------------------
Key = END
Value = VEVENT
-------------------------