2013-07-17 35 views
2

我有它的HTML大的文本文件,我想以后每隔“</b>”添加一个空格(粗体的每一个字后)检测文本的特定字符串的文件

文本lenght约581 810

,我不知道如何正确地做到这一点,我想试试这个:

1,创建一个名为“v”串矢量

2,获取文本的每一个字符(不知道如何做到这一点,我可以得到线条和文字,但我不知道如何获得特征cters)在此向量(与推背和另一个字符串)

3-检测每一个“</b>”与“为”像这样的循环:

for(int i = 0; i < 581810; i++) 
{ 
    if (v[i] + v[i+1] + v[i+2] + v[i+3] == "</b>"){ 

     // add a space after </b> (don't know how to this) 

    } 
} 

,但我不知道让每一个单在我的字符串向量中的字符,我知道如何获得线条,getline和带有“>>”的单词。我无法用语言做,因为HTML标签贴的话

感谢

+0

您是否必须使用C++? – nouney

+0

是否有任何特定的原因,你想在C++中做到这一点,或者你只是在寻找一个解决方案? – jpw

+0

你需要一个漂亮的解决方案,并不假设字符只有581810. – turnt

回答

0

http://ideone.com/KZsyc6

没有做任何幻想(正则表达式,shell命令),你可以做这样的事情:

const std::string bTag("</b>"); 
std::string line; 
size_t indexOfBTag=0; 
for(...) //iterate through your file, line by line 
{ 
    //populate line with current line from file via getline, up to you 

    //store position of the b tag into indexOfBTag and make sure that theres a b tag in your line 
    //make sure to search starting after the last BTag found, or this loop will never end 
    //however, if the index is 0 (meaning the first search), dont bother adding the size 
    //hence the find(..., indexOfBTag > 0 ? indexOfBTag + bTag.size() : 0) 
    while((indexOfBTag = line.find(bTag, indexOfBTag > 0 ? indexOfBTag + bTag.size() : 0)) != std::string::npos) { 
    line.insert(line.begin() + indexOfBTag + bTag.size(), ' '); 
    } 
} 
相关问题