0

我使用一个库来解析 iCalendar 文件,但我不了解拆分属性的正则表达式。
iCalendar 属性有 3 种不同的风格:

BEGIN:VEVENT
DTSTART;VALUE=DATE:20080402
RRULE:FREQ=YEARLY;WKST=MO

该库使用我想理解的这个正则表达式:

var matches:Array = data.match(/(.+?)(;(.*?)=(.*?)((,(.*?)=(.*?))*?))?:(.*)$/);
p.name = matches[1];
p.value = matches[9];                   
p.paramString = matches[2];

谢谢。

4

1 回答 1

5

这是一个可怕的正则表达式! .*并且.*?意味着匹配尽可能多(贪婪)或尽可能少(懒惰)的任何东西。这些只能作为最后的手段。当正则表达式无法匹配输入文本时,使用不当将导致灾难性的回溯。关于这个正则表达式,你只需要了解你不想写这样的正则表达式。

让我展示一下我将如何解决这个问题。显然iCalendar 文件格式是基于行的。每行都有一个属性和一个用冒号分隔的值。该属性可以具有用分号分隔的参数。这意味着属性不能包含换行符、分号或冒号,可选参数不能包含换行符或冒号,并且值不能包含换行符。这些知识使我们能够编写一个使用否定字符类的有效正则表达式:

([^\r\n;:]+)(;[^\r\n:]+)?:(.+)

或在 ActionScript 中:

var matches:Array = data.match(/([^\r\n;:]+)(;[^\r\n:]+)?:(.+)/);  
p.name = matches[1];
p.value = matches[3];
p.paramString = matches[2];

正如 RegexBuddy 所解释的:

Match the regular expression below and capture its match into backreference number 1 «([^\r\n;:]+)»
   Match a single character NOT present in the list below «[^\r\n;:]+»
      Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
      A carriage return character «\r»
      A line feed character «\n»
      One of the characters “;:” «;:»
Match the regular expression below and capture its match into backreference number 2 «(;[^\r\n:]+)?»
   Between zero and one times, as many times as possible, giving back as needed (greedy) «?»
   Match the character “;” literally «;»
   Match a single character NOT present in the list below «[^\r\n:]+»
      Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
      A carriage return character «\r»
      A line feed character «\n»
      The character “:” «:»
Match the character “:” literally «:»
Match the regular expression below and capture its match into backreference number 3 «(.+)»
   Match any single character that is not a line break character «.+»
      Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
于 2010-03-20T05:30:47.887 回答