Boost logo

Boost :

Subject: [boost] [xpressive] Concatenating two regular expressions into one?
From: Robert Dailey (rcdailey_at_[hidden])
Date: 2012-03-13 18:18:02


I have the following function:

    bool MimeDocument::GetMimeHeaderData( PMIMEENTITY entity,
MimeHeaderData& header_data )
    {
        bool success = false;

        std::string headers;
        GetMimeEntityData( entity, MIME_ENTITY_DATA_HEADERS, headers );

        using namespace boost::xpressive;

        sregex content_disposition_re =
            icase("Content-Disposition:") >> +_s >> (s1=!as_xpr("inline;"))
>> -*_ >> "size=" >> (s2=+_d) >> ';'
            ;

        sregex content_id_re =
            icase("Content-ID:") >> +_s >> '<' >> (s1=+_) >> '>'
            ;

        smatch what;
        if( regex_search( headers, what, content_disposition_re ) ) {
            header_data.is_inline = what[1] == "inline;";
            header_data.size = boost::lexical_cast<unsigned>( what[2] );
            success = true;
        }

        if( regex_search( headers, what, content_id_re ) ) {
            header_data.content_id = what[1];
            success = true;
        }

        return success;
    }

Right now I use 2 sregex objects and search the MIME headers twice to find
2 different lines. Ideally I'd like to just use 1 regex and combine them,
but I need to then somehow make it search between all lines, and then the
order in which Content-Distribution and Content-ID appear in the headers
may vary. Is it best to keep them separated like this, or can I concatenate
the regular expressions into 1?

---------
Robert Dailey


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk