Boost logo

Boost Users :

Subject: Re: [Boost-users] Regular Expression
From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2009-07-13 12:10:48


AMDG

Wong Peter wrote:
> Hello to all, i want to search a container which copied from file with
> certain regular expression and then display it to stdout;
>
> Here is my code:
>
> [code]
>
> std::cout << "\n\nEnter Search designation : ";
> std::string userDesignation;
> getline(std::cin, userDesignation);
>
> std::ifstream inHandle("C:\\Staff.txt");
> std::vector<std::string> data;
> data.reserve(100);
> std::copy(std::istream_iterator<std::string>(inHandle),
> std::istream_iterator<std::string>(),
> std::back_inserter(data) );
>

This breaks the input file at whitespace. Is this really
what you want?

> boost::regex searchExpression(userDesignation);
> boost::match_results<std::vector<std::string>::iterator > result;
>
> boost::regex_match(data.begin(), data.end(), result, searchExpression);
>
> [/code]
>
> The error is
>
> error C2664: 'char boost::w32_regex_traits<charT>::translate(charT,bool)
> const' : cannot convert parameter 1 from
> 'std::basic_string<_Elem,_Traits,_Ax>' to 'char'
>
> error C2664: 'char boost::w32_regex_traits<charT>::translate(charT,bool)
> const' : cannot convert parameter 1 from
> 'std::basic_string<_Elem,_Traits,_Ax>' to 'char'
>
> I think the error cause by vector string iterator rather than string
> iterator.
>
> Any workaround solution to this problem ?
>

If you really need to have vector<string> instead of just string,
then you need to run regex_search in a loop:

BOOST_FOREACH(const std::string& s, data) {
    boost::regex_match(s, result, search_expression);
}

In Christ,
Steven Watanabe


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net