📄 regex.3
字号:
.\" Copyright (c) 1992, 1993, 1994 Henry Spencer..\" Copyright (c) 1992, 1993, 1994.\" The Regents of the University of California. All rights reserved..\".\" This code is derived from software contributed to Berkeley by.\" Henry Spencer..\".\" Redistribution and use in source and binary forms, with or without.\" modification, are permitted provided that the following conditions.\" are met:.\" 1. Redistributions of source code must retain the above copyright.\" notice, this list of conditions and the following disclaimer..\" 2. Redistributions in binary form must reproduce the above copyright.\" notice, this list of conditions and the following disclaimer in the.\" documentation and/or other materials provided with the distribution..\" 3. All advertising materials mentioning features or use of this software.\" must display the following acknowledgement:.\" This product includes software developed by the University of.\" California, Berkeley and its contributors..\" 4. Neither the name of the University nor the names of its contributors.\" may be used to endorse or promote products derived from this software.\" without specific prior written permission..\".\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION).\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF.\" SUCH DAMAGE..\".\" @(#)regex.3 8.4 (Berkeley) 3/20/94.\".TH REGEX 3 "March 20, 1994".de ZR.\" one other place knows this name: the SEE ALSO section.IR re_format (7) \\$1...SH NAMEregcomp, regexec, regerror, regfree \- regular-expression library.SH SYNOPSIS.ft B.\".na#include <sys/types.h>.br#include <regex.h>.HP 10int regcomp(regex_t\ *preg, const\ char\ *pattern, int\ cflags);.HPint\ regexec(const\ regex_t\ *preg, const\ char\ *string,size_t\ nmatch, regmatch_t\ pmatch[], int\ eflags);.HPsize_t\ regerror(int\ errcode, const\ regex_t\ *preg,char\ *errbuf, size_t\ errbuf_size);.HPvoid\ regfree(regex_t\ *preg);.\".ad.ft.SH DESCRIPTIONThese routines implement POSIX 1003.2 regular expressions (``RE''s);see.ZR ..I Regcompcompiles an RE written as a string into an internal form,.I regexecmatches that internal form against a string and reports results,.I regerrortransforms error codes from either into human-readable messages,and.I regfreefrees any dynamically-allocated storage used by the internal formof an RE..PPThe header.I <regex.h>declares two structure types,.I regex_tand.IR regmatch_t ,the former for compiled internal forms and the latter for match reporting.It also declares the four functions,a type.IR regoff_t ,and a number of constants with names starting with ``REG_''..PP.I Regcompcompiles the regular expression contained in the.I patternstring,subject to the flags in.IR cflags ,and places the results in the.I regex_tstructure pointed to by.IR preg ..I Cflagsis the bitwise OR of zero or more of the following flags:.IP REG_EXTENDED \w'REG_EXTENDED'u+2nCompile modern (``extended'') REs,rather than the obsolete (``basic'') REs thatare the default..IP REG_BASICThis is a synonym for 0,provided as a counterpart to REG_EXTENDED to improve readability..IP REG_NOSPECCompile with recognition of all special characters turned off.All characters are thus considered ordinary,so the ``RE'' is a literal string.This is an extension,compatible with but not specified by POSIX 1003.2,and should be used withcaution in software intended to be portable to other systems.REG_EXTENDED and REG_NOSPEC may not be usedin the same call to.IR regcomp ..IP REG_ICASECompile for matching that ignores upper/lower case distinctions.See.ZR ..IP REG_NOSUBCompile for matching that need only report success or failure,not what was matched..IP REG_NEWLINECompile for newline-sensitive matching.By default, newline is a completely ordinary character with no specialmeaning in either REs or strings.With this flag,`[^' bracket expressions and `.' never match newline,a `^' anchor matches the null string after any newline in the stringin addition to its normal function,and the `$' anchor matches the null string before any newline in thestring in addition to its normal function..IP REG_PENDThe regular expression ends,not at the first NUL,but just before the character pointed to by the.I re_endpmember of the structure pointed to by.IR preg .The.I re_endpmember is of type.IR const\ char\ * .This flag permits inclusion of NULs in the RE;they are considered ordinary characters.This is an extension,compatible with but not specified by POSIX 1003.2,and should be used withcaution in software intended to be portable to other systems..PPWhen successful,.I regcompreturns 0 and fills in the structure pointed to by.IR preg .One member of that structure(other than.IR re_endp )is publicized:.IR re_nsub ,of type.IR size_t ,contains the number of parenthesized subexpressions within the RE(except that the value of this member is undefined if theREG_NOSUB flag was used).If.I regcompfails, it returns a non-zero error code;see DIAGNOSTICS..PP.I Regexecmatches the compiled RE pointed to by.I pregagainst the.IR string ,subject to the flags in.IR eflags ,and reports results using.IR nmatch ,.IR pmatch ,and the returned value.The RE must have been compiled by a previous invocation of.IR regcomp .The compiled form is not altered during execution of.IR regexec ,so a single compiled RE can be used simultaneously by multiple threads..PPBy default,the NUL-terminated string pointed to by.I stringis considered to be the text of an entire line, minus any terminatingnewline.The.I eflagsargument is the bitwise OR of zero or more of the following flags:.IP REG_NOTBOL \w'REG_STARTEND'u+2nThe first character ofthe stringis not the beginning of a line, so the `^' anchor should not match before it.This does not affect the behavior of newlines under REG_NEWLINE..IP REG_NOTEOLThe NUL terminatingthe stringdoes not end a line, so the `$' anchor should not match before it.This does not affect the behavior of newlines under REG_NEWLINE..IP REG_STARTENDThe string is considered to start at\fIstring\fR\ + \fIpmatch\fR[0].\fIrm_so\fRand to have a terminating NUL located at\fIstring\fR\ + \fIpmatch\fR[0].\fIrm_eo\fR(there need not actually be a NUL at that location),regardless of the value of.IR nmatch .See below for the definition of.IR pmatchand.IR nmatch .This is an extension,compatible with but not specified by POSIX 1003.2,and should be used withcaution in software intended to be portable to other systems.Note that a non-zero \fIrm_so\fR does not imply REG_NOTBOL;REG_STARTEND affects only the location of the string,not how it is matched..PPSee.ZRfor a discussion of what is matched in situations where an RE or aportion thereof could match any of several substrings of.IR string ..PPNormally,.I regexecreturns 0 for success and the non-zero code REG_NOMATCH for failure.Other non-zero error codes may be returned in exceptional situations;see DIAGNOSTICS..PPIf REG_NOSUB was specified in the compilation of the RE,or if.I nmatchis 0,.I regexecignores the.I pmatchargument (but see below for the case where REG_STARTEND is specified).Otherwise,.I pmatchpoints to an array of.I nmatchstructures of type.IR regmatch_t .Such a structure has at least the members.I rm_soand.IR rm_eo ,both of type.I regoff_t(a signed arithmetic type at least as large as an.I off_tand a.IR ssize_t ),containing respectively the offset of the first character of a substringand the offset of the first character after the end of the substring.Offsets are measured from the beginning of the.I stringargument given to.IR regexec .
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -