regcomp, regexec, regerror, regfree - POSIX regex functions


   #include <sys/types.h>
   #include <regex.h>

   int regcomp(regex_t *preg, const char *regex, int cflags);

   int regexec(const regex_t *preg, const char *string, size_t nmatch,
               regmatch_t pmatch[], int eflags);

   size_t regerror(int errcode, const regex_t *preg, char *errbuf,
                   size_t errbuf_size);

   void regfree(regex_t *preg);


   POSIX regex compiling
   regcomp()  is  used to compile a regular expression into a form that is
   suitable for subsequent regexec() searches.

   regcomp() is supplied with preg, a pointer to a pattern buffer  storage
   area;  regex, a pointer to the null-terminated string and cflags, flags
   used to determine the type of compilation.

   All regular expression searching must be done via  a  compiled  pattern
   buffer,  thus  regexec()  must always be supplied with the address of a
   regcomp() initialized pattern buffer.

   cflags may be the bitwise-or of zero or more of the following:

          Use POSIX Extended Regular Expression syntax  when  interpreting
          regex.   If  not  set,  POSIX Basic Regular Expression syntax is

          Do not differentiate case.  Subsequent regexec() searches  using
          this pattern buffer will be case insensitive.

          Do  not  report  position  of  matches.   The  nmatch and pmatch
          arguments  to  regexec()  are  ignored  if  the  pattern  buffer
          supplied was compiled with this flag set.

          Match-any-character operators don't match a newline.

          A  nonmatching  list ([^...])  not containing a newline does not
          match a newline.

          Match-beginning-of-line operator (^) matches  the  empty  string
          immediately  after  a newline, regardless of whether eflags, the
          execution flags of regexec(), contains REG_NOTBOL.

          Match-end-of-line  operator  ($)  matches   the   empty   string
          immediately  before  a  newline,  regardless  of  whether eflags
          contains REG_NOTEOL.

   POSIX regex matching
   regexec() is  used  to  match  a  null-terminated  string  against  the
   precompiled  pattern  buffer,  preg.   nmatch  and  pmatch  are used to
   provide information regarding the location of any matches.  eflags  may
   be  the  bitwise-or  of  one or both of REG_NOTBOL and REG_NOTEOL which
   cause changes in matching behavior described below.

          The match-beginning-of-line operator always fails to match  (but
          see  the  compilation flag REG_NEWLINE above).  This flag may be
          used when different portions of a string are passed to regexec()
          and the beginning of the string should not be interpreted as the
          beginning of the line.

          The match-end-of-line operator always fails to  match  (but  see
          the compilation flag REG_NEWLINE above).

   Byte offsets
   Unless  REG_NOSUB was set for the compilation of the pattern buffer, it
   is possible to obtain match addressing  information.   pmatch  must  be
   dimensioned  to  have at least nmatch elements.  These are filled in by
   regexec()  with  substring  match  addresses.   The  offsets   of   the
   subexpression  starting  at  the  ith  open  parenthesis  are stored in
   pmatch[i].  The entire regular expression's match addresses are  stored
   in  pmatch[0].   (Note  that  to  return the offsets of N subexpression
   matches, nmatch must be at least N+1.)  Any unused  structure  elements
   will contain the value -1.

   The  regmatch_t  structure  which  is  the type of pmatch is defined in

       typedef struct {
           regoff_t rm_so;
           regoff_t rm_eo;
       } regmatch_t;

   Each rm_so element that is not -1 indicates the  start  offset  of  the
   next  largest  substring  match  within the string.  The relative rm_eo
   element indicates the end offset of the match, which is the  offset  of
   the first character after the matching text.

   POSIX error reporting
   regerror() is used to turn the error codes that can be returned by both
   regcomp() and regexec() into error message strings.

   regerror() is passed the error code, errcode, the pattern buffer, preg,
   a  pointer  to  a  character string buffer, errbuf, and the size of the
   string buffer, errbuf_size.  It returns the size of the errbuf required
   to  contain  the  null-terminated error message string.  If both errbuf
   and errbuf_size are  nonzero,  errbuf  is  filled  in  with  the  first
   errbuf_size  - 1 characters of the error message and a terminating null
   byte ('\0').

   POSIX pattern buffer freeing
   Supplying regfree() with a precompiled pattern buffer, preg  will  free
   the  memory  allocated  to the pattern buffer by the compiling process,


   regcomp() returns zero for a successful compilation or  an  error  code
   for failure.

   regexec()  returns  zero  for  a  successful  match  or REG_NOMATCH for


   The following errors can be returned by regcomp():

          Invalid use of back reference operator.

          Invalid use of pattern operators such as group or list.

          Invalid use of repetition operators such as  using  '*'  as  the
          first character.

          Un-matched brace interval operators.

          Un-matched bracket list operators.

          Invalid collating element.

          Unknown character class name.

          Nonspecific error.  This is not defined by POSIX.2.

          Trailing backslash.

          Un-matched parenthesis group operators.

          Invalid use of the range operator; for example, the ending point
          of the range occurs prior to the starting point.

          Compiled regular expression requires  a  pattern  buffer  larger
          than 64Kb.  This is not defined by POSIX.2.

          The regex routines ran out of memory.

          Invalid back reference to a subexpression.


   For   an   explanation   of   the  terms  used  in  this  section,  see

   │InterfaceAttributeValue          │
   │regcomp(), regexec() │ Thread safety │ MT-Safe locale │
   │regerror()           │ Thread safety │ MT-Safe env    │
   │regfree()            │ Thread safety │ MT-Safe        │


   POSIX.1-2001, POSIX.1-2008.


   grep(1), regex(7)
   The glibc manual section, Regular Expressions


   This page is part of release 4.09 of the Linux  man-pages  project.   A
   description  of  the project, information about reporting bugs, and the
   latest    version    of    this    page,    can     be     found     at


Personal Opportunity - Free software gives you access to billions of dollars of software at no cost. Use this software for your business, personal use or to develop a profitable skill. Access to source code provides access to a level of capabilities/information that companies protect though copyrights. Open source is a core component of the Internet and it is available to you. Leverage the billions of dollars in resources and capabilities to build a career, establish a business or change the world. The potential is endless for those who understand the opportunity.

Business Opportunity - Goldman Sachs, IBM and countless large corporations are leveraging open source to reduce costs, develop products and increase their bottom lines. Learn what these companies know about open source and how open source can give you the advantage.

Free Software

Free Software provides computer programs and capabilities at no cost but more importantly, it provides the freedom to run, edit, contribute to, and share the software. The importance of free software is a matter of access, not price. Software at no cost is a benefit but ownership rights to the software and source code is far more significant.

Free Office Software - The Libre Office suite provides top desktop productivity tools for free. This includes, a word processor, spreadsheet, presentation engine, drawing and flowcharting, database and math applications. Libre Office is available for Linux or Windows.

Free Books

The Free Books Library is a collection of thousands of the most popular public domain books in an online readable format. The collection includes great classical literature and more recent works where the U.S. copyright has expired. These books are yours to read and use without restrictions.

Source Code - Want to change a program or know how it works? Open Source provides the source code for its programs so that anyone can use, modify or learn how to write those programs themselves. Visit the GNU source code repositories to download the source.


Study at Harvard, Stanford or MIT - Open edX provides free online courses from Harvard, MIT, Columbia, UC Berkeley and other top Universities. Hundreds of courses for almost all major subjects and course levels. Open edx also offers some paid courses and selected certifications.

Linux Manual Pages - A man or manual page is a form of software documentation found on Linux/Unix operating systems. Topics covered include computer programs (including library and system calls), formal standards and conventions, and even abstract concepts.