我有以下文字正則表達式重複子表達式
<pattern name="pattern1"/>
<success>success case 1</success>
<failed> failure 1</failed>
<failed> failure 2</failed>
<unknown> unknown </unknown>
<pattern name="pattern4"/>
<pattern name="pattern5"/>
<success>success case 3</success>
<pattern name="pattern2"/>
<success>success case 2</success>
<otherTag>There are many other tags.</otherTag>
<failed> failure 3</failed>
<pattern name="pattern3"/>
<unknown>unkown</unknown>
而正則表達式匹配<failed>[\w|\W]*?</failed>
所有的線路包含失敗的標籤。
如果我希望所有失敗的標籤和失敗標籤上方的模式標籤,我需要做些什麼。如果模式標籤下沒有失敗標籤,那麼模式標籤不應該匹配? 基本上,我想下面的輸出:
<pattern name="pattern1"/>
<failed> failure 1</failed>
<failed> failure 2</failed>
<pattern name="pattern2"/>
<failed> failure 3</failed>
我在JavaScript這樣做,我不介意做一些中間步驟。
編輯開始 幾乎所有repliers建議我採取不同的方法。我不確定我應該採取哪種方法。 JQuery,正則表達式或其他。爲了做出更好的決策,我在此提供更多信息。數據格式會改變,但不會經常改變。該數據是從文件類型「.SVRL」的Schematron的validition報告的文件的結構是在使用‘RELAX NG compact syntax’
schematron-output = element schematron-output {
attribute title { text }?,
attribute phase { xsd:NMTOKEN }?,
attribute schemaVersion { text }?,
human-text*,
ns-prefix-in-attribute-values*,
(active-pattern,
(fired-rule, (failed-assert | successful-report)*)+)+
}
映射到主動模式中定義的以下架構,並且匹配失敗斷言和成功報告。
現在有了更多的信息,我應該採取哪種方法? 非常感謝您的幫助。 :)
編輯結束
請參閱[RegEx匹配除XHTML自包含標記以外的開放標記](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags)和[XML用JavaScript解析](http://stackoverflow.com/questions/649614/xml-parsing-in-javascript)。大多數JavaScript環境都很好地支持XML解析。你不想使用正則表達式。 – 2010-06-15 03:37:58
在附註中,'|'並不是指字符類中的「或」,它只是匹配'|'。無論如何,「或」都隱含在角色類中; '[\ w \ W]'表示「一個字符或一個非字符」。 – 2010-06-15 05:06:03
@Matthew:非常感謝您的建議,我會評估您的選擇。 @Alan:非常感謝您指出「|」在角色類中並不意味着「或」。 :) – 2010-06-15 21:17:33