1

(word経由で)htmlに変換されたwordドキュメントの見出しタグからすべてのデータを抽出しようとしています

私は次の正規表現を持っています:

<(?<Class>h[5|6|7|8])>(?<ListIdentifier>.*?)<span style='font:7.0pt "Times New Roman"'>(?:&nbsp;)+.+</span>(?<Text>.*?)(?:</h[5|6|7|8]>)?

私の原文は次のようになります

<h5>(1)<span style='font:7.0pt "Times New Roman"'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span>The Scheme (planning scheme) has been
prepared in accordance with the <i>asdf </i>(the Act)
as a framework for managing development in a way that advances the purpose of
the Act.</h5>

<h5>(2)<span style='font:7.0pt "Times New Roman"'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span>In seeking to achieve this purpose, the planning scheme sets out
the future development in the
planning scheme area over the next 20 years.</h5>

<h5>(3)<span style='font:7.0pt "Times New Roman"'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span>While the planning scheme has been prepared with a 20 year horizon, it
will be reviewed periodically in accordance with the Act to ensure that it
responds appropriately to the changes of the community at Local, Regional and State
levels.</h5>

正規表現は機能しているように見えますが、最初のh5から最後のh6 | 7|8までキャプチャします。

私はここでデータを複雑にするために何もしようとはしていません。単純な抽出が必要なので、htmlパーサーを使用するのではなく、正規表現を使用したいと思います。私の例では、見出しは適切であると言っても過言ではありません。形成された、すなわち。hXは常にhYではなくhXによって閉じられ、見出しには見出しやそのようなファンキーなものはありません。

?を追加しようと思いました (?:)の終わりまでは、それを貪欲ではないので、最初のインスタンスにのみ一致し、可能な限り多くは一致しません。貪欲がどのように機能するかについて、ここで何かが欠けていますか?

編集:

正規表現

<(?<Class>h[5-8])>(?<ListIdentifier>.*?)<span style='font:7.0pt "Times New Roman"'>(?:&nbsp;)+.+?</span>(?<Text>.*?)(?:</h[5-8]>)

も一致するようです

<h6>&nbsp;</h6>

<h6>&nbsp;</h6>

<h6>&nbsp;</h6>

<h6>&nbsp;</h6>

<h5>(1)<span style='font:7.0pt "Times New Roman"'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span>Short Title -The planning scheme policy may be cited as PSP No 2. –
Engineering Standards – Road and Drainage Infrastructure.</h5>

したがって、テキスト全体が含まれますが、nbspを含むh6はスパンがないため、無視したいと思います。

4

1 に答える 1

2

問題を引き起こしている正規表現の真ん中に貪欲があり.+ます(直前</span>)。これをに変更する.+?と、正規表現が正しく機能するはずです。

文字クラスは(文字間のORを意味する)の[5678]代わりに使用する必要があり、に短縮することもできることに注意してください。[5|6|7|8][5-8]

?また、末尾から末尾を削除する(?:</h[5-8]>)?必要があります(?:</h[5-8]>)。この変更がないと、試合は終了する前に終了します。

編集:現在の正規表現が編集に入力したテキストと一致する理由は、スパンとnbspがその前に表示されていない場合、.*?ListIdentifierグループのがaと一致するためです。これをに変更すること</hX>でこれを修正できるはずです。これは符号よりも小さくは一致しないため、スパンが存在する必要があります。.*?[^<]*

結果:

<(?<Class>h[5-8])>(?<ListIdentifier>[^<]*)<span style='font:7.0pt "Times New Roman"'>(?:&nbsp;)+.+?</span>(?<Text>.*?)(?:</h[5-8]>)
于 2012-02-08T23:06:51.400 に答える