1

私は基本的に、大きなテキストのすべての脚注を置き換えようとしています。私がObjective-Cでこれを行っている理由はさまざまですので、その制約を想定してください。

これを持つすべての脚注の存在:[脚注

すべての脚注はこれで終わります:]

これらの2つのマーカーの間には、改行を含め、絶対に何でも存在する可能性があります。ただし、それらの間に]はありません。

したがって、基本的には、[脚注、次に]が一致するまで、]以外のものと一致させたいと思います。

これは、すべての脚注を特定するために私が得ることができた最も近いものです。

NSString *regexString = @"[\\[][F][o][o][t][n][o][t][e][^\\]\n]*[\\]]";

この正規表現を使用すると、780/889の脚注を識別できます。また、これらの780はいずれも誤警報ではないようです。見逃しているように見えるのは、改行が含まれている脚注だけです。

私はwww.regular-expressions.info、特にドットに関するページ(http://www.regular-expressions.info/dot.html)に長い時間を費やしてきました。これは、上記の正規表現を作成するのに役立ちましたが、右角かっこを除いて、文字や改行を含める方法を本当に理解していません。

代わりに次の正規表現を使用すると、すべての脚注をキャプチャできますが、*が貪欲であるため、キャプチャするテキストが多すぎます。(?s)[\\[][F][o][o][t][n][o][t][e].*[\\]]

正規表現が実行されるサンプルテキストは次のとおりです。

  <p id="id00082">[Footnote 1: In the history of Florence in the early part of the XVIth century <i>Piero di Braccio Martelli</i> is frequently mentioned as <i>Commissario della Signoria</i>. He was famous for his learning and at his death left four books on Mathematics ready for the press; comp. LITTA, <i>Famiglie celebri Italiane</i>, <i>Famiglia Martelli di Firenze</i>.—In the Official Catalogue of MSS. in the Brit. Mus., New Series Vol. I., where this passage is printed, <i>Barto</i> has been wrongly given for Braccio.</p>

  <p id="id00083">2. <i>addi 22 di marzo 1508</i>. The Christian era was computed in Florence at that time from the Incarnation (Lady day, March 25th). Hence this should be 1509 by our reckoning.</p>

  <p id="id00084">3. <i>racolto tratto di molte carte le quali io ho qui copiate</i>. We must suppose that Leonardo means that he has copied out his own MSS. and not those of others. The first thirteen leaves of the MS. in the Brit. Mus. are a fair copy of some notes on physics.]</p>

  <p id="id00085">Suggestions for the arrangement of MSS treating of particular subjects.(5-8).</p>

When you put together the science of the motions of water, remember to include under each proposition its application and use, in order that this science may not be useless.--

[Footnote 2: A comparatively small portion of Leonardo's notes on water-power was published at Bologna in 1828, under the title: "_Del moto e misura dell'Acqua, di L. da Vinci_".]

この例では、2つの脚注といくつかの非脚注テキストがあります。ご覧のとおり、最初の脚注には2つの改行が含まれています。2つ目は、改行が含まれていません。

上記の最初の正規表現は、この例のテキストでは脚注2をキャプチャできますが、改行が含まれているため、脚注1はキャプチャされません。

私の正規表現の改善があれば幸いです。

4

1 に答える 1

3

試す

@"\\[Footnote[^\\]]*\\]";

これは改行間で一致する必要があります。単一の文字を文字クラスに入れる必要もありません。

コメントとして、複数行の正規表現(文字列エスケープなし):

\[        # match a literal [
Footnote  # match literal "Footnote"
[^\]]*    # match zero or more characters except ]
\]        # match ]

キャラクタークラス([...])内では、カレット^は異なる意味を持ちます。クラスの内容を無効にします。つまり、またはに[ab]一致しますが、またはを除くすべての文字に一致します。ab[^ab]ab

もちろん、脚注をネストしている場合、これは誤動作します。のようなテキスト[Footnote foo [footnote bar] foo]は最初から。まで一致しbar]ます。これを回避するには、正規表現を次のように変更します

@"\\[Footnote[^\\]\\[]*\\]";

したがって、角かっこを開くことも閉じることもできません。次に、もちろん、最も内側の脚注のみを一致させ、同じ正規表現をテキスト全体に2回(またはネストの最大レベルによってはそれ以上)適用して、レイヤーごとに「ピールバック」する必要があります。

于 2010-12-03T18:59:16.337 に答える