끔찍한 정규 표현식입니다. .*
및 .*?
은 많은 것을 (탐욕스러운) 또는 몇 가지 (게으른) 것과 일치시키는 것을 의미합니다. 이것들은 최후의 수단으로 사용해야합니다. 부적절한 사용은 정규식이 입력 텍스트와 일치하지 않을 때 catastrophic backtracking이됩니다. 이 정규 표현식에 대해 이해해야 할 필요가있는 모든 정규식을 이와 같이 작성하고 싶지는 않습니다.
어떻게 문제에 접근 할 수 있는지 보여 드리겠습니다. 분명히 iCalendar File Format은 회선 기반입니다. 각 행에는 속성과 콜론으로 구분 된 값이 있습니다. 이 속성은 세미콜론으로 구분 된 매개 변수를 가질 수 있습니다. 이는 속성에 줄 바꿈, 세미콜론 또는 콜론을 포함 할 수 없으며 선택적 매개 변수에 줄 바꿈이나 콜론을 포함 할 수없고 줄 바꿈을 포함 할 수 없음을 의미합니다.
([^\r\n;:]+)(;[^\r\n:]+)?:(.+)
또는 ActionScript의 : 정규식 같아요 그렇게 반발하는지
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) «+»
:로 RegexBuddy에 의해 설명
를이 지식은 우리가 negated character classes를 사용하는 효율적인 정규 표현식을 쓸 수 있습니다 아무도 그것을 설명하고 싶어하지 않는다 : P. 꽤 기본적인 것들입니다. "그룹 포착"과 "욕심이 많지 않음"을 검색하면 알아낼 것입니다. – Kobi