📄 xbd_chap09.html
字号:
<dt><tt>|</tt></dt><dd>The vertical-line is special except when used in a bracket expression (see <a href="#tag_09_03_05">RE Bracket Expression</a> ).A vertical-line appearing first or last in an ERE, or immediately following a vertical-line or a left-parenthesis, or immediatelypreceding a right-parenthesis, produces undefined results.</dd><dt><tt>^</tt></dt><dd>The circumflex shall be special when used as: <ul><li><p>An anchor (see <a href="#tag_09_04_09">ERE Expression Anchoring</a> )</p></li><li><p>The first character of a bracket expression (see <a href="#tag_09_03_05">RE Bracket Expression</a> )</p></li></ul></dd><dt><tt>$</tt></dt><dd>The dollar sign shall be special when used as an anchor.</dd></dl><h4><a name="tag_09_04_04"></a>Periods in EREs</h4><p>A period ( <tt>'.'</tt> ), when used outside a bracket expression, is an ERE that shall match any character in the supportedcharacter set except NUL.</p><h4><a name="tag_09_04_05"></a>ERE Bracket Expression</h4><p>The rules for ERE Bracket Expressions are the same as for Basic Regular Expressions; see <a href="#tag_09_03_05">RE BracketExpression</a> .</p><h4><a name="tag_09_04_06"></a>EREs Matching Multiple Characters</h4><p>The following rules shall be used to construct EREs matching multiple characters from EREs matching a single character:</p><ol><li><p>A concatenation of EREs shall match the concatenation of the character sequences matched by each component of the ERE. Aconcatenation of EREs enclosed in parentheses shall match whatever the concatenation without the parentheses matches. For example,both the ERE <tt>"cd"</tt> and the ERE <tt>"(cd)"</tt> are matched by the third and fourth character of the string<tt>"abcdefabcdef"</tt> .</p></li><li><p>When an ERE matching a single character or an ERE enclosed in parentheses is followed by the special character plus-sign (<tt>'+'</tt> ), together with that plus-sign it shall match what one or more consecutive occurrences of the ERE would match. Forexample, the ERE <tt>"b+(bc)"</tt> matches the fourth to seventh characters in the string <tt>"acabbbcde"</tt> . And,<tt>"[ab]+"</tt> and <tt>"[ab][ab]*"</tt> are equivalent.</p></li><li><p>When an ERE matching a single character or an ERE enclosed in parentheses is followed by the special character asterisk (<tt>'*'</tt> ), together with that asterisk it shall match what zero or more consecutive occurrences of the ERE would match. Forexample, the ERE <tt>"b*c"</tt> matches the first character in the string <tt>"cabbbcde"</tt> , and the ERE <tt>"b*cd"</tt> matchesthe third to seventh characters in the string <tt>"cabbbcdebbbbbbcdbc"</tt> . And, <tt>"[ab]*"</tt> and <tt>"[ab][ab]"</tt> areequivalent when matching the string <tt>"ab"</tt> .</p></li><li><p>When an ERE matching a single character or an ERE enclosed in parentheses is followed by the special character question-mark (<tt>'?'</tt> ), together with that question-mark it shall match what zero or one consecutive occurrences of the ERE would match.For example, the ERE <tt>"b?c"</tt> matches the second character in the string <tt>"acabbbcde"</tt> .</p></li><li><p>When an ERE matching a single character or an ERE enclosed in parentheses is followed by an interval expression of the format<tt>"{m}"</tt> , <tt>"{m,}"</tt> , or <tt>"{m,n}"</tt> , together with that interval expression it shall match what repeatedconsecutive occurrences of the ERE would match. The values of <i>m</i> and <i>n</i> are decimal integers in the range 0 <=<i>m</i><= <i>n</i><= {RE_DUP_MAX}, where <i>m</i> specifies the exact or minimum number of occurrences and <i>n</i>specifies the maximum number of occurrences. The expression <tt>"{m}"</tt> matches exactly <i>m</i> occurrences of the precedingERE, <tt>"{m,}"</tt> matches at least <i>m</i> occurrences, and <tt>"{m,n}"</tt> matches any number of occurrences between <i>m</i>and <i>n</i>, inclusive.</p><p>For example, in the string <tt>"abababccccccd"</tt> the ERE <tt>"c{3}"</tt> is matched by characters seven to nine and the ERE<tt>"(ab){2,}"</tt> is matched by characters one to six.</p></li></ol><p>The behavior of multiple adjacent duplication symbols ( <tt>'+'</tt> , <tt>'*'</tt> , <tt>'?'</tt> , and intervals) producesundefined results.</p><p>An ERE matching a single character repeated by an <tt>'*'</tt> , <tt>'?'</tt> , or an interval expression shall not match a nullexpression unless this is the only match for the repetition or it is necessary to satisfy the exact or minimum number ofoccurrences for the interval expression.</p><h4><a name="tag_09_04_07"></a>ERE Alternation</h4><p>Two EREs separated by the special character vertical-line ( <tt>'|'</tt> ) shall match a string that is matched by either. Forexample, the ERE <tt>"a((bc)|d)"</tt> matches the string <tt>"abc"</tt> and the string <tt>"ad"</tt> . Single characters, orexpressions matching single characters, separated by the vertical bar and enclosed in parentheses, shall be treated as an EREmatching a single character.</p><h4><a name="tag_09_04_08"></a>ERE Precedence</h4><p>The order of precedence shall be as shown in the following table:</p><center><table border="1" cellpadding="3" align="center"><tr valign="top"><th colspan="2" align="center"><p class="tent"><b>ERE Precedence (from high to low)</b></p></th></tr><tr valign="top"><td align="left"><p class="tent">Collation-related bracket symbols</p></td><td align="left"><p class="tent">[==] [::] [..]</p></td></tr><tr valign="top"><td align="left"><p class="tent">Escaped characters</p></td><td align="left"><p class="tent">\<special character></p></td></tr><tr valign="top"><td align="left"><p class="tent">Bracket expression</p></td><td align="left"><p class="tent">[]</p></td></tr><tr valign="top"><td align="left"><p class="tent">Grouping</p></td><td align="left"><p class="tent">()</p></td></tr><tr valign="top"><td align="left"><p class="tent">Single-character-ERE duplication</p></td><td align="left"><p class="tent">* + ? {m,n}</p></td></tr><tr valign="top"><td align="left"><p class="tent">Concatenation</p></td><td align="left"><p class="tent"> </p></td></tr><tr valign="top"><td align="left"><p class="tent">Anchoring</p></td><td align="left"><p class="tent">^ $</p></td></tr><tr valign="top"><td align="left"><p class="tent">Alternation</p></td><td align="left"><p class="tent">|</p></td></tr></table></center><p>For example, the ERE <tt>"abba|cde"</tt> matches either the string <tt>"abba"</tt> or the string <tt>"cde"</tt> (rather than thestring <tt>"abbade"</tt> or <tt>"abbcde"</tt> , because concatenation has a higher order of precedence than alternation).</p><h4><a name="tag_09_04_09"></a>ERE Expression Anchoring</h4><p>An ERE can be limited to matching strings that begin or end a line; this is called "anchoring". The circumflex and dollar signspecial characters shall be considered ERE anchors when used anywhere outside a bracket expression. This shall have the followingeffects:</p><ol><li><p>A circumflex ( <tt>'^'</tt> ) outside a bracket expression shall anchor the expression or subexpression it begins to thebeginning of a string; such an expression or subexpression can match only a sequence starting at the first character of a string.For example, the EREs <tt>"^ab"</tt> and <tt>"(^ab)"</tt> match <tt>"ab"</tt> in the string <tt>"abcdef"</tt> , but fail to matchin the string <tt>"cdefab"</tt> , and the ERE <tt>"a^b"</tt> is valid, but can never match because the <tt>'a'</tt> prevents theexpression <tt>"^b"</tt> from matching starting at the first character.</p></li><li><p>A dollar sign ( <tt>'$'</tt> ) outside a bracket expression shall anchor the expression or subexpression it ends to the end of astring; such an expression or subexpression can match only a sequence ending at the last character of a string. For example, theEREs <tt>"ef$"</tt> and <tt>"(ef$)"</tt> match <tt>"ef"</tt> in the string <tt>"abcdef"</tt> , but fail to match in the string<tt>"cdefab"</tt> , and the ERE <tt>"e$f"</tt> is valid, but can never match because the <tt>'f'</tt> prevents the expression<tt>"e$"</tt> from matching ending at the last character.</p></li></ol><h3><a name="tag_09_05"></a>Regular Expression Grammar</h3><p>Grammars describing the syntax of both basic and extended regular expressions are presented in this section. The grammar takesprecedence over the text. See the Shell and Utilities volume of IEEE Std 1003.1-2001, <a href="../utilities/xcu_chap01.html#tag_01_10">Section 1.10, Grammar Conventions</a>.</p><h4><a name="tag_09_05_01"></a>BRE/ERE Grammar Lexical Conventions</h4><p>The lexical conventions for regular expressions are as described in this section.</p><p>Except as noted, the longest possible token or delimiter beginning at a given point is recognized.</p><p>The following tokens are processed (in addition to those string constants shown in the grammar):</p><dl compact><dt><b>COLL_ELEM_SINGLE</b></dt><dd><br>Any single-character collating element, unless it is a <b>META_CHAR</b>.</dd><dt><b>COLL_ELEM_MULTI</b></dt><dd>Any multi-character collating element.</dd><dt><b>BACKREF</b></dt><dd>Applicable only to basic regular expressions. The character string consisting of <tt>'\'</tt> followed by a single-digitnumeral, <tt>'1'</tt> to <tt>'9'</tt> .</dd><dt><b>DUP_COUNT</b></dt><dd>Represents a numeric constant. It shall be an integer in the range 0 <= <b>DUP_COUNT</b> <= {RE_DUP_MAX}. This token isonly recognized when the context of the grammar requires it. At all other times, digits not preceded by <tt>'\'</tt> are treated as<b>ORD_CHAR</b>.</dd><dt><b>META_CHAR</b></dt><dd>One of the characters: <dl compact><dt><tt>^</tt></dt><dd>When found first in a bracket expression</dd><dt><tt>-</tt></dt><dd>When found anywhere but first (after an initial <tt>'^'</tt> , if any) or last in a bracket expression, or as the ending rangepoint in a range expression</dd><dt><tt>]</tt></dt><dd>When found anywhere but first (after an initial <tt>'^'</tt> , if any) in a bracket expression</dd></dl></dd><dt><b>L_ANCHOR</b></dt><dd>Applicable only to basic regular expressions. The character <tt>'^'</tt> when it appears as the first character of a basicregular expression and when not <b>QUOTED_CHAR</b>. The <tt>'^'</tt> may be recognized as an anchor elsewhere; see <a href="#tag_09_03_08">BRE Expression Anchoring</a> .</dd><dt><b>ORD_CHAR</b></dt><dd>A character, other than one of the special characters in <b>SPEC_CHAR</b>.</dd><dt><b>QUOTED_CHAR</b></dt>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -