⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 regexec.html

📁 unix 下的C开发手册,还用详细的例程。
💻 HTML
📖 第 1 页 / 共 2 页
字号:
If, when<i>regexec()</i>is called, the locale is different from when the regular expression wascompiled, the result is undefined.<p>If REG_NEWLINE is not set in<i>cflags</i>,then a newline character in<i>pattern</i>or<i>string</i>will be treated as an ordinary character.If REG_NEWLINE is set, then newlinewill be treated as an ordinary character except as follows:<ol><p><li>A newline character in<i>string</i>will not be matched by a period outside a bracket expressionor by any form of a non-matching list (see the <b>XBD</b> specification, <a href="../xbd/re.html"><b>Regular Expressions</b>&nbsp;</a>).<p><li>A circumflex (^) in<i>pattern</i>,when used to specify expression anchoring(see the <b>XBD</b> specification, <a href="../xbd/re.html#tag_007_003_008"><b>BRE Expression Anchoring</b>&nbsp;</a>),will match the zero-length string immediately after a newline in<i>string</i>,regardless of the setting of REG_NOTBOL.<p><li>A dollar-sign ($) in<i>pattern</i>,when used to specify expressionanchoring, will match the zero-length string immediately before anewline in<i>string</i>,regardless of the setting of REG_NOTEOL.<p></ol><p>The<i>regfree()</i>function frees any memory allocated by<i>regcomp()</i>associated with<i>preg</i>.<p>The following constants are defined as error return values:<dl compact><dt>REG_NOMATCH<dd><i>regexec()</i>failed to match.<dt>REG_BADPAT<dd>Invalid regular expression.<dt>REG_ECOLLATE<dd>Invalid collating element referenced.<dt>REG_ECTYPE<dd>Invalid character class type referenced.<dt>REG_EESCAPE<dd>Trailing \ in pattern.<dt>REG_ESUBREG<dd>Number in \<i>digit</i> invalid or in error.<dt>REG_EBRACK<dd>[ ] imbalance.<dt>REG_ENOSYS<dd>The function is not supported.<dt>REG_EPAREN<dd>\( \) or ( ) imbalance.<dt>REG_EBRACE<dd>\{ \} imbalance.<dt>REG_BADBR<dd>Content of \{ \} invalid:not a number, number too large, more than two numbers, firstlarger than second.<dt>REG_ERANGE<dd>Invalid endpoint in range expression.<dt>REG_ESPACE<dd>Out of memory.<dt>REG_BADRPT<dd>?, * or + not preceded by valid regular expression.</dl><p>The<i>regerror()</i>function provides a mapping from error codesreturned by<i>regcomp()</i>and<i>regexec()</i>to unspecified printable strings.It generates a string correspondingto the value of the<i>errcode</i>argument, which must be 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, thecontent of the generated string is unspecified.<p>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.<p>If the<i>errbuf_size</i>argument is not 0,<i>regerror()</i>will place the generated string into thebuffer of size<i>errbuf_size</i>bytes pointed to by<i>errbuf</i>.If the string (including theterminating null) cannot fit in the buffer,<i>regerror()</i>will truncate the string and null-terminate the result.<p>If<i>errbuf_size</i>is 0,<i>regerror()</i>ignores the<i>errbuf</i>argument, and returns the size of the buffer needed to hold thegenerated string.<p>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 itis given to<i>regfree()</i>.</blockquote><h4><a name = "tag_000_008_069">&nbsp;</a>RETURN VALUE</h4><blockquote>On successful completion, the<i>regcomp()</i>function returns 0.Otherwise, it returns an integer value indicating an error as describedin<i><a href="regex.h.html">&lt;regex.h&gt;</a></i>,and the content of<i>preg</i>is undefined.<p>On successful completion, the<i>regexec()</i>function returns 0.  Otherwise it returns REG_NOMATCH to indicate no match, orREG_ENOSYS to indicate that the function is not supported.<p>Upon successful completion, the<i>regerror()</i>function returns the number of bytesneeded to hold the entire generated string.Otherwise, it returns 0 to indicate that the function is not implemented.<p>The<i>regfree()</i>function returns no value.</blockquote><h4><a name = "tag_000_008_070">&nbsp;</a>ERRORS</h4><blockquote>No errors are defined.</blockquote><h4><a name = "tag_000_008_071">&nbsp;</a>EXAMPLES</h4><blockquote><pre><code>#include &lt;regex.h&gt;/* * Match string against the extended regular expression in * pattern, treating errors as no match. * * return 1 for match, 0 for no match */intmatch(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);}</code></pre><p>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.)<pre><code>(void) regcomp (&amp;re, pattern, 0);/* this call to regexec() finds the first match on the line */error = regexec (&amp;re, &amp;buffer[0], 1, &amp;pm, 0);while (error == 0) {    /* while matches found */    /* substring found between pm.rm_so and pm.rm_eo */    /* This call to regexec() finds the next match */    error = regexec (&amp;re, buffer + pm.rm_eo, 1, &amp;pm, REG_NOTBOL);}</code></pre></blockquote><h4><a name = "tag_000_008_072">&nbsp;</a>APPLICATION USAGE</h4><blockquote>An application could use:<pre><code>regerror(code,preg,(char&nbsp;*)NULL,(size_t)0)</code></pre>to find out how big a buffer is needed for the generated string,<i><a href="malloc.html">malloc()</a></i>a buffer to hold the string, and then call<i>regerror()</i>again to get the string.Alternatively, it could allocate afixed, static buffer that is big enough to hold most strings, and then use<i><a href="malloc.html">malloc()</a></i>to allocate a larger buffer if it finds that this is too small.<p>To match a pattern as described in the <b>XCU</b> specification, <b>Section 2.13</b>, <b>Pattern Matching Notation</b>use the<i><a href="fnmatch.html">fnmatch()</a></i>function.</blockquote><h4><a name = "tag_000_008_073">&nbsp;</a>FUTURE DIRECTIONS</h4><blockquote>None.</blockquote><h4><a name = "tag_000_008_074">&nbsp;</a>SEE ALSO</h4><blockquote><i><a href="fnmatch.html">fnmatch()</a></i>,<i><a href="glob.html">glob()</a></i>,<i><a href="regex.h.html">&lt;regex.h&gt;</a></i>,<i><a href="systypes.h.html">&lt;sys/types.h&gt;</a></i>.</blockquote><h4>DERIVATION</h4><blockquote>Derived from the ISO POSIX-2 standard.</blockquote><hr size=2 noshade><center><font size=2>UNIX &reg; is a registered Trademark of The Open Group.<br>Copyright &copy; 1997 The Open Group<br> [ <a href="../index.html">Main Index</a> | <a href="../xshix.html">XSH</a> | <a href="../xcuix.html">XCU</a> | <a href="../xbdix.html">XBD</a> | <a href="../cursesix.html">XCURSES</a> | <a href="../xnsix.html">XNS</a> ]</font></center><hr size=2 noshade></body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -