[boost]algorithm-erase_all_regex , split_regrex & regex_match

在涉及解析TypeB报文时,出现了报文分行的情况(可能分也可能不分,可能在代码过长时,选择分行)。如果单纯是一行行的解析会比较繁琐,因为你不知道这行是不是分行了。但一般分行都是有规律的,因此选择采用boost库中的两个方法:erase_all_regex & split_regrex,头文件 boost/algorithm/string/regex.hpp

        template< 
            typename SequenceT, 
            typename CharT, 
            typename RegexTraitsT>
        inline void erase_all_regex( 
            SequenceT& Input,
            const basic_regex& Rx,
            match_flag_type Flags=match_default )
  • erase_all_regex
    erase_all_regex(string &, regex)
    删除满足regrex的子串,对原串进行修改
    Erase all substrings, matching given regex, from the input. The input string is modified in-place.
      template< 
            typename SequenceSequenceT, 
            typename RangeT,         
            typename CharT, 
            typename RegexTraitsT >
        inline SequenceSequenceT& split_regex(
            SequenceSequenceT& Result,
            const RangeT& Input,
            const basic_regex& Rx,
            match_flag_type Flags=match_default )
  • split_regex

split_regex(container &, string, regex)
以满足regex的字符串作为分隔符,对string进行分割,将结果存在container中。
Tokenize expression. This function is equivalent to C strtok. Input sequence is split into tokens, separated by separators. Separator is an every match of the given regex. Each part is copied and added as a new element to the output container. Thus the result container must be able to hold copies of the matches (in a compatible structure like std::string) or a reference to it (e.g. using the iterator range class). Examples of such a container are \c std::vector or \c std::list

        //1.调整TypeB报文结构,删除掉分行标志
        erase_all_regex(sTYPEB,boost::regex(
        "(\\r\\nSSR [A-Z]{4} [A-Z0-9]{2} ///)"
        "|(\\r\\nOSI [A-Z0-9]{2} ///)"
        ));

        //2.分行
        vector<string> vecLine;
        split_regex(vecLine, sTYPEB, boost::regex("\\r\\n"));  

对SSR TKNE HU ///和 OSI HU ///均进行分割。 或者((正则表达式1)|(正则表达式2))
原:
SSR TKNE MU HK1 SHAPEK5101Y26DEC-1ZHUYINGXIANG
SSR TKNE MU ///.7818757832922C1
OSI MU LNNM @@VlS&Oi@@
OSI MU ///1ZHUYINGXIANG
分割后:
SSR TKNE MU HK1 SHAPEK5101Y26DEC-1ZHUYINGXIANG.7818757832922C1
OSI MU LNNM @@VlS&Oi@@1ZHUYINGXIANG

  • regex_match

匹配
regex_match(string, regex)
验证string是否满足regex

template <class charT, class traits>
inline bool regex_match(const charT* str, 
                        const basic_regex& e, 
                        match_flag_type flags = match_default)

头文件boost\regex\v4\regex_match.hpp

if (boost::regex_match(curLine.c_str(), boost::regex("OSI [A-Z0-9]{2} LNNM.*")))

筛选OSI MU LNNM @@VlS&Oi@@1ZHUYINGXIANG

你可能感兴趣的:(C++)