さて、正規表現の忍者。ePub 電子ブック XHTML ファイルの文末脚注にハイパーリンクを追加するパターンを考案しようとしています。問題は、各章内で番号付けが再開されることです。そのため、アンカー名へのリンクをハッシュするために、一意の識別子をアンカー名に追加する必要があります。
次のような(非常に単純化された)リストが与えられた場合:
<h2>Introduction</h2>
<p> 1 Endnote entry number one.</p>
<p> 2 Endnote entry number two.</p>
<p> 3 Endnote entry number three.</p>
<p> 4 Endnote entry number four.</p>
<h2>Chapter 1: The Beginning</h2>
<p> 1 Endnote entry number one.</p>
<p> 2 Endnote entry number two.</p>
<p> 3 Endnote entry number three.</p>
<p> 4 Endnote entry number four.</p>
私はそれを次のようなものに変える必要があります:
<h2>Introduction</h2>
<a name="endnote-introduction-1"></a><p> 1 Endnote entry number one.</p>
<a name="endnote-introduction-2"></a><p> 2 Endnote entry number two.</p>
<a name="endnote-introduction-3"></a><p> 3 Endnote entry number three.</p>
<a name="endnote-introduction-4"></a><p> 4 Endnote entry number four.</p>
<h2>Chapter 1: The Beginning</h2>
<a name="endnote-chapter-1-the-beginning-1"></a><p> 1 Endnote entry number one.</p>
<a name="endnote-chapter-1-the-beginning-2"></a><p> 2 Endnote entry number two.</p>
<a name="endnote-chapter-1-the-beginning-3"></a><p> 3 Endnote entry number three.</p>
<a name="endnote-chapter-1-the-beginning-4"></a><p> 4 Endnote entry number four.</p>
明らかに、書籍の実際のテキスト、つまり各文末脚注がリンクされている場所endnotes.xhtml#endnote-introduction-1
などで、同様の検索が必要になります。
最大の障害は、前の検索が終了した後に各一致検索が開始されることです。そのため、再帰を使用しない限り、複数のエントリに対して同じビット (この場合はタイトル) を一致させることはできません。ただし、再帰を使用した私の試みでは、これまでのところ無限ループしか得られませんでした。
私は TextWrangler の grep エンジンを使用していますが、別のエディター (vim など) で解決策がある場合は、それも問題ありません。
ありがとう!