Ok, I did have definitely a very simple programming error. This now is the correct version of the code. Maybe it's helpful for someone else which gets stuck into regular expressions using regex.h
Code:
#include "stdio.h"
#include "stdlib.h"
#include "regex.h"
#include <string>
using std::string;
int main() {
string str = "Ae:3B:22:77:AC:22";
string pattern = "[[:xdigit:]]{2}:[[:xdigit:]]{2}:[[:xdigit:]]{2}:[[:xdigit:]]{2}:[[:xdigit:]]{2}:[[:xdigit:]]{2}";
regex_t pattern_t;
regmatch_t pmatch_t;
int ret = regcomp(&pattern_t, pattern.c_str(), REG_ICASE|REG_EXTENDED);
printf ("ret: %d\n", ret);
ret = regexec(&pattern_t, str.c_str(), 1, &pmatch_t, 0);
printf("ret: %d\n", ret);
if (ret == 0 && pmatch_t.rm_so == 0 && str.length() == 17)
{
printf("Correct MAC address!\n");
}
else
{
printf("Wrong MAC address!\n");
}
regfree(&pattern_t);
return 0;
}
I want to check if a given string represents a MAC address. That's it. Thx anyway for your help.
As far as I got known and tested regex.h doesn't support nested regular expressions. First I wanted to write
Code:
string pattern = "[[[:xdigit:]]{2}:]{5}[[:xdigit:]]{2}";
but that doesn't provide the correct results. That's why I have to use the much longer version
Code:
string pattern = "[[:xdigit:]]{2}:[[:xdigit:]]{2}:[[:xdigit:]]{2}:[[:xdigit:]]{2}:[[:xdigit:]]{2}:[[:xdigit:]]{2}";
which is definitely not so smart but at least works
pippo