Boost Tokenizer 使用介绍

Boost Tokenizer 使用介绍

-------------------------

1. 介绍

Boost Tokenizer提供了一种把字符序列转换成一组Token的能力,当然,你也可以定义TokenizerFunction来自定义序列的切分符号,如果不指定,默认是以空格为分割,去掉一些标点符号。


2. 几个简单的例子


下面是一个简单的例子:
// simple_example_1.cpp
#include<iostream>
#include<boost/tokenizer.hpp>
#include<string>


int main(){
   using namespace std;
   using namespace boost;
   string s = "This is,  a test";
   tokenizer<> tok(s);
   for(tokenizer<>::iterator beg=tok.begin(); beg!=tok.end();++beg){
       cout << *beg << "\n";
   }
}

结果如下:
This
is
a
test

这里已经过滤了标点符号。

下面是一个以字符步长来进行分割的例子:
// simple_example_3.cpp
#include<iostream>
#include<boost/tokenizer.hpp>
#include<string>


int main(){
   using namespace std;
   using namespace boost;
   string s = "12252001";
   int offsets[] = {2,2,4};   // 这里指定了三个步长
   offset_separator f(offsets, offsets+3);
   tokenizer<offset_separator> tok(s,f);
   for(tokenizer<offset_separator>::iterator beg=tok.begin(); beg!=tok.end();++beg){
       cout << *beg << "\n";
   }
}

结果如下:
12
23
2001

3. 什么是TokenizerFunction

TokenizerFunction是一个用于查询符合匹配要求的token,目前提供了三种TokenizerFunction模板,
   × escaped_list_separator 主要用于解析csv格式的字符串
             explicit escaped_list_separator(Char e = '\\', Char c = ',',Char q = '\"')
             escaped_list_separator(string_type e, string_type c, string_type q):
   × offset_separator 主要用于解析基于特定步长的要求
template<typename Iter>
offset_separator(Iter begin,Iter end,bool bwrapoffsets = true, bool breturnpartiallast = true)
   × char_separator 主要是用于解析基于特定字符分割的需求
explicit char_separator(const Char* dropped_delims,
                        const Char* kept_delims = "",
                        empty_token_policy empty_tokens = drop_empty_tokens)

4. 一个简单的解析/etc/passwd的例子
/**
 * @auth lemo.lu
 * @date 2011.11.03
 *
 * example of Boost tokenizer template usage,This example uses delimiter
 * separator. 
 */


// stl header
#include <iostream>                  // iostream
#include <string>                    // string
#include <fstream>                   // ifstream


// boost
#include <boost/tokenizer.hpp>       // boost Tokenizer


int main(){
    std::ifstream passwdFile;
    passwdFile.open("/etc/passwd",std::ifstream::in);


    // store password line
    char passwdString[256];
    
    typedef boost::tokenizer<boost::char_separator<char> > passwdTokenizer;
    // set a TokenizerFunction , dropped delimiters ":" and keep delimiters ""
    boost::char_separator<char> tokenSep(":", "", boost::keep_empty_tokens);
    // passwd format information
    static const char* passwd_st[] = { "Account","password","UID","GID","GECOS","Dir","Shell"
    };


    // iterator the passwd file
    while(passwdFile.good())
    {
        // get line
        passwdFile.getline(passwdString,256);
        passwdTokenizer tok(std::string(passwdString), tokenSep);
        int passwd_c = 0;
        for(passwdTokenizer::iterator curTok=tok.begin(); curTok!=tok.end(); ++curTok)
            std::cout << passwd_st[passwd_c++] << ":" << *curTok  << std::endl;
        std::cout << "---------------------" << std::endl;
    }passwdFile.close();}


部分结果如下:

Account:root
password:x
UID:0
GID:0
GECOS:root
Dir:/root
Shell:/bin/bash
---------------------
Account:daemon
password:x
UID:1
GID:1
GECOS:daemon
Dir:/usr/sbin
Shell:/bin/sh
---------------------

5. 参考

http://www.boost.org/doc/libs/1_47_0/libs/tokenizer/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值