regex_search() for digits not working

I have written a small program (based on an example in Josuttis' book) to search for an occurrence of digits in a string:

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
/* The following code example is taken from the book
 * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition"
 * by Nicolai M. Josuttis, Addison-Wesley, 2012
 *
 * (C) Copyright Nicolai M. Josuttis 2012.
 */

#include <string>
#include <regex>
#include <iostream>
#include <iomanip>

using namespace std;


int main()
{
    string data = "Hello this is my attempt # 723 to analyze regexes!";
    cout << "data:             " << data << "\n\n\n";

    smatch m;  // for returned details of the match
    bool found = regex_search (data,
                               m, 
                               regex { "([[:d:]]*)" });

    // print match details:
    cout << "m.empty():        " 
         << boolalpha << m.empty() << endl;
    cout << "m.size():         " 
         << m.size() << endl;

    if (found)
    {
        cout << "m.str():          " 
             << m.str() << endl;
        cout << "m.length():       " 
             << m.length() << endl;
        cout << "m.position():     " 
             << m.position() << endl;
        cout << "m.prefix().str(): " 
             << m.prefix().str() << endl;
        cout << "m.suffix().str(): " 
             << m.suffix().str() << endl;
        cout << endl << endl;

        // iterating over all matches 
        // (using the match index):
        for (unsigned int i=0; i<m.size(); ++i)
        {
            cout << "m[" << i << "].str():       " 
                 << m[i].str() << endl;
            cout << "m.str(" << i << "):         " 
                 << m.str(i) << endl;
            cout << "m.position(" << i << "):    " 
                 << m.position(i)
                 << endl << endl;
        }
        
        cout << endl;

        // iterating over all matches (using iterators):
        cout << "matches:" << endl;

        for (auto pos = m.begin(); pos != m.end(); ++pos)
        {
            cout << " " << *pos << " ";
            cout << "(length: " << pos->length() << ")" 
                 << endl;
        }
    }
}


http://cpp.sh/24sy6


However, the output is wrong and digits are not being detected:


data:             Hello this is my attempt # 723 to analyze regexes!


m.empty():        false
m.size():         2
m.str():          
m.length():       0
m.position():     0
m.prefix().str(): 
m.suffix().str(): Hello this is my attempt # 723 to analyze regexes!


m[0].str():       
m.str(0):         
m.position(0):    0

m[1].str():       
m.str(1):         
m.position(1):    0


matches:
  (length: 0)
  (length: 0)



Obviously, the regex is incorrect:
 
regex { "([[:d:]]*)" }


I have tried variants of this regex also, but they don't work.

What's the problem?

Thanks.
OK, I've solved the problem.

The correct regex is:
 
regex { "([[:d:]]+)" }

Another option is: regex("(\\d+)")

data: Hello this is my attempt # 723 to analyze regexes!


m.empty(): false
m.size(): 2
m.str(): 723
m.length(): 3
m.position(): 27
m.prefix().str(): Hello this is my attempt #
m.suffix().str(): to analyze regexes!


m[0].str(): 723
m.str(0): 723
m.position(0): 27

m[1].str(): 723
m.str(1): 723
m.position(1): 27


matches:
723 (length: 3)
723 (length: 3)
Press any key to continue . . .
Topic archived. No new replies allowed.