Search code examples
c++regexcharacter-class

C++ regex character class not matching


from what i researched, the expression "[:alpha:]" will be matched for any alphabetic character, but the expression only match for lowercase character and not uppercase character. I not sure what's wrong with it.

std::regex e ("[:alpha:]");
if(std::regex_match("A",e))
     std::cout<<"hi";
  else
     std::cout<<"no";

Solution

  • Change this:

    std::regex e ("[:alpha:]");
    

    to:

    std::regex e ("[[:alpha:]]");
    

    As Adrian stated: Please note that the brackets in the class names are additional to those opening and closing the class definition. For example: [[:alpha:]] is a character class that matches any alphabetic character. Read more in the ref.