I am trying to write some code to regex search against wide strings. The problem I am having is that the regex operations ( regex_search in particular ) does not take in wide arguements. Here is the snippet I am having trouble with:

Function Header:

void scan_filesystem( wpath const& f,  wregex const& reg, unsigned i = 0 )

Snippet:

        while ( !fileStream.eof() ) {
            vector<wstring> tokens;
            wstring line;
            getline( fileStream, line );
            wistringstream iss( line );
            copy( istream_iterator<wstring, wchar_t, char_traits<wchar_t>>(iss),
                istream_iterator<wstring, wchar_t, char_traits<wchar_t>>(),
                back_inserter< vector<wstring> >(tokens));

            auto it = tokens.cbegin();
            auto end = tokens.cend();
            wsmatch m;

            while ( regex_search( it, end, m, reg ) ){

            }

            int br =0;
        }

Any suggestions on how to regex_search with wide arguements would be greatly appreciated!

For std::regex_search() with iterators, the iterators must be for iterating over a sequence of characters.

Iterator through the vector tokens, pick up each std::wstring and then apply std::regex_search() on the sequence of characters in the string.

#include <fstream>
#include <vector>
#include <string>
#include <boost/regex.hpp> // testing with GCC 4.8, broken std::regex

int main()
{
    std::vector< std::wstring > tokens { L"abcd", L"eacchij", L"kmlnop", L"qrbcccst" } ;

    boost::wregex regex { L"[ab]c+" } ;

    for( const auto& wstr : tokens )
    {
        std::wcout << wstr ;
        boost::wsmatch results ;
        if( regex_search( wstr, results, regex ) ) std::wcout << L": " << results[0] ;
        std::wcout << L'\n' ;
    }
}

Output:

abcd: bc
eacchij: acc
kmlnop
qrbcccst: bccc

That worked! Thank you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.