c++regexqnx

Regex with posix regex.h faild to match


I used the regex.h library on a qnx6.6 system.

Unfortunately, the compiler does not provide the C++ library and I have a problem with regex.h which I cannot explain.

Maybe someone can tell me why my regex does not match.

#include <iostream>
#include <regex.h>
#include <string>

int main() {
  regex_t parse_regex_;
  std::string match_pattern_ = "^\\d*[1-9]\\d*";
  auto return_errno = regcomp(&parse_regex_, match_pattern_.c_str(),
                              (REG_EXTENDED | REG_NOSUB));

  std::string test_string = "000187149889";
  auto regex_pass = regexec(&parse_regex_, test_string.c_str(), 0, nullptr, 0);

  if (regex_pass != 0) {
    char error_c_str_buffer[100];
    regerror(regex_pass, &parse_regex_, error_c_str_buffer,
             sizeof(error_c_str_buffer));
    std::cerr << "regexec() :" << regex_pass
              << " regerror() :" << error_c_str_buffer << std::endl;
  }
}

Output: regexec() :1 regerror() :regexec() failed to match

My regex should recognize if the content of the test_string is only digits from 0 - 9 and at least one digit is 1-9.


Solution

  • \d is not supported by <regex.h>, use [0-9] instead

    std::string match_pattern_ = "^[0-9]*[1-9][0-9]*$";
    

    Demo