Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ regex character class not matching [duplicate]

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";
like image 412
Lim Ta Sheng Avatar asked Jun 09 '18 10:06

Lim Ta Sheng


2 Answers

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.

like image 97
gsamaras Avatar answered Nov 20 '22 11:11

gsamaras


You have to use [[:alpha:]]

see online example

#include <iostream>
#include <string>
#include <regex>
using namespace std;

int main() {
    std::regex e ("[[:alpha:]]");
if(std::regex_match("A",e))
     std::cout<<"hi";
  else
     std::cout<<"no";
    return 0;
}
like image 43
Thomas Ayoub Avatar answered Nov 20 '22 12:11

Thomas Ayoub