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 に答える