0

したがって、この質問は、C++を除いてほぼすべての言語で行われているようです。テキストノード内にいくつかのbbcodeが保存されているXMLドキュメントがあります。私はそれを削除するための最良の方法を探しています。誰かが事前に構築されたライブラリまたはこれを自分で達成するための効率的な方法を知っているかどうかを確認するためにここをチェックすると思いました。'['と']'の文字の間にあるものを削除することを考えていましたが、BBのインスタンスの多くがフォームに含まれているため、提供されたXMLドキュメントを使用すると非常識になります'[[blahblahblah]]''[blahblahblah].'

これがXMLドキュメントです。<text>タグ間のすべてのデータが文字列に追加されます、何か提案はありますか?

<mediawiki xmlns="http://www.mediawiki.org/xml/export-0.7/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mediawiki.org/xml/export-0.7/ http://www.mediawiki.org/xml/export-0.7.xsd" version="0.7" xml:lang="en">
 <page>
   <title>Human Anatomy/Osteology/Axialskeleton</title>
   <ns>0</ns>
   <id>181313</id>
   <revision>
      <id>1481605</id>
      <parentid>1379871</parentid>
      <timestamp>2009-04-26T02:03:12Z</timestamp>
      <contributor>
          <username>Adrignola</username>
          <id>169232</id>
      </contributor>
      <minor />
      <comment>+Category</comment>
      <sha1>hvxozde19haz4yhwj73ez82tf2bocbz</sha1>
      <text xml:space="preserve"> [[Image:Axial_skeleton_diagram.svg|thumb|240px|right|Diagram of the axial skeleton]]

       The Axial Skeleton is a division of the human skeleton and is named because it makes up the longitudinal ''axis'' of the body. It consists of the skull, hyoid bone, vertebral column, sternum and ribs. It is widely accepted to be made up of 80 bones, although this number varies from individual to individual.

       [[Category:{{FULLBOOKNAME}}|{{FULLCHAPTERNAME}}]]</text>
   </revision>
  </page>
  <page>
    <title>Horn/General/Fingering Chart</title>
    <ns>0</ns>
    <id>23346</id>
    <revision>
        <id>1942387</id>
        <parentid>1734837</parentid>
        <timestamp>2010-10-02T20:21:09Z</timestamp>
        <contributor>
            <username>Nat682</username>
            <id>144010</id>
        </contributor>
        <comment>added important note</comment>
        <sha1>lana7m8m9r23oor0nh24ky45v71sai9</sha1>
        <text xml:space="preserve">{{HornNavGeneral}}
     The horn spans four plus octaves depending on the player and uses both the treble and bass clefs. In this chart it is assumed the player is using a double-horn with F and Bb sides. The number 1 indicates that the index-finger valve should be depressed, the number 2 indicates that the middle-finger valve should be depressed and the number 3 indicates that the ring-finger valve should be depressed. There are eight possible valve combinations among the first, second and third valves: 0, 1, 2, 3, 1-2, 1-3, 2-3, and 1-2-3. However, there are effectively seven combinations, because 1-2 will produce the same notes, perhaps slightly out of tune, as 3 alone. One depresses the thumb key to use the Bb side of the horn.
    [[Image:Fingering chart.png]]
    [[Category:Horn]]</text>
    </revision>
  </page>
</mediawiki>

したがって、各<page>タグの下部を見ると、次[[Category:{{FULLBOOKNAME}}|{{FULLCHAPTERNAME}}]]のようなものが表示されます。これは、Imが削除しようとしているものです。

4

1 に答える 1

2

データは、読み取り可能なイテレータの形式で提供されると想定します。の形式で取得している場合は、std::string読み取り可能なイテレータを取得するのは非常に簡単です。

その場合、必要なのはブーストfilter_iteratorです:http ://www.boost.org/doc/libs/1_39_0/libs/iterator/doc/filter_iterator.html

必要なフィルター機能は非常に簡単です。あなたはあなたが見た数を追跡し、[あなたが見た数を差し引きます](0で止まります)。あなたのカウントが正である間、あなたはキャラクターを除外します。

を使用できないがboost、から取得している場合std::stringは、少し注意が必要です。しかし、ほんの少し。 std::copy_if動作します。

C ++ 11を使用している場合、ラムダを使用するとこれが非常に簡単になります。そうでない場合は、sをカウントする独自のファンクターを作成する必要があります[

単純なケースの具体例として、あなたはaを与えられており、区切られたコンテンツなしstd::stringでを生成したいと考えています。std::string[]

struct SquareBracketStripper
{
  enum { open_bracket = '[', close_bracket = ']' };
  size_t count;
  SquareBracketStripper():count(0) {}
  bool operator()(char c)
  {
    bool skip = (count > 0) || c == open_bracket;
    if (c == open_bracket) {
      ++count;
    } else if (c== close_bracket && count > 0) {
      --count;
    }
    return skip;
  }
};

std::string FilterBBCode( std::string input ) {
  input.erase(input.end(), std::remove_if( input.begin(), input.end(), SquareBracketStripper() ) );
  return input;
}

ネストされた[]sの任意の深さを処理します。

文字列全体をメモリにロードする必要がないというfilter_iterator点で役立ちます。これは、入力がどの程度不正であるかわからない場合に役立ちます。データをストリーミングしてその場でフィルタリングを実行できる場合は、フィルターで除外するためだけにディスクからメモリに数テラバイトのデータをロードする[]必要はありません。しかし、あなたのユースケースは本当に気にしないかもしれません。

于 2012-11-24T03:49:00.097 に答える