Regular Expression Specification

zhaozj2021-02-16  154

THE SINGLE UNIX? SPECification, VERSION 2COPYRIGHT? 1997 THE Open Group

? Regular Expressions

Note:

TWO Versions of Regular Expressions Are Supported in this Specification Set:

the historical Simple Regular Expressions, which provide backward compatibility, but which may be withdrawn from a future issue of this specification set the improved internationalised version that complies with the ISO / IEC 9945-2: 1993 standard.

............................ ..

Regular Expressions (RES) Provide a Mechanism To Select Specific Strings from a set of character strings.

Regular expressions are a context-independent syntax that can represent a wide variety of character sets and character set orderings, where these character sets are interpreted according to the current locale. While many regular expressions can be interpreted differently depending on the current locale, many features , Such As Character Class Expressions, Provide for Contextual Invariance Across Locales.

The Basic Regular Expression (BRE) NOTATION AND CONSTRUCTION RULES IN

Basic Regular Expressions

Apply to Most Utilities Supporting Regular Expressions. Some Utilities, Instead, Support The Extended Regular Expressions (ERE) Described in

Extended Regular Expressions

; Any exceptions for both cases are noted in the descriptions of the specific utilities using regular expressions Both BREs and EREs are supported by the Regular Expression Matching interface in the XSH specification under regcomp (), regexec () and related functions..

? Regular Expression definitions

For the purposes of this section, The Following Definitions Apply:

? Entire regular expressionThe concatenated set of one or more BREs or EREs that make up the pattern specified for string selection.?matchedA sequence of zero or more characters is said to be matched by a BRE or ERE when the characters in the sequence correspond to a Sequence of characters defined by the pattern.

Matching is based on the bit pattern used for encoding the character, not on the graphic representation of the character. This means that if a character set contains two or more encodings for a graphic symbol, or if the strings searched contain text encoded in more than one codeset, no attempt is made to search for any other representation of the encoded symbol. If that is required, the user can specify equivalence classes containing all variations of the desired graphic symbol.

The search for a matching sequence starts at the beginning of a string and stops when the first sequence matching the expression is found, where first is defined to mean "begins earliest in the string". If the pattern permits a variable number of matching characters and thus there is more than one such sequence starting at that point, the longest such sequence will be matched For example: the BRE bb * matches the second to fourth characters of abbbc, and the ERE (wee | week) (knights | night). Matches All Ten Characters of Weeknights.

Consistent with the whole match being the longest of the leftmost matches, each subpattern, from left to right, matches the longest possible string. For this purpose, a null string is considered to be longer than no match at all. For example, matching the BRE /(.*/).* Against Abcdef, THE SUBEXPRESSION (/ 1) Is Abcdef, And Matching The BRE / (A * /) * Against BC, The SubExpression (/ 1) Is The Null String.

It is possible to determine what strings correspond to subexpressions by recursively applying the leftmost longest rule to each subexpression, but only with the proviso that the overall match is leftmost longest. For example, matching / (ac * /) c * d [ac] * / 1 against acdacaaa matches acdacaaa (with / 1 = a);. simply matching the longest match for / (ac * /) would yield / 1 = ac, but the overall match would be smaller (acdac) Conceptually, the implementation must examine every possible match and among those that yield the leftmost longest total matches, pick the one that does the longest match for the leftmost subexpression and so on Note that this means that matching by subexpressions is context-dependent:. a subexpression within a larger RE may match a different string from the one it would match as an independent RE, and two instances of the same subexpression within the same larger RE may match different lengths even in similar sequences of characters. For example, in the ERE (a. * b ) (a. * b), The Two Identical SubExpressions Would Match Four and Six Characters, Respective, of Accbccb.when a Multi-Character Collating Element in A Bracket Expression (See

Re Bracket Expression

IS Involved, The Longest Sequence Will Be MeaSured In Characters Consumed from The String To Be Matched; That IS, The Collating Element Count Count NOT As One Element, But As The Numr of Characters it matches.

. BRE (ERE) Matching a Single Charactera Bre Or Ere That Matches Either A Single Character OR A Single Collating Element.

Only A BRE ORE OF THIS TYPE THAT INCLUDES A BRACKET Expression (see

Re Bracket Expression

CAN MATCH A collating element.

The definition of single character has been expanded to include also collating elements consisting of two or more characters; this expansion is applicable only when a bracket expression is included in the BRE or ERE An example of such a collating element may be the Dutch ij,. which collates as a y. In some encodings, a ligature "i with j" exists as a character and would represent a single-character collating element. In another encoding, no such ligature exists, and the two-character sequence ij is defined as a multi-character collating element. Outside brackets, the ij is treated as a two-character RE and matches the same characters in a string. Historically, a bracket expression only matched a single character. If, however, the bracket expression defines, for example, a range that includes ij, then this particular bracket expression will also match a sequence of the two characters i and j in the string.?BRE (ERE) matching multiple charactersA BRE or ERE that matches a concatenation o f Single Characters Or Collating Elements.

SUCH A BRE ORE IS MADE UP A BRE (ERE) Matching a Single Character and BRE (ERE) Special Characters.

? INVALIDTHIS Section Uses The Term

INVALID for CERTAIN CONSTRUCTS OR CONDitions. Invalid Res will cause the utility or function sale the re to generate an error condition .when

invalid is not used, violations of the specified syntax or semantics for REs produce undefined results: this may entail an error, enabling an extended syntax for that RE, or using the construct in error as literal characters to be matched For example, the BRE. CONSTRUCT / {1,2,3/} does not comply with the grammar. a portable application cannot rely on it products,

The Requirements In this section section........

. The use of regular expressions is generally associated with text processing REs (BREs and EREs) operate on text strings; that is, zero or more characters followed by an end-of-string delimiter (typically NUL) Some utilities employing regular expressions limit. the processing to lines;. that is, zero or more characters followed by a newline character in the regular expression processing described in this specification, the newline character is regarded as an ordinary character and both a period and a non-matching list can match one . The XCU specification specifies within the individual descriptions of those standard utilities employing regular expressions whether they permit matching of newline characters; if not stated otherwise, the use of literal newline characters or any escape sequence equivalent produces undefined results Those utilities (like grep). That Do Not ALOW Newline Characters to Match Are Responsible for Eliminating Any Newline Character from strings Before matching ag Ainst the re. The regcomp () Function In The Xsh Specification, However, CAN Provide Support for Such Processing WITHOUT VIOLATING The Rules of this section.

The interfaces specified in this specification set do not permit the inclusion of a NUL character in an RE or in the string to be matched. If during the operation of a standard utility a NUL is included in the text designated to be matched, that NUL may designate the end of the text string for the purposes of matching.When a standard utility or function that uses regular expressions specifies that pattern matching will be performed without regard to the case (upper- or lower-) of either data or patterns, then when each character in the string is matched against the pattern, not only the character, but also its case counterpart (if any), will be matched. This definition of case-insensitive processing is intended to allow matching of multi-character collating elements as well As Characters. for Instance, As Each Character in the string is match @................................

The importation will support.

? Basic Regular Expressions

? BREs Matching a Single Character or Collating ElementA BRE ordinary character, a special character preceded by a backslash or a period matches a single character. A bracket expression matches a single character or a single collating element.

Bre Ordinary Charactersan Ordinary Character Is A Bre That Matches Itself: Any Character In The Supported Character Set, Except for the Bre Special Characters listed in

BRE Special Characters

.

The interpretation of an order of an orderded by a backslash (/) is undefined, Except for:

THE Characters, (, {and} the Digits 1 To 9 Inclusive (see

Bres matching multiple characters

) a character inside a bracket expression.?bre special character characters

A

BRE special character has special properties in certain contexts. Outside those contexts, or when preceded by a backslash, such a character will be a BRE that matches the special character itself. The BRE special characters and the contexts in which they have their special meaning are :

.?? [? /

The Period, Left-Bracket and Backslash IS Special Except When Used in A Bracket Expression (SeeE

Re Bracket Expression

..

*

The asseterisk is special Except When Used:

IN A Bracket Expression As The First Character of An Entire Bre (After An Initial ^, IF ANY) As The First Character of A SubExpression (after an initial ^, if any); See

Bres matching multiple characters

.

^

The Circumflex Is Special WHEN USED:

as an anchor (see

BRE EXPRESSION Anchoring

) AS THE FIRST Character of A Bracket Expression (See

Re Bracket Expression

).

$

The Dollar Sign Is Special When Used as an Anchor.

Periods in Bresa Period (.), WHEN Used Outside A Bracket Expression, IS A BRE That Matches Any Character In The Supported Character Set Except Nul.

? Re Bracket Expression

A Bracket Expression (An Expression Enclosed In Square Brackets, []) IS An Re That Matches A Single Collating Element Contained in Non-Empty Set of Collating Elements Represented by The Bracket Expression.

The Following Rules and Definitions Apply to Bracket Expressions:

. A bracket expression is either a matching list expression or a non-matching list expression It consists of one or more expressions: collating elements, collating symbols, equivalence classes, character classes or range expressions Portable applications must not use range expressions, even though. all implementations support them. The right-bracket (]) loses its special meaning and represents itself in a bracket expression if it occurs first in the list (after an initial circumflex (^), if any). Otherwise, it terminates the bracket expression , unless it appears in a collating symbol (such as] [.].) or is the ending right-bracket for a collating symbol, equivalence class or character class The special characters:.. * [/ (period, asterisk, left- bracket and backslash, respectively) lose their special meaning within a bracket expression The character sequences:. [[= [:. (left-bracket followed by a period, equals-sign or colon) are special inside a bracket expression and are used toDelimit Collating Symbols, Equivalence Class Expressions and Character Class Expressions. There Symbols Must Be Followed by a Valid Expression and The matching termating sequence.], =

] Or:], as described in the following items A matching list expression specifies a list that matches any one of the expressions represented in the list The first character in the list must not be the circumflex For example, [abc] is... an RE that matches any of the characters a, b or c. A non-matching list expression begins with a circumflex (^), and specifies a list that matches any character or collating element except for the expressions represented in the list after the leading circumflex. For example, [^ abc] is an RE that matches any character or collating element except the characters a, b or c. The circumflex will have this special meaning only when it occurs first in the list, immediately following the left-bracket . A collating symbol is a collating element enclosed with bracket-period ([.]) Delimiters. Collating Elements Are Defined As Described Incollation ORDER

. Multi-character collating elements must be represented as collating symbols when it is necessary to distinguish them from a list of the individual characters that make up the multi-character collating element. For example, if the string ch is a collating element in the current Collation sequence with the associated collating symbol

, The expression [[.ch.]] Will be treated as an RE matching the character sequence ch, while [ch] will be treated as an RE matching c or h. Collating symbols will be recognised only inside bracket expressions. This implies that the RE [[.ch.]] * c matches the first to fifth character in the string chchch. If the string is not a collating element in the current collating sequence definition, or if the collating element has no characters associated with it (for Example, See the symbol

In The Example Collation Definition Shown Incollation Order

), The Symbol Will BE Treated as an invalid expression.

An equivalence class expression represents the set of collating elements belonging to an equivalence class, as described in Collation Order. Only primary equivalence classes will be recognised. The class is expressed by enclosing any one of the collating elements in the equivalence class within bracket-equal ([= =]) Delimiters., IF a, à and a belong to the same equivalence class, then [[= a =] b], [= à =] b] and [[= a =

] B] will each be equivalent to [aàab]. If the collating element does not belong to an equivalence class, the equivalence class expression will be treated as a collating symbol. A character class expression represents the set of characters belonging to a character class ., as defined in the LC_CTYPE category in the current locale All character classes specified in the current locale will be recognised A character class expression is expressed as a character class name enclosed within bracket-colon ([::]).. delimiters The following Character Class Expressions Are Supported in All locals: [:] [: ap: ● [: print:] [: upper: : Blank:] [: graph:] [: Punct:] [: xdigit:] in addition, character class expnessions of the form: [: name:] Are Recoised in Those Locales Where The name keyword has been given a charclass definition in THE LC_CTYPE CATEGORY. A Range Expression Reperests The Set of Collating Elements That Fall Between Two Elements in The current collation sequence, inclusively It is expressed as the starting point and the ending point separated by a hyphen. (-). Range expressions must not be used in portable applications because their behaviour is dependent on the collating sequence Ranges will be treated according to. the current collating sequence, and include such characters that fall within the range based on that collating sequence, regardless of character values. This, however, means that the interpretation will differ depending on collating sequence. If, for instance, one collating sequence defines? AS A VARIANT OF A, WHILE ANOTHER Defines IT AS A Letter Following Z, THE Expression [?

-z] is valid in the first language and invalid in the second. In the following, all examples assume the collation sequence specified for the POSIX locale, unless another collation sequence is specifically defined. The starting range point and the ending range point must be a collating element or collating symbol. An equivalence class expression used as a starting or ending point of a range expression produces unspecified results. An equivalence class can be used portably within a bracket expression, but only outside the range. For example, the unspecified expression [[= E =] - F] SHOULD BE GIVEN AS [[= E =] EF]. The ending range point must collate qual to or higher Than The Starting Range Point; OtherWise, The Expression Will Be Treated As Invalid. The Order Used is the order in which the collating elements area specified in the current collation definition. One-to-man mappings (see the description of lc_collate inlocale

) Will not be performed. For example, assuming that the character eszet (?) Is placed in the collation sequence after r and s, but before t and that it maps to the sequence ss for collation purposes, then the expression [rs] matches only r and s, but the expression [st] matches s,? or t. The interpretation of range expressions where the ending range point is also the starting range point of a subsequent range expression (for instance [amo]) is undefined. The HYPHEN CHARACTER WILL BE TREATED AS ITSELF if IT Occurs First (after an initial ^, if any) or place in the list, or as an ending. AS example, The Expressions [-ac] and [AC- ] Are Equivalent and match any of the characters a, c or -; [^ -ac] and [^ ac-] is equivalent and match any character ^ ;; the expression [% -] matches any of the Characters BetWeen% and - Inclusive; The Expression [- @] matches any of the character; and @ inclusive; and the expness . Id, because the letter a follows the symbol - in the POSIX locale To use a hyphen as the starting range point, it must either come first in the bracket expression or be specified as a collating symbol, for example: [] [.- .] - 0], which matches either a right bracket or any character or collating element that collates between hyphen and 0, inclusive If a bracket expression must specify both -. and], the] must be placed first (after the ^, if Any) and the - Last within The Bracket Expression.?bres match multiple characters

The Following Rules Can Be Used to Construct Bres Matching Multiple Characters from Bres Matching a Single Character:

The concatenation of BREs matches the concatenation of the strings matched by each component of the BRE. A subexpression can be defined within a BRE by enclosing it between the character pairs / (and /). Such a subexpression matches whatever it would have matched without the / (and /), Except That Anchoring Within SubExpressions Is Optional Behaviour; Seebre Expression Anchoring

. Subexpressions can be arbitrarily nested. The back-reference expression / n matches the same (possibly empty) string of characters as was matched by a subexpression enclosed between / (and /) preceding the / n. The character n must be a digit from 1 to 9 inclusive, nth subexpression (the one that begins with the nth / (and ends with the corresponding paired /)). The expression is invalid if less than n subexpressions precede the / n. For example, the expression ^ / (. * /) / 1 $ matches a line consisting of two adjacent appearances of the Same string, and the expression / (a ​​/) * / 1 fails to match a. The limit of nine back-reference to subscribeds in the Re is based ON THE BASEDOMS IN THE BASED ON The Use of a Single Digit Identifier. This Does Not Imply That Only Nine SubExpressions Are Allowed in Res. The Following IS A Valid Bre with Ten SubExpressions: / (/ (ab /) * c /) * d /) / EF /) * / (GH /) / {2 /} / (ij /) * / (kl /) * / (mn /) * / (op /) * / (qr /) * WHEN A BRE MATCHING A SINGLE Character, A SubExpression or A Back-Reference Is Followed by the special character asterisk (*), together with that asterisk it matches what zero or more consecutive occurrences of the BRE would match. For example, [ab] * and [ab] [ab] are equivalent when matching the string ab. When a BRE MATCHING A SINGLE CHARACTER, A SUBEXPIRESSION OR A Back-Reference Is Followed by An Interval Expression of The Format / {m /}, / {m, / {m, {m, n /}, TOGETHER WITH That Interval Expression IT Matches What Repeated Consecutive Occurrence of The Bre Would Match. The Values ​​of M and N Will Be Decimal Integers in the Range 0 <= m <= n <=

{RE_DUP_MAX}, where m specifies the exact or minimum number of occurrences and n specifies the maximum number of occurrences. The expression / {m /} matches exactly m occurrences of the preceding BRE, / {m, /} matches at least m occurrences And / {m, n /} matches any number of occurrences Between M and N, Inclusive. For example, in the string abababcccccccd the BRE C / {3 /} Is matched by Characters Seven to Nine, The Bre / (AB /) / {4, /} IS NOT MATED AT All and the BRE C / {1,3 /} D is match by Characters Ten to Thirteen.The Behaviour of Multiple AdjaCent Duplication Symbols (* and "Products undefined results.

? BRE Precedencethe Order of Precedence Is as Shown in The Following Table:

BRE Precedence (from high to low) collation-corrected bracket symbols [= =] [::] [.] Escaped characters / bracket expression [] subspressions / back-references / (/) / n Single-Character -Bre duplication * / {m, n /} ConcateNation? Anchoring ^ $

? BRE EXPRESSION Anchoring

A BRE CAN BE LIMITED TO MATCHING STRINGS That Begin or End a line; this is caled

Anchoring. The Circumflex and Dollar Sign Special Characters Will Be Considered Bre Anchors in The Following Contexts:

A circumflex (^) is an anchor when used as the first character of an entire BRE. The implementation may treat circumflex as an anchor when used as the first character of a subexpression. The circumflex will anchor the expression (or optionally subexpression) to the beginning of a string;. only sequences starting at the first character of a string will be matched by the BRE For example, the BRE ^ ab matches ab in the string abcdef, but fails to match in the string cdefab The BRE / (^. ab /) may match the former string. A portable BRE must escape a leading circumflex in a subexpression to match a literal circumflex. A dollar sign ($) is an anchor when used as the last character of an entire BRE. The implementation may treat a dollar sign as an anchor when used as the last character of a subexpression The dollar sign will anchor the expression (or optionally subexpression) to the end of the string being matched;. the dollar sign can be said to match the end-of- String Following The Last Chara Cter. A BRE Anchored by Both "^" and "$" matches only an entire string. for example, the bre ^ Abcdef $ matches strings consisting online @ ^

THE

extended regular expression (ERE) notation and construction rules will apply to utilities defined as using extended regular expressions; any exceptions to the following rules are noted in the descriptions of the specific utilities using EREs.

? EREs Matching a Single Character or Collating ElementAn ERE ordinary character, a special character preceded by a backslash or a period matches a single character. A bracket expression matches a single character or a single collating element. An

ERE Matching a Single Character Enclosed in Parents Matches The Same As The Ere With Parentheses Would Have Matched.?ere Ordinary Charactersan

Ordinary Character Is An Ere That Matches Itself. An Ordinary Character Is Any Character In The Supported Character Set, Except for the Ere Special Characters listed in

ERE Special Characters

The interpretation of an order of an orderded by a backslash (/) IS undefined.

? ERE Special Characters

AN

ERE special character has special properties in certain contexts. Outside those contexts, or when preceded by a backslash, such a character is an ERE that matches the special character itself. The extended regular expression special characters and the contexts in which they have their special meaning ARE:

[/

The Period, Left-Bracket, Backslash and Left-Pouthesis Are Special Except When Used In A Bracket Expression (See

Re Bracket Expression

). Outside a bracket expression, a left-parenthes ident a right - parenthes ident "undefined results.

)

The Right-Parenthesis IS Special When Matched with a Preceding Left-Parenthesis, Both Outside A Bracket Expression.

* ? {

The Asterisk, Plus-Sign, Question-Mark and Left-Brace Are Special Except When Used In A Bracket Expression (SeeE

Re Bracket Expression

Any of the folloading buying uses production undefined results:

If The Characters Appear First in An Ere, Or Immediately Following a Vertical-Line, Circumflex OR Left-Parenthesis if A LEFT-BRACE IS NOT Part of a Valid Interval Expression.

|

The Vertical-Line IS Special Except When Used In A Bracket Expression (SeeE

Re Bracket Expression

). A Vertical-Line Appearing First Or Last in An Ere, or Immediately Following a Vertical-Line OR a Left-Pouthesis, or Immediately Preceding A Right-Parenthesis, Produces Undefined Results. ^

The Circumflex Is Special WHEN USED:

as an anchor (see

ERE Expression Anchoring

) AS THE FIRST Character of A Bracket Expression (See

Re Bracket Expression

).

$

The Dollar Sign Is Special When Used as an Anchor.

PERIODS IN ERESA PERIOD (.), WHEN Used Outside A Bracket Expression, IS An Ere That Matches Any Character In The Supported Character Set Except Nul.

? ERE Bracket ExpressionThe Rules for Ere Bracket Expressions Are The Same As for Basic Regular Expressions; See

Re Bracket Expression

.

ERES Matching Multiple Characters

The Following Rules Will Be Used to Construct Eres Matching Multiple Characters from ERES Matching A Single Character:

A concatenation of EREs matches the concatenation of the character sequences matched by each component of the ERE. A concatenation of EREs enclosed in parentheses matches whatever the concatenation without the parentheses matches. For example, both the ERE cd and the ERE (cd) are matched by the third and fourth character of the string abcdefabcdef. When an ERE matching a single character or an ERE enclosed in parentheses is followed by the special character plus-sign ( ), together with that plus-sign it matches what one or more consecutive Occurrences of the Ere Would Match. for Example, The Ere B (BC) Matches The Fourth To Seventh Characters in The String Acabbbcde. and, [AB]

and [ab] [ab] * are equivalent. When an ERE matching a single character or an ERE enclosed in parentheses is followed by the special character asterisk (*), together with that asterisk it matches what zero or more consecutive occurrences of the ERE would match. For example, the ERE b * c matches the first character in the string cabbbcde, and the ERE b * cd matches the third to seventh characters in the string cabbbcdebbbbbbcdbc. and, [ab] * and [ab] [ab] are equivalent when matching the string ab. When an ERE matching a single character or an ERE enclosed in parentheses is followed by the special character question-mark (?), together with that question-mark it matches what zero or one consecutive occurrences of the ERE would match. For example, the ERE b? c matches the second character in the string acabbbcde. When an ERE matching a single character or an ERE enclosed in parentheses is followed by an interval expression of the format {m}, {m, } or {m, n}, together with That Interval Expressio n it matches what repeated consecutive occurrences of the ERE would match. The values ​​of m and n will be decimal integers in the range 0 <= m <= n <= {RE_DUP_MAX}, where m specifies the exact or minimum number of occurrences and n specifies the maximum number of occurrences. The expression {m} matches exactly m occurrences of the preceding ERE, {m,} matches at least m occurrences and {m, n} matches any number of occurrences between m and n, inclusive. For example, in the string abababccccccd the ERE c {3} is matched by characters seven to nine and the ERE (ab) {2,} is matched by characters one to six.The behaviour of multiple adjacent duplication symbols ( , *,? And Intervals).

ERE AlternationTwo Eres Separated by The Special Character Vertical-Line (|) Match A String That Is Matched by Either. For Example, The Ere A ((BC) | D) Matches The string abc and the string ad. Single Characters, or expressions matching single characters, separated by the vertical bar and enclosed in parentheses, will be treated as an ERE matching a single character.?ERE PrecedenceThe order of precedence will be as shown in the following table:

ERE Precedence (from high to low) collation-limited bracket symbols [==] [::] [.] Escaped character> Bracket Expression [] grouping () Single-character-ere duplication * ? {M , n} concatenation? Anchoring ^ $ alternation |

For Example, The EITHER ABBA | CDE Matches Either The String Abba or The String Abbade (Rather Than The String Abbade Or Abbcde, Because Concatenation Has A Higher ORDER OF Precedency Than Alternation).

? ERE Expression Anchoring

An ERE CAN BE LIMITED TO MATCHING STRINGS That Begin or End a line; this is caled

Anchoring. The Circumflex and Dollar Sign Special Characters Are Considered ERE Anchors When Used Anywhere Outside A Bracket Expression. This Has The Following Effects:

A circumflex (^) outside a bracket expression anchors the expression or subexpression it begins to the beginning 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 ^ ab and (^ ab) match ab in the string abcdef, but fail to match in the string cdefab, and the ERE a ^ b is valid, but can never match because the a prevents the expression ^ b from matching starting at the first character. A dollar sign ($) outside a bracket expression anchors the expression or subexpression it ends to the end of a string;. such an expression or subexpression can match only a sequence ending at the last character of a string For example, the EREs ef $ and (ef $) match ef in the string abcdef, but fail to match in the string cdefab, and the ERE e $ f is valid, but can never match because the f prevents the expression e $ from matching ending at the last character.? Regular Expression Grammar

Grammars describing the syntax of book.

XCU Specification,

Section 1.8,

Grammar convention.

BRE / ERE Grammar Lexical ConventionSthe Lexical Conventions for Regular Expressions Are As Described in this section.

Except As Noted, The Longest Possible Token Or Delimiter Beginning At A Given Point Will Be Recognosed.

The Following tokens Will Be Processed (in Addition To Those String Constants Shown In The Grammar):

COLL_ELEM

Any Single-Character Collating Element, Unless it is a meta_char.

Backref

Applicable Only to Basic Regular Expressions. The Character String Consisting of "/" FOLLOWED BY A SINGLE-DIGIT NUMERAL, 1 TO 9.

DUP_COUNT

Represents a numeric constant. It is an integer in the range 0 <= DUP_COUNT <= {RE_DUP_MAX}. This token will only be recognised when the context of the grammar requires it. At all other times, digits not preceded by "/" will BE Treated as ORD_CHAR.META_CHAR

One of the characters:

^

When Found First in A Bracket Expression

-

When Found Anywhere But First (after an initial "^", if any) or place in a bracket expression, or as the ending range point Point in a

]

.

L_anchor

. Applicable only to basic regular expressions The character "^" when it appears as the first character of a basic regular expression and when not QUOTED_CHAR The "^" may be recognised as an anchor elsewhere;. See

BRE EXPRESSION Anchoring

.

ORD_CHAR

A Character, Other Than One of The Special Characters in Spec_Char.

Quoted_char

In A BRE, One of the Character Sequences:

/ ^ /. / * / [/ $ //

In an Ere, One of the Character Sequences:

/ ^ /. / [/ $ / (/) / | / * / /? / {//

R_anchor

. (. Applicable only to basic regular expressions) The character "$" when it appears as the last character of a basic regular expression and when not QUOTED_CHAR The "$" may be recognised as an anchor elsewhere; see

BRE EXPRESSION Anchoring

.

SPEC_CHAR

For Basic Regular Expressions, Will BE ONE FOLLOWING Special Characters:

.

Anywhere Outside Bracket Expressions

/

Anywhere Outside Bracket Expressions

[

Anywhere Outside Bracket Expressions

^

When use as an an anchor (see

BRE EXPRESSION Anchoring

) or when first in a Bracket Expression

$

When use as an anchor

*

anywhere except: first in an entire RE; anywhere in a bracket expression; directly following / (; directly following an anchoring "^" For extended regular expressions, will be one of the following special characters found anywhere outside bracket expressions.:

^. [$ () | * ? {/

...................

RE AND BRACKET Expression Grammarthis Section Presents The Grammar for Basic Regular Expressions, Including The Bracket Expression Grammar That Is Common To Both Bres and Ers.

% Token ORD_CHAR QUOTED_CHAR DUP_COUNT% token BACKREF L_ANCHOR R_ANCHOR% token Back_open_paren Back_close_paren / * '/ (' '/)' * /% token Back_open_brace Back_close_brace / * '/ {' '/}' * / / * The following tokens are for the Bracket Expression grammar common to both REs and EREs * /% token COLL_ELEM META_CHAR% token Open_equal Equal_close Open_dot Dot_close Open_colon Colon_close / * '[=' '=]' '[.' '' [: '.'.] ':]' * /% token class_name / * class_name is a keyword to the LC_CTYPE locale category * / / * (representing a character class) in the current locale * / / * and is only recognised between [: and:] * /% start basic_reg_exp% % / * ------------------------------------------- Basic Regular Expression ------------------------------------------- * / Basic_reg_exp: re_expression | L_ANCHOR | R_ANCHOR | L_ANCHOR R_ANCHOR | L_ANCHOR RE_expression | RE_expression R_ANCHOR | L_ANCHOR RE_expression R_ANCHOR; RE_expression: simple_RE | RE_expression simple_RE; simple_RE: nondupl_RE | nondupl_RE RE_dup l_symbol; nondupl_RE: one_character_RE | Back_open_paren RE_expression Back_close_paren | Back_open_paren Back_close_paren | BACKREF; one_character_RE: ORD_CHAR | QUOTED_CHAR | bracket_expression; RE_dupl_symbol | '.': '*' | Back_open_brace DUP_COUNT Back_close_brace | Back_open_brace DUP_COUNT ',' Back_close_brace | Back_open_brace DUP_COUNT ',' DUP_COUNT Back_close_brace; / * ------------------------------------------ Bracket Expression ------------------------------------------- * / bracket_expression: '[' Matching_list ']' | '[' Nonmatching_List ']';

matching_list: bracket_list; nonmatching_list: '^' bracket_list; bracket_list: follow_list | follow_list '-'; follow_list: expression_term | follow_list expression_term; expression_term: single_expression | range_expression; single_expression: end_range | character_class | equivalence_class; range_expression: start_range end_range | start_range '-' ; start_range: end_range '-'; end_range: COLL_ELEM | collating_symbol; collating_symbol: Open_dot COLL_ELEM Dot_close | Open_dot META_CHAR Dot_close; equivalence_class: Open_equal COLL_ELEM Equal_close; character_class: Open_colon class_name Colon_close; The BRE grammar does not permit L_ANCHOR or R_ANCHOR inside / (and / (Which Implies That ^ and $ is Ordinary Characters). This Reflects The Semantic Limits on The Application, AS NOTED IN

BRE EXPRESSION Anchoring

. Implementations are permitted to extend the language to interpret "^" and "$" as anchors in these locations, and as such, portable applications can not use unescaped "^" and "$" in positions inside / (and /) that might be Interpreted as anchors.

ERE Grammarthis Section Presents The Grammar for Extended Regular Expressions, Excluding The Bracket Expression Grammar.

Note:

The Bracket Expression Grammar and The Associated

% Token Lines Are Identical Between Bres and Eres. It has been omitted from the ere section to avoid unnecessary Editorial Duplication.

% token ord_char quoted_char dup_count% Start Extended_Reg_exp %% / * -------------------------------------- --------------------------------------------------------------------------------------------------- --- * / extended_reg_exp: ERE_branch | extended_reg_exp '|' ERE_branch; ERE_branch: ERE_expression | ERE_branch ERE_expression; ERE_expression: one_character_ERE | '^' | '$' | '(' extended_reg_exp ')' | ERE_expression ERE_dupl_symbol; one_character_ERE: ORD_CHAR | QUOTED_CHAR | '.' | Bracket_Expression; ERE_DUPL_SYMBOL: '*' | ' ' | '?' | '{' DUP_COUNT '}' | '{' DUP_COUNT ',' '}' | '{' DUP_COUNT ',' DUP_COUNT '} '; The Ere Grammar Does Not Permit Several Construction That Previous Sections Specify As Having Undefined Results:

ORD_CHAR preceded by "/" one or more ERE_dupl_symbols appearing first in an ERE, or immediately following "|", "^" or "(" "{" not part of a valid ERE_dupl_symbol "|" appearing first or last in an ERE, Or Immediately Following "|" OR "(", or Immediately Preceding ")".

Implementations are Permitted to Extend The Language To Allow these. Portable Applications Cannot Use Such Construction.

UNIX? Is a registered trademark of the open group.copyright? 1997 The open group

转载请注明原文地址:https://www.9cbs.com/read-7764.html

New Post(0)