Help a noob with regex_search problem

I have this code that is not working, im very locked here:
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <iostream>
#include <vector>
#include <string>
#include <windows.h>
#include <algorithm>
#include <iterator>
#include <regex>


typedef match_results<const char*> cmatch; // //////////////////////// ERROR HERE
//template <class outIter>
void find_locs(HANDLE process) {

    unsigned char *p = NULL;
    MEMORY_BASIC_INFORMATION info;

    // VirtualQueryEx does the hard part, telling use about each
    // block in the target process.

    for ( p = NULL;
        VirtualQueryEx(process, p, &info, sizeof(info)) == sizeof(info);
        p += info.RegionSize )
    {
        // buffer to hold copies of memory blocks from the target
        std::vector<char> buffer;
        std::vector<char>::iterator pos;
 
        // We only want committed memory that's mapped or private --
        // screens out things like the code in the target process.
        //
        if (info.State == MEM_COMMIT &&
            (info.Type == MEM_MAPPED || info.Type == MEM_PRIVATE))
        {
            DWORD bytes_read;

            // copy block from target to our buffer, search for pattern:
            buffer.resize(info.RegionSize);
            ReadProcessMemory(process, p, &buffer[0], info.RegionSize, &bytes_read);
            buffer.resize(bytes_read);
            
            const std::tr1::regex rx("([\\w-+]+(?:\\.[\\w-+]+)*@(?:[\\w-]+\\.)+[a-zA-Z]{2,7})");
            std::tr1::cmatch res;

			std::tr1::regex_search(buffer, res, rx); // //////////////////////// ERROR HERE
     

           
            for ( int i=0; i<sizeof(res); ++i)
            {
               
                std::cout << res[i] << std::endl ;
            }
        }
    }
}

int main(int argc, char **argv) {
    if (argc != 2) {
        fprintf(stderr, "Usage: %s <process ID>", argv[0]);
        return 1;
    }

    // Read the target PID and search pattern:
    int pid;
    sscanf_s(argv[1], "%i", &pid);
    //std::string pattern(argv[2]);

    // Open the target process with rights to read its information and memory:
    HANDLE process = OpenProcess(
        PROCESS_VM_READ | PROCESS_QUERY_INFORMATION,
        false,
        pid);

    
    find_locs(process);

    return 0;
}


Compiling with VC++2010 gives this errors, i believe a lot of errors in code.
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
1>main.cpp(10): error C2143: syntax error : missing ';' before '<'
1>main.cpp(10): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>main.cpp(44): error C2784: 'bool std::tr1::regex_search(const std::basic_string<_Elem,_StTraits,_StAlloc> &,const std::tr1::basic_regex<_Elem,_RxTraits> &,std::tr1::regex_constants::match_flag_type)' : could not deduce template argument for 'const std::basic_string<_Elem,_StTraits,_StAlloc> &' from 'std::vector<_Ty>'
1>          with
1>          [
1>              _Ty=char
1>          ]
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\regex(2597) : see declaration of 'std::tr1::regex_search'
1>main.cpp(44): error C2784: 'bool std::tr1::regex_search(const std::basic_string<_Elem,_StTraits,_StAlloc> &,const std::tr1::basic_regex<_Elem,_RxTraits> &,std::tr1::regex_constants::match_flag_type)' : could not deduce template argument for 'const std::basic_string<_Elem,_StTraits,_StAlloc> &' from 'std::vector<_Ty>'
1>          with
1>          [
1>              _Ty=char
1>          ]
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\regex(2597) : see declaration of 'std::tr1::regex_search'
1>main.cpp(44): error C2784: 'bool std::tr1::regex_search(const std::basic_string<_Elem,_StTraits,_StAlloc> &,const std::tr1::basic_regex<_Elem,_RxTraits> &,std::tr1::regex_constants::match_flag_type)' : could not deduce template argument for 'const std::basic_string<_Elem,_StTraits,_StAlloc> &' from 'std::vector<_Ty>'
1>          with
1>          [
1>              _Ty=char
1>          ]
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\regex(2597) : see declaration of 'std::tr1::regex_search'
1>main.cpp(44): error C2784: 'bool std::tr1::regex_search(const std::basic_string<_Elem,_StTraits,_StAlloc> &,std::tr1::match_results<::std::basic_string<_Elem,_StTraits,_StAlloc>::const_iterator,_Alloc> &,const std::tr1::basic_regex<_Elem,_RxTraits> &,std::tr1::regex_constants::match_flag_type)' : could not deduce template argument for 'const std::basic_string<_Elem,_StTraits,_StAlloc> &' from 'std::vector<_Ty>'
1>          with
1>          [
1>              _Ty=char
1>          ]
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\regex(2581) : see declaration of 'std::tr1::regex_search'
1>main.cpp(44): error C2784: 'bool std::tr1::regex_search(const std::basic_string<_Elem,_StTraits,_StAlloc> &,std::tr1::match_results<::std::basic_string<_Elem,_StTraits,_StAlloc>::const_iterator,_Alloc> &,const std::tr1::basic_regex<_Elem,_RxTraits> &,std::tr1::regex_constants::match_flag_type)' : could not deduce template argument for 'const std::basic_string<_Elem,_StTraits,_StAlloc> &' from 'std::vector<_Ty>'
1>          with
1>          [
1>              _Ty=char
1>          ]
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\regex(2581) : see declaration of 'std::tr1::regex_search'
1>main.cpp(44): error C2784: 'bool std::tr1::regex_search(const std::basic_string<_Elem,_StTraits,_StAlloc> &,std::tr1::match_results<::std::basic_string<_Elem,_StTraits,_StAlloc>::const_iterator,_Alloc> &,const std::tr1::basic_regex<_Elem,_RxTraits> &,std::tr1::regex_constants::match_flag_type)' : could not deduce template argument for 'const std::basic_string<_Elem,_StTraits,_StAlloc> &' from 'std::vector<_Ty>'
1>          with
1>          [
1>              _Ty=char
1>          ]
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\regex(2581) : see declaration of 'std::tr1::regex_search'
1>main.cpp(44): error C2784: 'bool std::tr1::regex_search(const _Elem *,std::tr1::match_results<const _Elem*,_Alloc> &,const std::tr1::basic_regex<_Elem,_RxTraits> &,std::tr1::regex_constants::match_flag_type)' : could not deduce template argument for 'const _Elem *' from 'std::vector<_Ty>'
1>          with
1>          [
1>              _Ty=char
1>          ]
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\regex(2566) : see declaration of 'std::tr1::regex_search'
1>main.cpp(44): error C2784: 'bool std::tr1::regex_search(const _Elem *,const std::tr1::basic_regex<_Elem,_RxTraits> &,std::tr1::regex_constants::match_flag_type)' : could not deduce template argument for 'const _Elem *' from 'std::vector<_Ty>'
1>          with
1>          [
1>              _Ty=char
1>          ]
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\regex(2552) : see declaration of 'std::tr1::regex_search'
1>main.cpp(44): error C2782: 'bool std::tr1::regex_search(_BidIt,_BidIt,const std::tr1::basic_regex<_Elem,_RxTraits> &,std::tr1::regex_constants::match_flag_type)' : template parameter '_BidIt' is ambiguous
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\regex(2540) : see declaration of 'std::tr1::regex_search'
1>          could be 'std::tr1::cmatch'
1>          or       'std::vector<_Ty>'
1>          with
1>          [
1>              _Ty=char
1>          ]
1>main.cpp(44): error C2780: 'bool std::tr1::regex_search(_BidIt,_BidIt,std::tr1::match_results<_BidIt,_Alloc> &,const std::tr1::basic_regex<_Elem,_RxTraits> &,std::tr1::regex_constants::match_flag_type)' : expects 5 arguments - 3 provided
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\regex(2528) : see declaration of 'std::tr1::regex_search'
1>
1>Build FAILED.


Somebody know how to fix? Thanks in advance
Is this available to you?
std::tr1::regex_search(buffer.begin(), buffer.end(), res, rx);
tried with that, now throws this error:

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
47
48
1>main.cpp(10): error C2143: syntax error : missing ';' before '<'
1>main.cpp(10): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>main.cpp(44): error C2780: 'bool std::tr1::regex_search(const std::basic_string<_Elem,_StTraits,_StAlloc> &,const std::tr1::basic_regex<_Elem,_RxTraits> &,std::tr1::regex_constants::match_flag_type)' : expects 3 arguments - 4 provided
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\regex(2597) : see declaration of 'std::tr1::regex_search'
1>main.cpp(44): error C2784: 'bool std::tr1::regex_search(const std::basic_string<_Elem,_StTraits,_StAlloc> &,std::tr1::match_results<::std::basic_string<_Elem,_StTraits,_StAlloc>::const_iterator,_Alloc> &,const std::tr1::basic_regex<_Elem,_RxTraits> &,std::tr1::regex_constants::match_flag_type)' : could not deduce template argument for 'const std::basic_string<_Elem,_StTraits,_StAlloc> &' from 'std::_Vector_iterator<_Myvec>'
1>          with
1>          [
1>              _Myvec=std::_Vector_val<char,std::allocator<char>>
1>          ]
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\regex(2581) : see declaration of 'std::tr1::regex_search'
1>main.cpp(44): error C2784: 'bool std::tr1::regex_search(const std::basic_string<_Elem,_StTraits,_StAlloc> &,std::tr1::match_results<::std::basic_string<_Elem,_StTraits,_StAlloc>::const_iterator,_Alloc> &,const std::tr1::basic_regex<_Elem,_RxTraits> &,std::tr1::regex_constants::match_flag_type)' : could not deduce template argument for 'const std::basic_string<_Elem,_StTraits,_StAlloc> &' from 'std::_Vector_iterator<_Myvec>'
1>          with
1>          [
1>              _Myvec=std::_Vector_val<char,std::allocator<char>>
1>          ]
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\regex(2581) : see declaration of 'std::tr1::regex_search'
1>main.cpp(44): error C2784: 'bool std::tr1::regex_search(const std::basic_string<_Elem,_StTraits,_StAlloc> &,std::tr1::match_results<::std::basic_string<_Elem,_StTraits,_StAlloc>::const_iterator,_Alloc> &,const std::tr1::basic_regex<_Elem,_RxTraits> &,std::tr1::regex_constants::match_flag_type)' : could not deduce template argument for 'const std::basic_string<_Elem,_StTraits,_StAlloc> &' from 'std::_Vector_iterator<_Myvec>'
1>          with
1>          [
1>              _Myvec=std::_Vector_val<char,std::allocator<char>>
1>          ]
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\regex(2581) : see declaration of 'std::tr1::regex_search'
1>main.cpp(44): error C2784: 'bool std::tr1::regex_search(const std::basic_string<_Elem,_StTraits,_StAlloc> &,std::tr1::match_results<::std::basic_string<_Elem,_StTraits,_StAlloc>::const_iterator,_Alloc> &,const std::tr1::basic_regex<_Elem,_RxTraits> &,std::tr1::regex_constants::match_flag_type)' : could not deduce template argument for 'const std::basic_string<_Elem,_StTraits,_StAlloc> &' from 'std::_Vector_iterator<_Myvec>'
1>          with
1>          [
1>              _Myvec=std::_Vector_val<char,std::allocator<char>>
1>          ]
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\regex(2581) : see declaration of 'std::tr1::regex_search'
1>main.cpp(44): error C2784: 'bool std::tr1::regex_search(const _Elem *,std::tr1::match_results<const _Elem*,_Alloc> &,const std::tr1::basic_regex<_Elem,_RxTraits> &,std::tr1::regex_constants::match_flag_type)' : could not deduce template argument for 'const _Elem *' from 'std::_Vector_iterator<_Myvec>'
1>          with
1>          [
1>              _Myvec=std::_Vector_val<char,std::allocator<char>>
1>          ]
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\regex(2566) : see declaration of 'std::tr1::regex_search'
1>main.cpp(44): error C2780: 'bool std::tr1::regex_search(const _Elem *,const std::tr1::basic_regex<_Elem,_RxTraits> &,std::tr1::regex_constants::match_flag_type)' : expects 3 arguments - 4 provided
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\regex(2552) : see declaration of 'std::tr1::regex_search'
1>main.cpp(44): error C2784: 'bool std::tr1::regex_search(_BidIt,_BidIt,const std::tr1::basic_regex<_Elem,_RxTraits> &,std::tr1::regex_constants::match_flag_type)' : could not deduce template argument for 'const std::tr1::basic_regex<_Elem,_RxTraits> &' from 'std::tr1::cmatch'
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\regex(2540) : see declaration of 'std::tr1::regex_search'
1>main.cpp(44): error C2782: 'bool std::tr1::regex_search(_BidIt,_BidIt,std::tr1::match_results<_BidIt,_Alloc> &,const std::tr1::basic_regex<_Elem,_RxTraits> &,std::tr1::regex_constants::match_flag_type)' : template parameter '_BidIt' is ambiguous
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\regex(2528) : see declaration of 'std::tr1::regex_search'
1>          could be 'const char *'
1>          or       'std::_Vector_iterator<_Myvec>'
1>          with
1>          [
1>              _Myvec=std::_Vector_val<char,std::allocator<char>>
1>          ]
1>
1>Build FAILED.


Can't understand this errors. Line 10 & 44

typedef match_results<const char*> cmatch; // i think this is the main error

Thanks for your reply.
http://www.johndcook.com/cpp_regex.html

I would suggest to ignore the template typedef on that site, and try to use std::string instead of std::vector<char>.
Working code:

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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <iostream>
#include <vector>
#include <string>
#include <windows.h>
#include <algorithm>
#include <iterator>
#include <regex>


void find_locs(HANDLE process) {

    unsigned char *p = NULL;
    MEMORY_BASIC_INFORMATION info;

    // VirtualQueryEx does the hard part, telling use about each
    // block in the target process.

    for ( p = NULL;
        VirtualQueryEx(process, p, &info, sizeof(info)) == sizeof(info);
        p += info.RegionSize )
    {
        // buffer to hold copies of memory blocks from the target
        std::string buffer;
        //std::vector<char>::iterator pos;
 
        // We only want committed memory that's mapped or private --
        // screens out things like the code in the target process.
        //
        if (info.State == MEM_COMMIT &&
            (info.Type == MEM_MAPPED || info.Type == MEM_PRIVATE))
        {
            DWORD bytes_read;

            // copy block from target to our buffer, search for pattern:
            buffer.resize(info.RegionSize);
            ReadProcessMemory(process, p, &buffer[0], info.RegionSize, &bytes_read);
            buffer.resize(bytes_read);
            
            const std::tr1::regex rx("([\\w-+]+(?:\\.[\\w-+]+)*@(?:[\\w-]+\\.)+[a-zA-Z]{2,7})");
			std::tr1::match_results<std::string::const_iterator> res; 
			std::tr1::regex_search(buffer, res, rx);
     

           
            for ( unsigned int i=0; i<res.size(); ++i)
            {
               
                std::cout << res[i] << std::endl ;
            }
        }
    }
}

int main(int argc, char **argv) {
    if (argc != 2) {
        fprintf(stderr, "Usage: %s <process ID>", argv[0]);
        return 1;
    }

    // Read the target PID and search pattern:
    int pid;
    sscanf_s(argv[1], "%i", &pid);
    //std::string pattern(argv[2]);

    // Open the target process with rights to read its information and memory:
    HANDLE process = OpenProcess(
        PROCESS_VM_READ | PROCESS_QUERY_INFORMATION,
        false,
        pid);

    
    find_locs(process);

    return 0;
}


Catfish2 <3 Much love to you my dear friend.
It worked! Thank you so much... Peace
OK. Weird because I think it should have worked with std::vector too.

One last suggestion: if you don't have Service Pack 1 for Visual Studio 2010, install it.
http://www.microsoft.com/en-us/download/details.aspx?id=23691
Thanks for the suggestion. Installing it right now
Topic archived. No new replies allowed.