
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
        "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- MyFirstUnitAd -->
<ins class="adsbygoogle"
     style="display:inline-block;width:970px;height:250px"
     data-ad-client="ca-pub-5778386704669218"
     data-ad-slot="1503492166"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <link rel="stylesheet" type="text/css" href="../../../style.css" title="style" />
    <link rel="stylesheet" type="text/css" href="../style.css" title="style" />
    <meta http-equiv="content-type" content="text/html;charset=utf-8" />

    <title>regcomp.3p - Linux manual page</title>
</head>

<body>

<div class="page-top"><a id="top_of_page"></a></div>
<!--%%%TOP_BAR%%%-->
    <div class="nav-bar">
        <table class="nav-table">
            <tr>
                <td class="nav-cell">
                    <p class="nav-text">
                        <a href="http://man7.org/index.html">man7.org</a> &gt; Linux &gt; <a href="../index.html">man-pages</a>
                    </p>
                </td>
                <td class="training-cell">
                    <p class="training-text"><a class="training-link" href="http://man7.org/training/">Linux/UNIX system programming training</a></p>
                </td>
            </tr>
        </table>
    </div>

<hr class="nav-end" />

<!--%%%PAGE_START%%%-->


<table class="sec-table">
<tr>
    <td>
        <p class="section-dir">
<a href="regexec.3p.html#PROLOG">PROLOG</a> | <a href="regexec.3p.html#NAME">NAME</a> | <a href="regexec.3p.html#SYNOPSIS">SYNOPSIS</a> | <a href="regexec.3p.html#DESCRIPTION">DESCRIPTION</a> | <a href="regexec.3p.html#RETURN_VALUE">RETURN&nbsp;VALUE</a> | <a href="regexec.3p.html#ERRORS">ERRORS</a> | <a href="regexec.3p.html#EXAMPLES">EXAMPLES</a> | <a href="regexec.3p.html#APPLICATION_USAGE">APPLICATION&nbsp;USAGE</a> | <a href="regexec.3p.html#RATIONALE">RATIONALE</a> | <a href="regexec.3p.html#FUTURE_DIRECTIONS">FUTURE&nbsp;DIRECTIONS</a> | <a href="regexec.3p.html#SEE_ALSO">SEE&nbsp;ALSO</a> | <a href="regexec.3p.html#COPYRIGHT">COPYRIGHT</a>
        </p>
    </td>
    <td class="search-box">
        <div class="man-search-box">

            <form method="get" action="http://www.google.com/search">
                <fieldset class="man-search">
                    <input type="text" name="q" size="10" maxlength="255" value="" />
                    <input type="hidden" name="sitesearch" value="man7.org/linux/man-pages" />
                    <input type="submit" name="sa" value="Search online pages" />
                </fieldset>
            </form>

        </div>
    </td>
    <td> </td>
</tr>
</table>

<pre>
<span class="headline">REGCOMP(3P)               POSIX Programmer's Manual              REGCOMP(3P)</span>
</pre>
<h2><a id="PROLOG" href="regexec.3p.html#PROLOG"></a>PROLOG  &nbsp; &nbsp; &nbsp; &nbsp; <a href="regexec.3p.html#top_of_page"><span class="top-link">top</span></a></h2><pre>
       This manual page is part of the POSIX Programmer's Manual.  The Linux
       implementation of this interface may differ (consult the
       corresponding Linux manual page for details of Linux behavior), or
       the interface may not be implemented on Linux.
</pre>
<h2><a id="NAME" href="regexec.3p.html#NAME"></a>NAME  &nbsp; &nbsp; &nbsp; &nbsp; <a href="regexec.3p.html#top_of_page"><span class="top-link">top</span></a></h2><pre>
       regcomp, regerror, regexec, regfree — regular expression matching
</pre>
<h2><a id="SYNOPSIS" href="regexec.3p.html#SYNOPSIS"></a>SYNOPSIS  &nbsp; &nbsp; &nbsp; &nbsp; <a href="regexec.3p.html#top_of_page"><span class="top-link">top</span></a></h2><pre>
       #include &lt;regex.h&gt;

       int regcomp(regex_t *restrict <i>preg</i>, const char *restrict <i>pattern</i>,
           int <i>cflags</i>);
       size_t regerror(int <i>errcode</i>, const regex_t *restrict <i>preg</i>,
           char *restrict <i>errbuf</i>, size_t <i>errbuf_size</i>);
       int regexec(const regex_t *restrict <i>preg</i>, const char *restrict <i>string</i>,
           size_t <i>nmatch</i>, regmatch_t <i>pmatch</i>[restrict], int <i>eflags</i>);
       void regfree(regex_t *<i>preg</i>);
</pre>
<h2><a id="DESCRIPTION" href="regexec.3p.html#DESCRIPTION"></a>DESCRIPTION  &nbsp; &nbsp; &nbsp; &nbsp; <a href="regexec.3p.html#top_of_page"><span class="top-link">top</span></a></h2><pre>
       These functions interpret <i>basic</i> and <i>extended</i> regular expressions as
       described in the Base Definitions volume of POSIX.1‐2008, <i>Chapter 9</i>,
       <i>Regular Expressions</i>.

       The <b>regex_t </b>structure is defined in <i>&lt;regex.h&gt;</i> and contains at least
       the following member:

            ┌──────────────┬──────────────┬───────────────────────────┐
            │<b>Member Type   </b>│ <b>Member Name  </b>│        <b>Description        </b>│
            ├──────────────┼──────────────┼───────────────────────────┤
            │<b>size_t        </b>│<i>re_nsub</i>       │ Number of parenthesized   │
            │              │              │ subexpressions.           │
            └──────────────┴──────────────┴───────────────────────────┘
       The <b>regmatch_t </b>structure is defined in <i>&lt;regex.h&gt;</i> and contains at
       least the following members:

            ┌──────────────┬──────────────┬───────────────────────────┐
            │<b>Member Type   </b>│ <b>Member Name  </b>│        <b>Description        </b>│
            ├──────────────┼──────────────┼───────────────────────────┤
            │<b>regoff_t      </b>│<i>rm_so</i>         │ Byte offset from start of │
            │              │              │ <i>string</i> to start of        │
            │              │              │ substring.                │
            │<b>regoff_t      </b>│<i>rm_eo</i>         │ Byte offset from start of │
            │              │              │ <i>string</i> of the first       │
            │              │              │ character after the end   │
            │              │              │ of substring.             │
            └──────────────┴──────────────┴───────────────────────────┘
       The <i>regcomp</i>() function shall compile the regular expression contained
       in the string pointed to by the <i>pattern</i> argument and place the
       results in the structure pointed to by <i>preg</i>.  The <i>cflags</i> argument is
       the bitwise-inclusive OR of zero or more of the following flags,
       which are defined in the <i>&lt;regex.h&gt;</i> header:

       REG_EXTENDED  Use Extended Regular Expressions.

       REG_ICASE     Ignore case in match (see the Base Definitions volume
                     of POSIX.1‐2008, <i>Chapter 9</i>, <i>Regular Expressions</i>).

       REG_NOSUB     Report only success/fail in <i>regexec</i>().

       REG_NEWLINE   Change the handling of &lt;newline&gt; characters, as
                     described in the text.

       The default regular expression type for <i>pattern</i> is a Basic Regular
       Expression. The application can specify Extended Regular Expressions
       using the REG_EXTENDED <i>cflags</i> flag.

       If the REG_NOSUB flag was not set in <i>cflags</i>, then <i>regcomp</i>() shall set
       <i>re_nsub</i> to the number of parenthesized subexpressions (delimited by
       <b>"\(\)" </b>in basic regular expressions or <b>"()" </b>in extended regular
       expressions) found in <i>pattern</i>.

       The <i>regexec</i>() function compares the null-terminated string specified
       by <i>string</i> with the compiled regular expression <i>preg</i> initialized by a
       previous call to <i>regcomp</i>().  If it finds a match, <i>regexec</i>() shall
       return 0; otherwise, it shall return non-zero indicating either no
       match or an error. The <i>eflags</i> argument is the bitwise-inclusive OR of
       zero or more of the following flags, which are defined in the
       <i>&lt;regex.h&gt;</i> header:

       REG_NOTBOL    The first character of the string pointed to by <i>string</i>
                     is not the beginning of the line. Therefore, the
                     &lt;circumflex&gt; character (<b>'^'</b>), when taken as a special
                     character, shall not match the beginning of <i>string</i>.

       REG_NOTEOL    The last character of the string pointed to by <i>string</i>
                     is not the end of the line. Therefore, the &lt;dollar-
                     sign&gt; (<b>'$'</b>), when taken as a special character, shall
                     not match the end of <i>string</i>.

       If <i>nmatch</i> is 0 or REG_NOSUB was set in the <i>cflags</i> argument to
       <i>regcomp</i>(), then <i>regexec</i>() shall ignore the <i>pmatch</i> argument.
       Otherwise, the application shall ensure that the <i>pmatch</i> argument
       points to an array with at least <i>nmatch</i> elements, and <i>regexec</i>() shall
       fill in the elements of that array with offsets of the substrings of
       <i>string</i> that correspond to the parenthesized subexpressions of
       <i>pattern</i>: <i>pmatch</i>[<i>i</i>].<i>rm_so</i> shall be the byte offset of the beginning
       and <i>pmatch</i>[<i>i</i>].<i>rm_eo</i> shall be one greater than the byte offset of the
       end of substring <i>i</i>.  (Subexpression <i>i</i> begins at the <i>i</i>th matched open
       parenthesis, counting from 1.) Offsets in <i>pmatch</i>[0] identify the
       substring that corresponds to the entire regular expression. Unused
       elements of <i>pmatch</i> up to <i>pmatch</i>[<i>nmatch</i>−1] shall be filled with −1. If
       there are more than <i>nmatch</i> subexpressions in <i>pattern</i> (<i>pattern</i> itself
       counts as a subexpression), then <i>regexec</i>() shall still do the match,
       but shall record only the first <i>nmatch</i> substrings.

       When matching a basic or extended regular expression, any given
       parenthesized subexpression of <i>pattern</i> might participate in the match
       of several different substrings of <i>string</i>, or it might not match any
       substring even though the pattern as a whole did match. The following
       rules shall be used to determine which substrings to report in <i>pmatch</i>
       when matching regular expressions:

        1. If subexpression <i>i</i> in a regular expression is not contained
           within another subexpression, and it participated in the match
           several times, then the byte offsets in <i>pmatch</i>[<i>i</i>] shall delimit
           the last such match.

        2. If subexpression <i>i</i> is not contained within another subexpression,
           and it did not participate in an otherwise successful match, the
           byte offsets in <i>pmatch</i>[<i>i</i>] shall be −1. A subexpression does not
           participate in the match when:

           <b>'*' </b>or <b>"\{\}" </b>appears immediately after the subexpression in a
           basic regular expression, or <b>'*'</b>, <b>'?'</b>, or <b>"{}" </b>appears
           immediately after the subexpression in an extended regular
           expression, and the subexpression did not match (matched 0 times)

           or:

                  <b>'|' </b>is used in an extended regular expression to select
                  this subexpression or another, and the other subexpression
                  matched.

        3. If subexpression <i>i</i> is contained within another subexpression <i>j</i>,
           and <i>i</i> is not contained within any other subexpression that is
           contained within <i>j</i>, and a match of subexpression <i>j</i> is reported in
           <i>pmatch</i>[<i>j</i>], then the match or non-match of subexpression <i>i</i>
           reported in <i>pmatch</i>[<i>i</i>] shall be as described in 1. and 2. above,
           but within the substring reported in <i>pmatch</i>[<i>j</i>] rather than the
           whole string. The offsets in <i>pmatch</i>[<i>i</i>] are still relative to the
           start of <i>string</i>.

        4. If subexpression <i>i</i> is contained in subexpression <i>j</i>, and the byte
           offsets in <i>pmatch</i>[<i>j</i>] are −1, then the pointers in <i>pmatch</i>[<i>i</i>] shall
           also be −1.

        5. If subexpression <i>i</i> matched a zero-length string, then both byte
           offsets in <i>pmatch</i>[<i>i</i>] shall be the byte offset of the character or
           null terminator immediately following the zero-length string.

       If, when <i>regexec</i>() is called, the locale is different from when the
       regular expression was compiled, the result is undefined.

       If REG_NEWLINE is not set in <i>cflags</i>, then a &lt;newline&gt; in <i>pattern</i> or
       <i>string</i> shall be treated as an ordinary character. If REG_NEWLINE is
       set, then &lt;newline&gt; shall be treated as an ordinary character except
       as follows:

        1. A &lt;newline&gt; in <i>string</i> shall not be matched by a &lt;period&gt; outside
           a bracket expression or by any form of a non-matching list (see
           the Base Definitions volume of POSIX.1‐2008, <i>Chapter 9</i>, <i>Regular</i>
           <i>Expressions</i>).

        2. A &lt;circumflex&gt; (<b>'^'</b>) in <i>pattern</i>, when used to specify expression
           anchoring (see the Base Definitions volume of POSIX.1‐2008,
           <i>Section 9.3.8</i>, <i>BRE Expression Anchoring</i>), shall match the zero-
           length string immediately after a &lt;newline&gt; in <i>string</i>, regardless
           of the setting of REG_NOTBOL.

        3. A &lt;dollar-sign&gt; (<b>'$'</b>) in <i>pattern</i>, when used to specify expression
           anchoring, shall match the zero-length string immediately before
           a &lt;newline&gt; in <i>string</i>, regardless of the setting of REG_NOTEOL.

       The <i>regfree</i>() function frees any memory allocated by <i>regcomp</i>()
       associated with <i>preg</i>.

       The following constants are defined as the minimum set of error
       return values, although other errors listed as implementation
       extensions in <i>&lt;regex.h&gt;</i> are possible:

       REG_BADBR     Content of <b>"\{\}" </b>invalid: not a number, number too
                     large, more than two numbers, first larger than second.

       REG_BADPAT    Invalid regular expression.

       REG_BADRPT    <b>'?'</b>, <b>'*'</b>, or <b>'+' </b>not preceded by valid regular
                     expression.

       REG_EBRACE    <b>"\{\}" </b>imbalance.

       REG_EBRACK    <b>"[]" </b>imbalance.

       REG_ECOLLATE  Invalid collating element referenced.

       REG_ECTYPE    Invalid character class type referenced.

       REG_EESCAPE   Trailing &lt;backslash&gt; character in pattern.

       REG_EPAREN    <b>"\(\)" </b>or <b>"()" </b>imbalance.

       REG_ERANGE    Invalid endpoint in range expression.

       REG_ESPACE    Out of memory.

       REG_ESUBREG   Number in <b>"\digit" </b>invalid or in error.

       REG_NOMATCH   <i>regexec</i>() failed to match.

       If more than one error occurs in processing a function call, any one
       of the possible constants may be returned, as the order of detection
       is unspecified.

       The <i>regerror</i>() function provides a mapping from error codes returned
       by <i>regcomp</i>() and <i>regexec</i>() to unspecified printable strings. It
       generates a string corresponding to the value of the <i>errcode</i>
       argument, which the application shall ensure is the last non-zero
       value returned by <i>regcomp</i>() or <i>regexec</i>() with the given value of
       <i>preg</i>.  If <i>errcode</i> is not such a value, the content of the generated
       string is unspecified.

       If <i>preg</i> is a null pointer, but <i>errcode</i> is a value returned by a
       previous call to <i>regexec</i>() or <i>regcomp</i>(), the <i>regerror</i>() still
       generates an error string corresponding to the value of <i>errcode</i>, but
       it might not be as detailed under some implementations.

       If the <i>errbuf_size</i> argument is not 0, <i>regerror</i>() shall place the
       generated string into the buffer of size <i>errbuf_size</i> bytes pointed to
       by <i>errbuf</i>.  If the string (including the terminating null) cannot fit
       in the buffer, <i>regerror</i>() shall truncate the string and null-
       terminate the result.

       If <i>errbuf_size</i> is 0, <i>regerror</i>() shall ignore the <i>errbuf</i> argument, and
       return the size of the buffer needed to hold the generated string.

       If the <i>preg</i> argument to <i>regexec</i>() or <i>regfree</i>() is not a compiled
       regular expression returned by <i>regcomp</i>(), the result is undefined. A
       <i>preg</i> is no longer treated as a compiled regular expression after it
       is given to <i>regfree</i>().
</pre>
<h2><a id="RETURN_VALUE" href="regexec.3p.html#RETURN_VALUE"></a>RETURN VALUE  &nbsp; &nbsp; &nbsp; &nbsp; <a href="regexec.3p.html#top_of_page"><span class="top-link">top</span></a></h2><pre>
       Upon successful completion, the <i>regcomp</i>() function shall return 0.
       Otherwise, it shall return an integer value indicating an error as
       described in <i>&lt;regex.h&gt;</i>, and the content of <i>preg</i> is undefined. If a
       code is returned, the interpretation shall be as given in <i>&lt;regex.h&gt;</i>.

       If <i>regcomp</i>() detects an invalid RE, it may return REG_BADPAT, or it
       may return one of the error codes that more precisely describes the
       error.

       Upon successful completion, the <i>regexec</i>() function shall return 0.
       Otherwise, it shall return REG_NOMATCH to indicate no match.

       Upon successful completion, the <i>regerror</i>() function shall return the
       number of bytes needed to hold the entire generated string, including
       the null termination. If the return value is greater than
       <i>errbuf_size</i>, the string returned in the buffer pointed to by <i>errbuf</i>
       has been truncated.

       The <i>regfree</i>() function shall not return a value.
</pre>
<h2><a id="ERRORS" href="regexec.3p.html#ERRORS"></a>ERRORS  &nbsp; &nbsp; &nbsp; &nbsp; <a href="regexec.3p.html#top_of_page"><span class="top-link">top</span></a></h2><pre>
       No errors are defined.

       <i>The following sections are informative.</i>
</pre>
<h2><a id="EXAMPLES" href="regexec.3p.html#EXAMPLES"></a>EXAMPLES  &nbsp; &nbsp; &nbsp; &nbsp; <a href="regexec.3p.html#top_of_page"><span class="top-link">top</span></a></h2><pre>
           <b>#include &lt;regex.h&gt;</b>

           /*
            * Match string against the extended regular expression in
            * pattern, treating errors as no match.
            *
            * Return 1 for match, 0 for no match.
            */

           int
           match(const char *string, char *pattern)
           {
               int    status;
               regex_t    re;

               if (regcomp(&amp;re, pattern, REG_EXTENDED|REG_NOSUB) != 0) {
                   return(0);      /* Report error. */
               }
               status = regexec(&amp;re, string, (size_t) 0, NULL, 0);
               regfree(&amp;re);
               if (status != 0) {
                   return(0);      /* Report error. */
               }
               return(1);
           }

       The following demonstrates how the REG_NOTBOL flag could be used with
       <i>regexec</i>() to find all substrings in a line that match a pattern
       supplied by a user.  (For simplicity of the example, very little
       error checking is done.)

           <b>(void) regcomp (&amp;re, pattern, 0);</b>
           <b>/* This call to regexec() finds the first match on the line. */</b>
           <b>error = regexec (&amp;re, &amp;buffer[0], 1, &amp;pm, 0);</b>
           <b>while (error == 0) {  /* While matches found. */</b>
               <b>/* Substring found between pm.rm_so and pm.rm_eo. */</b>
               <b>/* This call to regexec() finds the next match. */</b>
               <b>error = regexec (&amp;re, buffer + pm.rm_eo, 1, &amp;pm, REG_NOTBOL);</b>
           <b>}</b>
</pre>
<h2><a id="APPLICATION_USAGE" href="regexec.3p.html#APPLICATION_USAGE"></a>APPLICATION USAGE  &nbsp; &nbsp; &nbsp; &nbsp; <a href="regexec.3p.html#top_of_page"><span class="top-link">top</span></a></h2><pre>
       An application could use:

           <b>regerror(code,preg,(char *)NULL,(size_t)0)</b>

       to find out how big a buffer is needed for the generated string,
       <i>malloc</i>() a buffer to hold the string, and then call <i>regerror</i>() again
       to get the string. Alternatively, it could allocate a fixed, static
       buffer that is big enough to hold most strings, and then use <i>malloc</i>()
       to allocate a larger buffer if it finds that this is too small.

       To match a pattern as described in the Shell and Utilities volume of
       POSIX.1‐2008, <i>Section 2.13</i>, <i>Pattern Matching Notation</i>, use the
       <i>fnmatch</i>() function.
</pre>
<h2><a id="RATIONALE" href="regexec.3p.html#RATIONALE"></a>RATIONALE  &nbsp; &nbsp; &nbsp; &nbsp; <a href="regexec.3p.html#top_of_page"><span class="top-link">top</span></a></h2><pre>
       The <i>regexec</i>() function must fill in all <i>nmatch</i> elements of <i>pmatch</i>,
       where <i>nmatch</i> and <i>pmatch</i> are supplied by the application, even if some
       elements of <i>pmatch</i> do not correspond to subexpressions in <i>pattern</i>.
       The application developer should note that there is probably no
       reason for using a value of <i>nmatch</i> that is larger than
       <i>preg</i>−&gt;<i>re_nsub</i>+1.

       The REG_NEWLINE flag supports a use of RE matching that is needed in
       some applications like text editors. In such applications, the user
       supplies an RE asking the application to find a line that matches the
       given expression. An anchor in such an RE anchors at the beginning or
       end of any line. Such an application can pass a sequence of
       &lt;newline&gt;-separated lines to <i>regexec</i>() as a single long string and
       specify REG_NEWLINE to <i>regcomp</i>() to get the desired behavior. The
       application must ensure that there are no explicit &lt;newline&gt;
       characters in <i>pattern</i> if it wants to ensure that any match occurs
       entirely within a single line.

       The REG_NEWLINE flag affects the behavior of <i>regexec</i>(), but it is in
       the <i>cflags</i> parameter to <i>regcomp</i>() to allow flexibility of
       implementation. Some implementations will want to generate the same
       compiled RE in <i>regcomp</i>() regardless of the setting of REG_NEWLINE and
       have <i>regexec</i>() handle anchors differently based on the setting of the
       flag. Other implementations will generate different compiled REs
       based on the REG_NEWLINE.

       The REG_ICASE flag supports the operations taken by the <i>grep</i> <b>−i</b>
       option and the historical implementations of <i>ex</i> and <i>vi</i>.  Including
       this flag will make it easier for application code to be written that
       does the same thing as these utilities.

       The substrings reported in <i>pmatch</i>[] are defined using offsets from
       the start of the string rather than pointers. This allows type-safe
       access to both constant and non-constant strings.

       The type <b>regoff_t </b>is used for the elements of <i>pmatch</i>[] to ensure that
       the application can represent large arrays in memory (important for
       an application conforming to the Shell and Utilities volume of
       POSIX.1‐2008).

       The 1992 edition of this standard required <b>regoff_t </b>to be at least as
       wide as <b>off_t</b>, to facilitate future extensions in which the string to
       be searched is taken from a file. However, these future extensions
       have not appeared.  The requirement rules out popular implementations
       with 32-bit <b>regoff_t </b>and 64-bit <b>off_t</b>, so it has been removed.

       The standard developers rejected the inclusion of a <i>regsub</i>() function
       that would be used to do substitutions for a matched RE. While such a
       routine would be useful to some applications, its utility would be
       much more limited than the matching function described here. Both RE
       parsing and substitution are possible to implement without support
       other than that required by the ISO C standard, but matching is much
       more complex than substituting. The only difficult part of
       substitution, given the information supplied by <i>regexec</i>(), is finding
       the next character in a string when there can be multi-byte
       characters. That is a much larger issue, and one that needs a more
       general solution.

       The <i><a href="errno.3.html">errno</a></i> variable has not been used for error returns to avoid
       filling the <i><a href="errno.3.html">errno</a></i> name space for this feature.

       The interface is defined so that the matched substrings <i>rm_sp</i> and
       <i>rm_ep</i> are in a separate <b>regmatch_t </b>structure instead of in <b>regex_t</b>.
       This allows a single compiled RE to be used simultaneously in several
       contexts; in <i>main</i>() and a signal handler, perhaps, or in multiple
       threads of lightweight processes. (The <i>preg</i> argument to <i>regexec</i>() is
       declared with type <b>const</b>, so the implementation is not permitted to
       use the structure to store intermediate results.) It also allows an
       application to request an arbitrary number of substrings from an RE.
       The number of subexpressions in the RE is reported in <i>re_nsub</i> in
       <i>preg</i>.  With this change to <i>regexec</i>(), consideration was given to
       dropping the REG_NOSUB flag since the user can now specify this with
       a zero <i>nmatch</i> argument to <i>regexec</i>().  However, keeping REG_NOSUB
       allows an implementation to use a different (perhaps more efficient)
       algorithm if it knows in <i>regcomp</i>() that no subexpressions need be
       reported. The implementation is only required to fill in <i>pmatch</i> if
       <i>nmatch</i> is not zero and if REG_NOSUB is not specified. Note that the
       <b>size_t </b>type, as defined in the ISO C standard, is unsigned, so the
       description of <i>regexec</i>() does not need to address negative values of
       <i>nmatch</i>.

       REG_NOTBOL was added to allow an application to do repeated searches
       for the same pattern in a line. If the pattern contains a
       &lt;circumflex&gt; character that should match the beginning of a line,
       then the pattern should only match when matched against the beginning
       of the line.  Without the REG_NOTBOL flag, the application could
       rewrite the expression for subsequent matches, but in the general
       case this would require parsing the expression. The need for
       REG_NOTEOL is not as clear; it was added for symmetry.

       The addition of the <i>regerror</i>() function addresses the historical need
       for conforming application programs to have access to error
       information more than ``Function failed to compile/match your RE for
       unknown reasons''.

       This interface provides for two different methods of dealing with
       error conditions. The specific error codes (REG_EBRACE, for example),
       defined in <i>&lt;regex.h&gt;</i>, allow an application to recover from an error
       if it is so able. Many applications, especially those that use
       patterns supplied by a user, will not try to deal with specific error
       cases, but will just use <i>regerror</i>() to obtain a human-readable error
       message to present to the user.

       The <i>regerror</i>() function uses a scheme similar to <i>confstr</i>() to deal
       with the problem of allocating memory to hold the generated string.
       The scheme used by <i>strerror</i>() in the ISO C standard was considered
       unacceptable since it creates difficulties for multi-threaded
       applications.

       The <i>preg</i> argument is provided to <i>regerror</i>() to allow an
       implementation to generate a more descriptive message than would be
       possible with <i>errcode</i> alone. An implementation might, for example,
       save the character offset of the offending character of the pattern
       in a field of <i>preg</i>, and then include that in the generated message
       string. The implementation may also ignore <i>preg</i>.

       A REG_FILENAME flag was considered, but omitted. This flag caused
       <i>regexec</i>() to match patterns as described in the Shell and Utilities
       volume of POSIX.1‐2008, <i>Section 2.13</i>, <i>Pattern Matching Notation</i>
       instead of REs. This service is now provided by the <i>fnmatch</i>()
       function.

       Notice that there is a difference in philosophy between the
       ISO POSIX‐2:1993 standard and POSIX.1‐2008 in how to handle a ``bad''
       regular expression. The ISO POSIX‐2:1993 standard says that many bad
       constructs ``produce undefined results'', or that ``the
       interpretation is undefined''. POSIX.1‐2008, however, says that the
       interpretation of such REs is unspecified. The term ``undefined''
       means that the action by the application is an error, of similar
       severity to passing a bad pointer to a function.

       The <i>regcomp</i>() and <i>regexec</i>() functions are required to accept any
       null-terminated string as the <i>pattern</i> argument. If the meaning of the
       string is ``undefined'', the behavior of the function is
       ``unspecified''. POSIX.1‐2008 does not specify how the functions will
       interpret the pattern; they might return error codes, or they might
       do pattern matching in some completely unexpected way, but they
       should not do something like abort the process.
</pre>
<h2><a id="FUTURE_DIRECTIONS" href="regexec.3p.html#FUTURE_DIRECTIONS"></a>FUTURE DIRECTIONS  &nbsp; &nbsp; &nbsp; &nbsp; <a href="regexec.3p.html#top_of_page"><span class="top-link">top</span></a></h2><pre>
       None.
</pre>
<h2><a id="SEE_ALSO" href="regexec.3p.html#SEE_ALSO"></a>SEE ALSO  &nbsp; &nbsp; &nbsp; &nbsp; <a href="regexec.3p.html#top_of_page"><span class="top-link">top</span></a></h2><pre>
       <a href="fnmatch.3p.html">fnmatch(3p)</a>, <a href="glob.3p.html">glob(3p)</a>

       The Base Definitions volume of POSIX.1‐2008, <i>Chapter 9</i>, <i>Regular</i>
       <i>Expressions</i>, <a href="../man0/regex.h.0p.html">regex.h(0p)</a>, <a href="../man0/sys_types.h.0p.html">sys_types.h(0p)</a>

       The Shell and Utilities volume of POSIX.1‐2008, <i>Section 2.13</i>, <i>Pattern</i>
       <i>Matching Notation</i>
</pre>
<h2><a id="COPYRIGHT" href="regexec.3p.html#COPYRIGHT"></a>COPYRIGHT  &nbsp; &nbsp; &nbsp; &nbsp; <a href="regexec.3p.html#top_of_page"><span class="top-link">top</span></a></h2><pre>
       Portions of this text are reprinted and reproduced in electronic form
       from IEEE Std 1003.1, 2013 Edition, Standard for Information
       Technology -- Portable Operating System Interface (POSIX), The Open
       Group Base Specifications Issue 7, Copyright (C) 2013 by the
       Institute of Electrical and Electronics Engineers, Inc and The Open
       Group.  (This is POSIX.1-2008 with the 2013 Technical Corrigendum 1
       applied.) In the event of any discrepancy between this version and
       the original IEEE and The Open Group Standard, the original IEEE and
       The Open Group Standard is the referee document. The original
       Standard can be obtained online at <a href="http://www.unix.org/online.html">http://www.unix.org/online.html</a> .

       Any typographical or formatting errors that appear in this page are
       most likely to have been introduced during the conversion of the
       source files to man page format. To report such errors, see
       <a href="https://www.kernel.org/doc/man-pages/reporting_bugs.html">https://www.kernel.org/doc/man-pages/reporting_bugs.html</a> .

<span class="footline">IEEE/The Open Group                 2013                         REGCOMP(3P)</span>
</pre>

<hr class="end-man-text" />
<p>Pages that refer to this page: 
    <a href="../man0/regex.h.0p.html">regex.h(0p)</a>
</p>
<hr/>

 
<hr class="start-footer" />

<div class="footer"> 

<table class="colophon-table">
    <tr>
    <td class="pub-info">
        <p>
            HTML rendering created 2018-02-02
            by <a href="http://man7.org/mtk/index.html">Michael Kerrisk</a>, 
            author of 
            <a href="http://man7.org/tlpi/"><em>The Linux Programming Interface</em></a>, 
            maintainer of the 
            <a href="https://www.kernel.org/doc/man-pages/">Linux <em>man-pages</em> project</a>.
        </p>
        <p>
            For details of in-depth
            <strong>Linux/UNIX system programming training courses</strong>
            that I teach, look <a href="http://man7.org/training/">here</a>.
        </p>
        <p>
            Hosting by <a href="http://www.jambit.com/index_en.html">jambit GmbH</a>.
        </p>
        <p>
            <a href="http://validator.w3.org/check?uri=referer">
            <img src="http://www.w3.org/Icons/valid-xhtml11"
                alt="Valid XHTML 1.1" height="31" width="88" />
            </a>
        </p>
    </td>
    <td class="colophon-divider">
    </td>
    <td class="tlpi-cover">
        <a href="http://man7.org/tlpi/"><img src="../../../tlpi/cover/TLPI-front-cover-vsmall.png" alt="Cover of TLPI" /></a>
    </td>
    </tr>
</table>

</div>

<hr class="end-footer" />



<!--BEGIN-SITETRACKING-->
<!-- SITETRACKING.man7.org_linux_man-pages -->

<!-- Start of StatCounter Code (xhtml) -->

<script type="text/javascript">
//<![CDATA[
var sc_project=7422636; 
var sc_invisible=1; 
var sc_security="9b6714ff"; 
//]]>
</script>
<script type="text/javascript"
src="http://www.statcounter.com/counter/counter_xhtml.js"></script>
<noscript><div class="statcounter"><a title="website
statistics" href="http://statcounter.com/"
class="statcounter"><img class="statcounter"
src="http://c.statcounter.com/7422636/0/9b6714ff/1/"
alt="website statistics" /></a></div></noscript>

<!-- End of StatCounter Code -->


<!-- Start of Google Analytics Code -->

<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-9830363-8']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>

<!-- End of Google Analytics Code -->

<!--END-SITETRACKING-->

</body>
</html>
