Regex to match 3 numbers in parentheses

I'm really struggling with regex in C++... do you know of a good resource where I can learn the proper syntax? Anyway, I would like something that would match any 3 numbers in parentheses, like (123), (284), (845) etc... I was thinking something like "([0-9][0-9][0-9])" but I don't think that will work. Here is what I have so far:

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

int main()
{
    std::string myString = "(123)";
    std::regex myRegex (/* Not sure what to put here to match any string like above */);

    if (std::regex_match (myString,myRegex))
        std::cout << "Match found!\n";

    return 0; 
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<iostream>
#include<regex>
#include<string>

int main()
{
    const std::string myString = "(123)";
    
    // open paranthesis, three decimal digits, close paranthesis
    const std::regex myRegex( "\\([0-9][0-9][0-9]\\)" ); // parantheses need to be escaped
    if( std::regex_match(myString,myRegex) ) std::cout << "Match found!\n";

    const std::regex myRegex2( "\\([0-9]{3}\\)" ); // [0-9] three times
    if( std::regex_match(myString,myRegex2) ) std::cout << "Match found!\n";

    const std::regex myRegex3( "\\(\\d{3}\\)" ); // decimal digit three times
    if( std::regex_match(myString,myRegex3) ) std::cout << "Match found!\n";

    // http://www.stroustrup.com/C++11FAQ.html#raw-strings
    const std::regex myRegex4( R"x(\(\d{3}\))x" ); // same as myRegex3 with a raw string literal
    if( std::regex_match(myString,myRegex3) ) std::cout << "Match found!\n";
}

http://coliru.stacked-crooked.com/a/425ff6b271113854

Tutorial: http://www.regular-expressions.info/tutorialcnt.html

Editor with regex builder and analyser: http://www.ultrapico.com/Expresso.htm
Thanks so much JLBorges!
Topic archived. No new replies allowed.