My first RegExp

Hello!

Why doesn't my program search "Ben"?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <regex>
#include <string>
#include <iostream>

int main()
{
    std::string s = "Hello, my name is Ben. Please visit"
                    "my website at http://forta.com/.";

    std::regex e( "Ben", std::regex_constants::basic );
    std::smatch m;

    while( std::regex_search( s, m, e ) ) {
        std::cout << "Hello" << std::endl;
        std::cout << m[1] << std::endl;
    }

    return 0;
}
Why do you believe your program doesn't work and what do you expect m[1] to contain?
I don't see "Hello" string. But I found why: http://www.cplusplus.com/forum/beginner/134744/

I cannot use std::regex because I have MinGW 4.8 with Qt. I will you VS C++ for studying std::regex and QRegExp in Qt. I will wait when I will be able to use std::regex in Qt too.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <regex>
#include <string>
#include <iostream>

int main()
{
    const std::string s = "Hello, my name is Ben. Please visit "
                          "my website at http://forta.com/.";

    const std::regex e( "Ben", std::regex_constants::basic );
    std::smatch m;

    /*while*/if( std::regex_search( s, m, e ) )  
                 std::cout << "Hello " << m[ /*1*/ 0 ] << '\n' ;
}

http://coliru.stacked-crooked.com/a/936a7f96da2c81d1
Thank you! But this code doesn't work in Qt: http://i7.pixs.ru/storage/7/0/5/377png_8934928_14058705.png
JLBorges, it works fine in VSC++2010! Thank you for showing mistakes and for using const and m[0]!
Topic archived. No new replies allowed.