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

📄 regexp.html

📁 unix 下的C开发手册,还用详细的例程。
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><html><head><!-- Copyright 1997 The Open Group, All Rights Reserved --><title>regexp</title></head><body bgcolor=white><center><font size=2>The Single UNIX &reg; Specification, Version 2<br>Copyright &copy; 1997 The Open Group</font></center><hr size=2 noshade><h4><a name = "tag_000_008_057">&nbsp;</a>NAME</h4><blockquote>advance, compile, step, loc1, loc2, locs - compileand match regular expressions (<b>LEGACY</b>)</blockquote><h4><a name = "tag_000_008_058">&nbsp;</a>SYNOPSIS</h4><blockquote><pre><code>#define INIT <i>declarations</i>#define GETC() getc <i>code</i>#define PEEKC() peek <i>code</i>#define UNGETC() ungetc <i>code</i>#define RETURN(ptr) return <i>code</i>#define ERROR(val) error <i>code</i>#include &lt;<a href="regexp.h.html">regexp.h</a>&gt;char *compile(char *<i>instring</i>, char *<i>expbuf</i>,    const char *<i>endbuf</i>, int <i>eof</i>);int step(const char *<i>string</i>, const char *<i>expbuf</i>);int advance(const char *<i>string</i>, const char *<i>expbuf</i>);extern char *loc1, *loc2, *locs;</code></pre></blockquote><h4><a name = "tag_000_008_059">&nbsp;</a>DESCRIPTION</h4><blockquote>These are general-purpose, regular expression-matchingfunctions to be used in programs that perform regular expressionmatching, using the Regular Expressions described in<xref href=simpreg><a href="#tag_000_008_059_001">Simple Regular Expressions (Historical Version)</a></xref>.These functions are defined by the<i><a href="regexp.h.html">&lt;regexp.h&gt;</a></i>header.<p>Implementations may also accept internationalised simple regularexpressions as input.<p>Programs must have the following five macros declared before the<b>#include</b><i><a href="regexp.h.html">&lt;regexp.h&gt;</a></i>statement.These macros are used by<i><a href="compile.html">compile()</a></i>.The macros GETC(), PEEKC() and UNGETC()operate on the regular expression given as input to<i><a href="compile.html">compile()</a></i>.<dl compact><dt>GETC()<dd>This macro returns the value of the next character (byte) in theregular expression pattern.Successive calls to GETC() should return successive characters ofthe regular expression.<dt>PEEKC()<dd>This macro returns the next character (byte) in the regular expression.Immediately successive calls to PEEKC() should return the same byte,which should also be the next character returned by GETC().<dt>UNGETC(<i>c</i>)<dd>This macro causes the argument<i>c</i>to be returned by the next call to GETC() and PEEKC().No more than one character of pushback is ever needed and thischaracter is guaranteed to be the last character read by GETC().The value of the macro UNGETC(<i>c</i>) is always ignored.<dt>RETURN(<i>ptr</i>)<dd>This macro is used on normal exit of the<i><a href="compile.html">compile()</a></i>function.  The value of the argument<i>ptr</i>is a pointer to the character after the last character of the compiledregular expression.This is useful to programs that have memory allocation to manage.<dt>ERROR(<i>val</i>)<dd>This macro is the abnormal return from<i><a href="compile.html">compile()</a></i>.The argument<i>val</i>is an error number (see the <b>ERRORS</b> section below for meanings).This call should never return.</dl><p>The<i><a href="step.html">step()</a></i>and<i><a href="advance.html">advance()</a></i>functions do pattern matching given a character string and a compiledregular expression as input.<p>The<i><a href="compile.html">compile()</a></i>function takes as input a simple regular expression (see<xref href=simpreg><a href="#tag_000_008_059_001">Simple Regular Expressions (Historical Version)</a></xref>)and produces a compiled expression that can be used with<i><a href="step.html">step()</a></i>and<i><a href="advance.html">advance()</a></i>.<p>The first parameter<i>instring</i>is never used explicitly by<i><a href="compile.html">compile()</a></i>but is useful for programs that pass down different pointersto input characters.  It is sometimes used in the INIT declaration (see below).Programs which invoke functions to input characters or have characters in anexternal array can pass down (<b>char*</b>)0 for this parameter.<p>The next parameter<i>expbuf</i>is a character pointer.It points to the place where the compiled regular expression will beplaced.<p>The parameter<i>endbuf</i>is one more than the highest address where the compiledregular expression may be placed.If the compiled expression cannot fit in(<i>endbuf-expbuf</i>)bytes, a call to ERROR(50) is made.<p>The parameter<i>eof</i>is the character which marks the end of the regular expression.<p>Each program that includes the<i><a href="regexp.h.html">&lt;regexp.h&gt;</a></i>header must have a<b>#define</b>statement for INIT.It is used for dependent declarations and initialisations.Most often it is used to set a register variable to point to thebeginning of the regular expression so that this register variable canbe used in the declarations for GETC(), PEEKC() and UNGETC().Otherwise it can be used to declare external variables that might beused by GETC(), PEEKC() and UNGETC().See the EXAMPLES section below.<p>The first parameter to<i><a href="step.html">step()</a></i>is a pointer to a string of characters to be checked for a match.This string should be null-terminated.<p>The second parameter,<i>expbuf</i>,is the compiled regular expression which was obtained by a call to<i>compile</i>.<p>The<i><a href="step.html">step()</a></i>function returns non-zero if some substring of<i>string</i>matches the regular expression in<i>expbuf</i>,and 0, if there is no match.If there is a match, two external character pointers are set as a sideeffect to the call to<i><a href="step.html">step()</a></i>.The variable<i>loc1</i>points to the first character that matched the regular expression;the variable<i>loc2</i>points to the character after the last character that matches theregular expression.Thus if the regular expression matches the entire input string,<i>loc1</i>will point to the first character of<i>string</i>and<i>loc2</i>will point to the null at the end of<i>string</i>.<p>The<i><a href="advance.html">advance()</a></i>function returns non-zero if the initial substring of<i>string</i>matches the regular expression in<i>expbuf</i>.If there is a match an external character pointer,<i>loc2</i>,is set as a side effect.The variable<i>loc2</i>points to the next character in<i>string</i>after the last character that matched.<p>When<i><a href="advance.html">advance()</a></i>encounters a "*" or \{&nbsp;\} sequence in the regular expression, itwill advance its pointer to the string to be matched as far aspossible and will recursively call itself trying to match the rest ofthe string to the rest of the regular expression.As long as there is no match,<i><a href="advance.html">advance()</a></i>will back up along the string until it finds a match or reaches thepoint in the string that initially matched the * or \{&nbsp;\}.It is sometimes desirable to stop this backing up before the initialpoint in the string is reached.If the external character pointer<i>locs</i>is equal to the point in the string at some time during the backing upprocess,<i><a href="advance.html">advance()</a></i>will break out of the loop that backs up and will return 0.<p>The external variables<i>circf</i>,<i>sed</i>and<i>nbra</i>are reserved.<h5><a name = "tag_000_008_059_001">&nbsp;</a>Simple Regular Expressions (Historical Version)</h5><xref type="5" name="simpreg"></xref>A Simple Regular Expression (SRE) specifies a set of character strings.A member of this set of strings is said to be <i>matched</i> by the SRE.<p>A <i>pattern</i> is constructed from one or more SREs.An SRE consists of <i>ordinary characters</i> or <i>metacharacters</i>.<p>Within a pattern, all alphanumeric charactersthat are not part of a bracket expression, back-reference or duplicationmatch themselves; that is, the SRE pattern <i>abc</i> ,when applied to a set of strings, will match only those strings containingthe character sequence <i>abc</i>anywhere in them.<p>Most other characters also match themselves. However, a smallset of characters, known as the <i>metacharacters</i>, havespecial meanings when encountered in patterns.They are described below.<h5><a name = "tag_000_008_059_002">&nbsp;</a>Simple Regular Expression Construction</h5>SREs are constructed as follows:<dl compact><dt><b>Expression</b><dd><b>Meaning</b><dt><i>c</i><dd>The character<i>c</i>,where<i>c</i>is not a special character.<dt>\<i>c</i><dd>The character<i>c</i>,where<i>c</i>is any character with special meaning, see below.<dt>^<dd>The beginning of the string being compared.<dt>$<dd>The end of the string being compared.<dt>.<dd>Any character.<dt>[<i>s</i>]<dd>Any character in the non-empty set<i>s</i>,where<i>s</i>is a sequence of characters.  Ranges may be specified as<i>c-c</i>.The character ] may be included in the set by placing it first in the set.The character "-"may be included in the set by placing it first or last in the set.The character "^"may be included in the set by placing it anywhere other than firstin the set, see below.Ranges in Simple Regular Expressions are only valid if the<i>LC_COLLATE</i>category is set to the C locale.Otherwise, the effect of using the range notation is unspecified.<dt>[^<i>s</i>]<dd>Any character not in the set<i>s</i>,where<i>s</i>is defined as above.<dt><i>r</i>*<dd>Zero or more successive occurrences of the regular expression<i>r</i>.The longest leftmost match is chosen.<dt><i>rx</i><dd>The occurrence of regular expression<i>r</i>followed by the occurrence of regular expression<i>x</i>.

⌨️ 快捷键说明

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