📄 library_9.html
字号:
expression into it by calling <CODE>regcomp</CODE>.<P><A NAME="IDX400"></A><U>Function:</U> int <B>regcomp</B> <I>(regex_t *<VAR>compiled</VAR>, const char *<VAR>pattern</VAR>, int <VAR>cflags</VAR>)</I><P>The function <CODE>regcomp</CODE> "compiles" a regular expression into adata structure that you can use with <CODE>regexec</CODE> to match against astring. The compiled regular expression format is designed forefficient matching. <CODE>regcomp</CODE> stores it into <CODE>*<VAR>compiled</VAR></CODE>.<P>It's up to you to allocate an object of type <CODE>regex_t</CODE> and pass itsaddress to <CODE>regcomp</CODE>.<P>The argument <VAR>cflags</VAR> lets you specify various options that controlthe syntax and semantics of regular expressions. See section <A HREF="library_9.html#SEC98" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_9.html#SEC98">Flags for POSIX Regular Expressions</A>.<P>If you use the flag <CODE>REG_NOSUB</CODE>, then <CODE>regcomp</CODE> omits fromthe compiled regular expression the information necessary to recordhow subexpressions actually match. In this case, you might as wellpass <CODE>0</CODE> for the <VAR>matchptr</VAR> and <VAR>nmatch</VAR> arguments whenyou call <CODE>regexec</CODE>.<P>If you don't use <CODE>REG_NOSUB</CODE>, then the compiled regular expressiondoes have the capacity to record how subexpressions match. Also,<CODE>regcomp</CODE> tells you how many subexpressions <VAR>pattern</VAR> has, bystoring the number in <CODE><VAR>compiled</VAR>->re_nsub</CODE>. You can use thatvalue to decide how long an array to allocate to hold information aboutsubexpression matches.<P><CODE>regcomp</CODE> returns <CODE>0</CODE> if it succeeds in compiling the regularexpression; otherwise, it returns a nonzero error code (see the tablebelow). You can use <CODE>regerror</CODE> to produce an error message stringdescribing the reason for a nonzero value; see section <A HREF="library_9.html#SEC102" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_9.html#SEC102">POSIX Regexp Matching Cleanup</A>.<P>Here are the possible nonzero values that <CODE>regcomp</CODE> can return:<P><DL COMPACT><DT><CODE>REG_BADBR</CODE><DD>There was an invalid <SAMP>`\{...\}'</SAMP> construct in the regularexpression. A valid <SAMP>`\{...\}'</SAMP> construct must contain eithera single number, or two numbers in increasing order separated by acomma.<P><DT><CODE>REG_BADPAT</CODE><DD>There was a syntax error in the regular expression.<P><DT><CODE>REG_BADRPT</CODE><DD>A repetition operator such as <SAMP>`?'</SAMP> or <SAMP>`*'</SAMP> appeared in a badposition (with no preceding subexpression to act on).<P><DT><CODE>REG_ECOLLATE</CODE><DD>The regular expression referred to an invalid collating element (one notdefined in the current locale for string collation). See section <A HREF="library_7.html#SEC79" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_7.html#SEC79">Categories of Activities that Locales Affect</A>.<P><DT><CODE>REG_ECTYPE</CODE><DD>The regular expression referred to an invalid character class name.<P><DT><CODE>REG_EESCAPE</CODE><DD>The regular expression ended with <SAMP>`\'</SAMP>.<P><DT><CODE>REG_ESUBREG</CODE><DD>There was an invalid number in the <SAMP>`\<VAR>digit</VAR>'</SAMP> construct.<P><DT><CODE>REG_EBRACK</CODE><DD>There were unbalanced square brackets in the regular expression.<P><DT><CODE>REG_EPAREN</CODE><DD>An extended regular expression had unbalanced parentheses,or a basic regular expression had unbalanced <SAMP>`\('</SAMP> and <SAMP>`\)'</SAMP>.<P><DT><CODE>REG_EBRACE</CODE><DD>The regular expression had unbalanced <SAMP>`\{'</SAMP> and <SAMP>`\}'</SAMP>.<P><DT><CODE>REG_ERANGE</CODE><DD>One of the endpoints in a range expression was invalid.<P><DT><CODE>REG_ESPACE</CODE><DD><CODE>regcomp</CODE> or <CODE>regexec</CODE> ran out of memory.</DL><P><H3><A NAME="SEC98" HREF="library_toc.html#SEC98" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC98">Flags for POSIX Regular Expressions</A></H3><P>These are the bit flags that you can use in the <VAR>cflags</VAR> operand whencompiling a regular expression with <CODE>regcomp</CODE>. <DL COMPACT><DT><CODE>REG_EXTENDED</CODE><DD>Treat the pattern as an extended regular expression, rather than as abasic regular expression.<P><DT><CODE>REG_ICASE</CODE><DD>Ignore case when matching letters.<P><DT><CODE>REG_NOSUB</CODE><DD>Don't bother storing the contents of the <VAR>matches_ptr</VAR> array.<P><DT><CODE>REG_NEWLINE</CODE><DD>Treat a newline in <VAR>string</VAR> as dividing <VAR>string</VAR> into multiplelines, so that <SAMP>`$'</SAMP> can match before the newline and <SAMP>`^'</SAMP> canmatch after. Also, don't permit <SAMP>`.'</SAMP> to match a newline, and don'tpermit <SAMP>`[^...]'</SAMP> to match a newline.<P>Otherwise, newline acts like any other ordinary character.</DL><P><H3><A NAME="SEC99" HREF="library_toc.html#SEC99" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC99">Matching a Compiled POSIX Regular Expression</A></H3><P>Once you have compiled a regular expression, as described in section <A HREF="library_9.html#SEC97" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_9.html#SEC97">POSIX Regular Expression 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>`^'</SAMP> or<SAMP>`$'</SAMP>).<P><A NAME="IDX401"></A><U>Function:</U> int <B>regexec</B> <I>(regex_t *<VAR>compiled</VAR>, char *<VAR>string</VAR>, size_t <VAR>nmatch</VAR>, regmatch_t <VAR>matchptr</VAR> <TT>[]</TT>, int <VAR>eflags</VAR>)</I><P>This function tries to match the compiled regular expression<CODE>*<VAR>compiled</VAR></CODE> 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 forwhat nonzero values mean. You can use <CODE>regerror</CODE> to produce anerror message string describing the reason for a nonzero value; see section <A HREF="library_9.html#SEC102" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_9.html#SEC102">POSIX Regexp Matching Cleanup</A>.<P>The argument <VAR>eflags</VAR> is a word of bit flags that enable variousoptions.<P>If you want to get information about what part of <VAR>string</VAR> actuallymatched 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 section <A HREF="library_9.html#SEC100" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_9.html#SEC100">Subexpressions Match Results</A>.<P>You must match the regular expression with the same set of currentlocales 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:<P><DL COMPACT><DT><CODE>REG_NOTBOL</CODE><DD>Do not regard the beginning of the specified string as the beginning ofa line; more generally, don't make any assumptions about what text mightprecede it.<P><DT><CODE>REG_NOTEOL</CODE><DD>Do not regard the end of the specified string as the end of a line; moregenerally, 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:<P><DL COMPACT><DT><CODE>REG_NOMATCH</CODE><DD>The pattern didn't match the string. This isn't really an error.<P><DT><CODE>REG_ESPACE</CODE><DD><CODE>regcomp</CODE> or <CODE>regexec</CODE> ran out of memory.</DL><P><H3><A NAME="SEC100" HREF="library_toc.html#SEC100" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC100">Subexpressions Match Results</A></H3><P>When <CODE>regexec</CODE> matches parenthetical subexpressions of<VAR>pattern</VAR>, it records which parts of <VAR>string</VAR> they match. Itreturns that information by storing the offsets into an array whoseelements are structures of type <CODE>regmatch_t</CODE>. The first element ofthe array records the part of the string that matched the entire regularexpression. Each other element of the array records the beginning andend of the part that matched a single parenthetical subexpression.<P><A NAME="IDX402"></A><U>Data Type:</U> <B>regmatch_t</B><P>This is the data type of the <VAR>matcharray</VAR> array that you pass to<CODE>regexec</CODE>. It containes two structure fields, as follows:<P><DL COMPACT><DT><CODE>rm_so</CODE><DD>The offset in <VAR>string</VAR> of the beginning of a substring. Add thisvalue to <VAR>string</VAR> to get the address of that part.<P><DT><CODE>rm_eo</CODE><DD>The offset in <VAR>string</VAR> of the end of the substring.</DL><P><A NAME="IDX403"></A><U>Data Type:</U> <B>regoff_t</B><P><CODE>regoff_t</CODE> is an alias for another signed integer type.The fields of <CODE>regmatch_t</CODE> have type <CODE>regoff_t</CODE>.<P>The <CODE>regmatch_t</CODE> elements correspond to subexpressionspositionally; the first element records where the first subexpressionmatched, the second element records the second subexpression, and so on.The order of the subexpressions is the order in which they begin.<P>When you call <CODE>regexec</CODE>, you specify how long the <VAR>matchptr</VAR>array is, with the <VAR>nmatch</VAR> argument. This tells <CODE>regexec</CODE> howmany elements to store. If the actual regular expression has more than<VAR>nmatch</VAR> subexpressions, then you won't get offset information aboutthe rest of them. But this doesn't alter whether the pattern matches aparticular string or not.<P>If you don't want <CODE>regexec</CODE> to return any information about wherethe subexpressions matched, you can either supply <CODE>0</CODE> for<VAR>nmatch</VAR>, or use the flag <CODE>REG_NOSUB</CODE> when you compile thepattern with <CODE>regcomp</CODE>.<P><H3><A NAME="SEC101" HREF="library_toc.html#SEC101" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC101">Complications in Subexpression Matching</A></H3><P>Sometimes a subexpression matches a substring of no characters. Thishappens when <SAMP>`f\(o*\)'</SAMP> matches the string <SAMP>`fum'</SAMP>. (It reallymatches just the <SAMP>`f'</SAMP>.) In this case, both of the offsets identifythe point in the string where the null substring was found. In thisexample, the offsets are both <CODE>1</CODE>.<P>Sometimes the entire regular expression can match without using some ofits subexpressions at all--for example, when <SAMP>`ba\(na\)*'</SAMP> matches thestring <SAMP>`ba'</SAMP>, the parenthetical subexpression is not used. Whenthis happens, <CODE>regexec</CODE> stores <CODE>-1</CODE> in both fields of theelement for that subexpression.<P>Sometimes matching the entire regular expression can match a particularsubexpression more than once--for example, when <SAMP>`ba\(na\)*'</SAMP>matches the string <SAMP>`bananana'</SAMP>, the parenthetical subexpressionmatches three times. When this happens, <CODE>regexec</CODE> usually storesthe offsets of the last part of the string that matched thesubexpression. In the case of <SAMP>`bananana'</SAMP>, these offsets are<CODE>6</CODE> and <CODE>8</CODE>.<P>But the last match is not always the one that is chosen. It's moreaccurate to say that the last <EM>opportunity</EM> to match is the onethat takes precedence. What this means is that when one subexpressionappears within another, then the results reported for the innersubexpression reflect whatever happened on the last match of the outersubexpression. For an example, consider <SAMP>`\(ba\(na\)*s \)'</SAMP> matchingthe string <SAMP>`bananas bas '</SAMP>. The last time the inner expressionactually matches is near the end of the first word. But it is <EM>considered</EM> again in the second word, and fails to match there.<CODE>regexec</CODE> reports nonuse of the "na" subexpression.<P>Another place where this rule applies is when <SAMP>`\(ba\(na\)*s\|nefer\(ti\)* \)*'</SAMP> matches <SAMP>`bananas nefertiti'</SAMP>. The "na"subexpression does match in the first word, but it doesn't match in thesecond word because the other alternative is used there. Once again,the second repetition of the outer subexpression overrides the first,and within that second repetition, the "na" subexpression is not used.So <CODE>regexec</CODE> reports nonuse of the "na" subexpression.<P><H3><A NAME="SEC102" HREF="library_toc.html#SEC102" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC102">POSIX Regexp Matching Cleanup</A></H3><P>When you are finished using a compiled regular expression, you canfree the storage it uses by calling <CODE>regfree</CODE>.<P><A NAME="IDX404"></A><U>Function:</U> void <B>regfree</B> <I>(regex_t *<VAR>compiled</VAR>)</I><P>Calling <CODE>regfree</CODE> frees all the storage that <CODE>*<VAR>compiled</VAR></CODE>points to. This includes various internal fields of the <CODE>regex_t</CODE>structure that aren't documented in this manual.<P><CODE>regfree</CODE> does not free the object <CODE>*<VAR>compiled</VAR></CODE> itself.<P>You should always free the space in a <CODE>regex_t</CODE> structure with<CODE>regfree</CODE> before using the structure to compile another regularexpression.<P>When <CODE>regcomp</CODE> or <CODE>regexec</CODE> reports an error, you can usethe function <CODE>regerror</CODE> to turn it into an error message string.<P><A NAME="IDX405"></A><U>Function:</U> size_t <B>regerror</B> <I>(int <VAR>errcode</VAR>, regex_t *<VAR>compiled</VAR>, char *<VAR>buffer</VAR>, size_t <VAR>length</VAR>)</I><P>This function produces an error message string for the error code<VAR>errcode</VAR>, and stores the string in <VAR>length</VAR> bytes of memorystarting at <VAR>buffer</VAR>. For the <VAR>compiled</VAR> argument, supply thesame compiled regular expression structure that <CODE>regcomp</CODE> or<CODE>regexec</CODE> was working with when it got the error. Alternatively,you can supply <CODE>NULL</CODE> for <VAR>compiled</VAR>; you will still get ameaningful error message, but it might not be as detailed.<P>If the error message can't fit in <VAR>length</VAR> bytes (including aterminating null character), then <CODE>regerror</CODE> truncates it.The string that <CODE>regerror</CODE> stores is always null-terminatedeven if it has been truncated.<P>The return value of <CODE>regerror</CODE> is the minimum length needed tostore the entire error message. If this is less than <VAR>length</VAR>, thenthe error message was not truncated, and you can use it. Otherwise, youshould call <CODE>regerror</CODE> again with a larger buffer.<P><PRE>char *get_regerror (int errcode, regex_t *compiled){ size_t length = regerror (errcode, compiled, NULL, 0);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -