How to match parenthesis using a regular expression?

10,381

If you want to use parentheses you need to escape them with a backslash. The issue is that you need to escape that backslash too (for the C++ compiler). Example:

std::string regexstring = "\\([a-z]\\):\\([0-9]\\)"; 
Share:
10,381
Ambi
Author by

Ambi

Nothing much to say

Updated on June 07, 2022

Comments

  • Ambi
    Ambi almost 2 years

    I am using using boost libraries to parse a file. Its known that when you use a parenthesis it denotes a sub-expression in a regular expression. How would I declare a regular expression if my file contains parenthesis? I tried using \( with no luck. Could anyone tell me how I should declare a regular expression for the following format of file?

    a:(1)
    b:(2)
    

    I'm able to do the parsing when the file content is

    a:1
    b:2
    

    by declaring the regular expression as boost::regex e("([a-z]):([0-9])"); Can you tell me how I can also match if the values are in braces?