boost regex

I need a boost regex search example.
first time I use boost lib. I am not able to use it.
so I need your help.
I just copy and past the code from http://www.informit.com/articles/article.aspx?p=517609&seqNum=3
to C::B

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <boost/regex.hpp>

using namespace std;

void testMatch(const boost::regex &ex, const string st) {
 cout << "Matching " << st << endl;
 if (boost::regex_match(st, ex)) {
  cout << " matches" << endl;
 }
 else {
  cout << " doesn’t match" << endl;
 }
}

void testSearch(const boost::regex &ex, const string st) {
 cout << "Searching " << st << endl;
 string::const_iterator start, end;
 start = st.begin();
 end = st.end();
 boost::match_results<std::string::const_iterator> what;
 boost::match_flag_type flags = boost::match_default;
 while(boost::regex_search(start, end, what, ex, flags))
 {
  cout << " " << what.str() << endl;
  start = what[0].second;
 }
}

int main(int argc, char *argv[])
{
 static const boost::regex ex("[Rr]eg...r");
 testMatch(ex, "regular");
 testMatch(ex, "abc");
 testMatch(ex, "some regular expressions are Regxyzr");
 testMatch(ex, "RegULarexpressionstring");
 testSearch(ex, "regular");
 testSearch(ex, "abc");
 testSearch(ex, "some regular expressions are Regxyzr");
 testSearch(ex, "RegULarexpressionstring");
 return 0;
}


but I get two error

G:\c-pro\Untitled1.o:Untitled1.cpp:(.text$_ZN5boost9re_detail27cpp_regex_traits_char_layerIcEC2ERKNS0_21cpp_regex_traits_baseIcEE[__ZN5boost9re_detail27cpp_regex_traits_char_layerIcEC2ERKNS0_21cpp_regex_traits_baseIcEE]+0x58)||undefined reference to `boost::re_detail::cpp_regex_traits_char_layer<char>::init()'|
G:\c-pro\Untitled1.o:Untitled1.cpp:(.text$_ZN5boost9re_detail11raw_storage6extendEj[__ZN5boost9re_detail11raw_storage6extendEj]+0x41)||undefined reference to `boost::re_detail::raw_storage::resize(unsigned int)'|
||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 18 second(s)) ===|


I download boost_1_57_0 today.
what to do;
The boost regex library is not a 'header only' library; it needs to be built.

STL's MinGW build comes bundled with pre-built boost libraries: http://nuwen.net/mingw.html

To make it work with CodeBlocks: http://www.cplusplus.com/forum/beginner/149018/#msg780212

In addition, you need to link with libboost_regex.a and perhaps libboost_system.a

Add these two libraries to: Settings=>Compiler=>Linker settings=>Link libraries


Or enable C++11 support and use the standard C++ regular expressions library:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <regex>

using namespace std;

void testMatch( const std::regex &ex, const string st )
{
    cout << "Matching " << st << endl;
    if ( std::regex_match( st, ex ) )
        {
            cout << " matches" << endl;
        }
    else
        {
            cout << " doesn't match" << endl;
        }
}

void testSearch( const std::regex &ex, const string st )
{
    cout << "Searching " << st << endl;
    string::const_iterator start, end;
    start = st.begin();
    end = st.end();
    std::match_results<std::string::const_iterator> what;
    std::regex_constants::match_flag_type flags = std::regex_constants::match_default; // ****
    while( std::regex_search( start, end, what, ex, flags ) )
        {
            cout << " " << what.str() << endl;
            start = what[0].second;
        }
}

int main()
{
    static const std::regex ex( "[Rr]eg...r" );
    testMatch( ex, "regular" );
    testMatch( ex, "abc" );
    testMatch( ex, "some regular expressions are Regxyzr" );
    testMatch( ex, "RegULarexpressionstring" );
    testSearch( ex, "regular" );
    testSearch( ex, "abc" );
    testSearch( ex, "some regular expressions are Regxyzr" );
    testSearch( ex, "RegULarexpressionstring" );
    return 0;
}

http://coliru.stacked-crooked.com/a/278ecb8696224470
I have C::B with MinGW and boost 1_57_0.
where to find
libboost_regex.a and perhaps libboost_system.a.
thank you it help me lot to setup boost lib.
Topic archived. No new replies allowed.