我開發了一個匹配pstops頁面規範的正則表達式。 (正則表達式空格不顯着。)在正則表達式中嵌套命名組,可能導航層次結構?
^(?:(?<modulo>\d+):)?
(?<pages>
(?<pagespec>
(?<pageno>-?\d+)
(?<rotation>[RUL]?)?
(?:@(?<scale>\d*(?:\.\d+)))?
(?:\(
(?<xoff>\d*\.?\d+)(?<xunit>in|cm|w|h)?
,
(?<yoff>\d*\.?\d+)(?<yunit>in|cm|w|h)?
\))?
\+?)+,?
)+$
。
'Sample string:
'"4:[email protected](21cm,0)[email protected](21cm,14.85cm),1L(21cm,0)[email protected](21cm,14.85cm)"
正如你所看到的,有嵌套命名的子組。例如,A pagespec
不需要指定rotation
。我希望能夠做一些這樣的效果:
If match.Groups("pages").Captures(0).Groups("pagespecs").Captures(1).Groups("rotation").Value > ""
當然Captures
的,但沒有Groups
財產。有沒有辦法以這種方式訪問層次結構中的子組?
編輯:這裏是一個更minmal示例(白空間顯著此時間):
(?<paragraph>(?:(?<sentence>The (?<child>boy|girl) is hungry\.|The (?<parent>mother|father) is angry\.)\s*)+)
匹配針對此字符串:
The boy is hungry. The mother is angry. The girl is hungry.
產生一個匹配。在該匹配中,
Groups("paragraph")
有一個匹配整個字符串的捕獲。Groups("sentence")
有三個捕獲。Groups("child")
有兩個捕獲,boy
和girl
。Groups("parent")
有一個捕獲,mother
。
但並沒有什麼,告訴我,對於parent
單捕捉位於第二捕獲內的sentence
,除非我開始看Index
和Length
每次捕捉。
編輯:這裏是最終的答案:
^(?:(?<modulo>\d+):)?
(?<pages>
(?<pagespec>
(?<pageno>-?\d+)
(?<rotation>[RUL]?)
(?:@(?<scale>\d*(?:\.\d+)))?
(?:\(
(?<xoff>\d*\.?\d+)(?<xunit>in|cm|w|h)?
,
(?<yoff>\d*\.?\d+)(?<yunit>in|cm|w|h)?
\))?
(?<pageno>)(?<rotation>)(?<scale>)(?<xoff>)(?<xunit>)(?<yoff>)(?<yunit>)
\+?)+,?
(?<pagespec>)
)+
這推動一個NULL
到每個page
之間的pagespec
棧,因此它們可與可page
相關;和一個NULL
到每個pagespec
之間的其他指定堆棧上。嘖嘖,解析很難...
我想這將有助於問題的用處,如果您將此減少到最小范例。有些東西就像'xy @ z'與'(?:(?:????????????????????????????????????????????[az]) - )+ 「sub」)。捕獲(1)'屬於'Groups(「part」)。捕獲(0)'或'(1)')。但是很好的問題,我也會對解決方案感興趣:)。 –