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

XPG4

Threadsafe

Yes

Locale Sensitive

The behavior of this function might be affected by the LC_CTYPE and LC_COLLATE categories of the current locale. This function is not available when LOCALETYPE(*CLD) is specified on the compilation command. For more information, see Understanding CCSIDs and Locales.

Description

The regerror() function finds the description for the error code errcode for the regular expression preg. The description for errcode is assigned to errbuf. The errbuf_size value specifies the maximum message size that can be stored (the size of errbuf). The description strings for errcode are:

errcode Description String
REG_NOMATCH regexec() failed to find a match.
REG_BADPAT Invalid regular expression.
REG_ECOLLATE Invalid collating element referenced.
REG_ECTYPE Invalid character class type referenced.
REG_EESCAPE Last character in regular expression is a \.
REG_ESUBREG Number in \digit invalid or error.
REG_EBRACK [] imbalance.
REG_EPAREN \( \) or () imbalance.
REG_EBRACE \{ \} imbalance.
REG_BADBR Expression between \{ and \} is invalid.
REG_ERANGE Invalid endpoint in range expression.
REG_ESPACE Out of memory.
REG_BADRPT ?, *, or + not preceded by valid regular expression.
REG_ECHAR Invalid multibyte character.
REG_EBOL ^ anchor not at beginning of regular expression.
REG_EEOL $ anchor not at end of regular expression.
REG_ECOMP Unknown error occurred during regcomp() call.
REG_EEXEC Unknown error occurred during regexec() call.

Return Value

The regerror() function returns the size of the buffer needed to hold the string that describes the error condition. The value of errno may be set to ECONVERT (conversion error).

Example

This example compiles an invalid regular expression, and prints an error message using the regerror() function.
#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.'
 **********************************************************/
}

Related Information