从字符串中删除BBcode

时间:2012-11-24 03:32:13

标签: c++ xml string bbcode

所以看起来这个问题几乎被问到了太阳下的每一种语言......除了在C ++中。我有一个XML文档,其中有一些bbcode存储在文本节点中。我正在寻找删除它的最佳方法,我想我会在这里查看是否有人知道一些预先建立的库或一些有效的方法来完成这个。我在考虑删除任何介于'['和''''字符之间的内容,但是,使用提供给我的XML文档会让我感到疯狂,因为BB的许多实例都是{{1和一些'[[blahblahblah]]'

这是XML文档。 '[blahblahblah].'标签之间的所有数据都会添加到字符串中,有任何建议吗?

<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>之类的内容,这就是我想删除的内容。

1 个答案:

答案 0 :(得分:1)

我假设数据是以您可以读取的迭代器的形式提供给您的。如果你以std::string的形式获得它,那么获取一个你可以阅读的迭代器非常容易。

在这种情况下,您想要的是提升filter_iteratorhttp://www.boost.org/doc/libs/1_39_0/libs/iterator/doc/filter_iterator.html

您想要的过滤功能非常简单。您可以跟踪您看到的[个数量,并减去您看到的]个数(停在0处)。当你的数量是正数时,你会过滤掉这个角色。

如果你不能使用boost,但是你是从std::string获得的,那么,这有点棘手。但只是一点点。 std::copy_if有效。

如果您使用的是C ++ 11,那么lambda非常容易。如果没有,你将不得不编写自己的算符[

作为一个简单案例的一个具体示例:您正在接受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;
}

处理嵌套[]的任意深度。

filter_iterator有助于您永远不必将整个字符串加载到内存中,如果您不知道输入的格式错误,这将非常有用。当您可以流式传输内容并动态进行过滤时,不需要将几TB的数据从磁盘加载到内存中以过滤掉[]。但是你的用例可能并不在乎。