| <html lang="en"> |
| <head> |
| <title>Matching POSIX Regexps - The GNU C Library</title> |
| <meta http-equiv="Content-Type" content="text/html"> |
| <meta name="description" content="The GNU C Library"> |
| <meta name="generator" content="makeinfo 4.13"> |
| <link title="Top" rel="start" href="index.html#Top"> |
| <link rel="up" href="Regular-Expressions.html#Regular-Expressions" title="Regular Expressions"> |
| <link rel="prev" href="Flags-for-POSIX-Regexps.html#Flags-for-POSIX-Regexps" title="Flags for POSIX Regexps"> |
| <link rel="next" href="Regexp-Subexpressions.html#Regexp-Subexpressions" title="Regexp Subexpressions"> |
| <link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> |
| <!-- |
| This file documents the GNU C library. |
| |
| This is Edition 0.12, last updated 2007-10-27, |
| of `The GNU C Library Reference Manual', for version |
| 2.8 (Sourcery G++ Lite 2011.03-41). |
| |
| Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2001, 2002, |
| 2003, 2007, 2008, 2010 Free Software Foundation, Inc. |
| |
| Permission is granted to copy, distribute and/or modify this document |
| under the terms of the GNU Free Documentation License, Version 1.3 or |
| any later version published by the Free Software Foundation; with the |
| Invariant Sections being ``Free Software Needs Free Documentation'' |
| and ``GNU Lesser General Public License'', the Front-Cover texts being |
| ``A GNU Manual'', and with the Back-Cover Texts as in (a) below. A |
| copy of the license is included in the section entitled "GNU Free |
| Documentation License". |
| |
| (a) The FSF's Back-Cover Text is: ``You have the freedom to |
| copy and modify this GNU manual. Buying copies from the FSF |
| supports it in developing GNU and promoting software freedom.''--> |
| <meta http-equiv="Content-Style-Type" content="text/css"> |
| <style type="text/css"><!-- |
| pre.display { font-family:inherit } |
| pre.format { font-family:inherit } |
| pre.smalldisplay { font-family:inherit; font-size:smaller } |
| pre.smallformat { font-family:inherit; font-size:smaller } |
| pre.smallexample { font-size:smaller } |
| pre.smalllisp { font-size:smaller } |
| span.sc { font-variant:small-caps } |
| span.roman { font-family:serif; font-weight:normal; } |
| span.sansserif { font-family:sans-serif; font-weight:normal; } |
| --></style> |
| <link rel="stylesheet" type="text/css" href="../cs.css"> |
| </head> |
| <body> |
| <div class="node"> |
| <a name="Matching-POSIX-Regexps"></a> |
| <p> |
| Next: <a rel="next" accesskey="n" href="Regexp-Subexpressions.html#Regexp-Subexpressions">Regexp Subexpressions</a>, |
| Previous: <a rel="previous" accesskey="p" href="Flags-for-POSIX-Regexps.html#Flags-for-POSIX-Regexps">Flags for POSIX Regexps</a>, |
| Up: <a rel="up" accesskey="u" href="Regular-Expressions.html#Regular-Expressions">Regular Expressions</a> |
| <hr> |
| </div> |
| |
| <h4 class="subsection">10.3.3 Matching a Compiled POSIX Regular Expression</h4> |
| |
| <p>Once you have compiled a regular expression, as described in <a href="POSIX-Regexp-Compilation.html#POSIX-Regexp-Compilation">POSIX Regexp Compilation</a>, you can match it against strings using |
| <code>regexec</code>. A match anywhere inside the string counts as success, |
| unless the regular expression contains anchor characters (‘<samp><span class="samp">^</span></samp>’ or |
| ‘<samp><span class="samp">$</span></samp>’). |
| |
| <!-- regex.h --> |
| <!-- POSIX.2 --> |
| <div class="defun"> |
| — Function: int <b>regexec</b> (<var>const regex_t *restrict compiled, const char *restrict string, size_t nmatch, regmatch_t matchptr</var>[<var>restrict</var>]<var>, int eflags</var>)<var><a name="index-regexec-879"></a></var><br> |
| <blockquote><p>This function tries to match the compiled regular expression |
| <code>*</code><var>compiled</var> against <var>string</var>. |
| |
| <p><code>regexec</code> returns <code>0</code> if the regular expression matches; |
| otherwise, it returns a nonzero value. See the table below for |
| what nonzero values mean. You can use <code>regerror</code> to produce an |
| error message string describing the reason for a nonzero value; |
| see <a href="Regexp-Cleanup.html#Regexp-Cleanup">Regexp Cleanup</a>. |
| |
| <p>The argument <var>eflags</var> is a word of bit flags that enable various |
| options. |
| |
| <p>If you want to get information about what part of <var>string</var> actually |
| matched the regular expression or its subexpressions, use the arguments |
| <var>matchptr</var> and <var>nmatch</var>. Otherwise, pass <code>0</code> for |
| <var>nmatch</var>, and <code>NULL</code> for <var>matchptr</var>. See <a href="Regexp-Subexpressions.html#Regexp-Subexpressions">Regexp Subexpressions</a>. |
| </p></blockquote></div> |
| |
| <p>You must match the regular expression with the same set of current |
| locales that were in effect when you compiled the regular expression. |
| |
| <p>The function <code>regexec</code> accepts the following flags in the |
| <var>eflags</var> argument: |
| |
| <dl> |
| <!-- regex.h --> |
| <!-- POSIX.2 --> |
| <dt><code>REG_NOTBOL</code><dd>Do not regard the beginning of the specified string as the beginning of |
| a line; more generally, don't make any assumptions about what text might |
| precede it. |
| |
| <!-- regex.h --> |
| <!-- POSIX.2 --> |
| <br><dt><code>REG_NOTEOL</code><dd>Do not regard the end of the specified string as the end of a line; more |
| generally, don't make any assumptions about what text might follow it. |
| </dl> |
| |
| <p>Here are the possible nonzero values that <code>regexec</code> can return: |
| |
| <dl> |
| <!-- regex.h --> |
| <!-- POSIX.2 --> |
| <dt><code>REG_NOMATCH</code><dd>The pattern didn't match the string. This isn't really an error. |
| |
| <!-- regex.h --> |
| <!-- POSIX.2 --> |
| <br><dt><code>REG_ESPACE</code><dd><code>regexec</code> ran out of memory. |
| </dl> |
| |
| </body></html> |
| |