regerror -- Return Error Message for Regular Expression

Format

#include <regex.h>
size_t regerror(int errcode,  const regex_t *preg,
                char *errbuf, size_t errbuf_size);

Language Level: POSIX, XPG4
regerror finds the description for the error code errcode for the regular expression preg. The description for errcode is assigned to errbuf. errbuf_size is a value that you provide that specifies the maximum message size that can be stored (the size of errbuf).

The description strings for errcode are:

errcode Description String
REG_NOMATCH RE pattern not found
REG_BADPAT Invalid regular expression
REG_ECOLLATE Invalid collating element
REG_ECTYPE Invalid character class
REG_EESCAPE Last character is \
REG_ESUBREG Invalid number in \digit
REG_EBRACK [] imbalance
REG_EPAREN \( \) or () imbalance
REG_EBRACE \{ \} or { } imbalance
REG_BADBR Invalid \{ \} range exp
REG_ERANGE Invalid range exp endpoint
REG_ESPACE Out of memory
REG_BADRPT ?*+ not preceded by valid RE
REG_ECHAR Invalid multibyte character
REG_EBOL ^ anchor and not BOL
REG_EEOL $ anchor and not EOL

Return Value
regerror returns the size of the buffer needed to hold the string that describes the error condition.

Example
This example compiles an invalid regular expression, and prints an error message using regerror.

#include <regex.h>
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
   regex_t preg;
   char    *pattern = "a[missing.bracket";
   int     rc;
   char    buffer[100];
   if (0 != (rc = regcomp(&preg, pattern, REG_EXTENDED))) {
      regerror(rc, &preg, buffer, 100);
      printf("regcomp() failed with '%s'\n", buffer);
      exit(EXIT_FAILURE);
   }
   return 0;
   /**********************************************************
      The output should be similar to:
      regcomp() failed with '[] imbalance'
   **********************************************************/
}



Regular Expressions


regcomp -- Compile Regular Expression
regexec -- Execute Compiled Regular Expression
regfree -- Free Memory for Regular Expression
<regex.h>