Tcl脚本语言教程 - 图文(6)

2019-06-17 12:57

five characters of `chchcc'. Also, the RE [^c]b matches all of `chb' (because [^c] matches the multi-character ch). Within a bracket expression, a collating element enclosed in [= and =] is an equivalence class, standing for the sequences of characters of all collating elements equivalent to that one, including itself. (If there are no other equivalent collating elements, the treatment is as if the enclosing delimiters were `[.' and `.]'.) For example, if o and ?are the members of an equivalence class, then `[[=o=]]', `[[=?]]', and `[o' are all synonymous. An equivalence class may not be an endpoint of a range. (Note: Tcl currently implements only the Unicode locale. It doesn't define any equivalence classes. The examples above are just illustrations.) Within a bracket expression, the name of a character class enclosed in [: and :] stands for the list of all characters (not all collating elements!) belonging to that class. Standard character classes are: 字符 alpha upper lower digit xdigit alnum print blank space punct graph cntrl 意义 A letter. An upper-case letter. A lower-case letter. A decimal digit. A hexadecimal digit. An alphanumeric (letter or digit). An alphanumeric (same as alnum). A space or tab character. A character producing white space in displayed text. A punctuation character. A character with a visible representation. A control character. A locale may provide others. (Note that the current Tcl implementation has only one locale: the Unicode locale.) A character class may not be used as an endpoint of a range. There are two special cases of bracket expressions: the bracket expressions [[:<:]] and [[:>:]] are constraints, matching empty strings at the beginning and end of a word respectively. A word is defined as a sequence of word characters that is neither preceded nor followed by word characters. A word character is an alnum character or an underscore (_). These special bracket expressions are deprecated; users of AREs should use constraint escapes instead (see below). ◆ESCAPES(转意字符) Escapes (AREs only), which begin with a \\ followed by an alphanumeric character, 2 6

come in several varieties: character entry, class shorthands, constraint escapes, and back references. A \\ followed by an alphanumeric character but not constituting a valid escape is illegal in AREs. In EREs, there are no escapes: outside a bracket expression, a \\ followed by an alphanumeric character merely stands for that character as an ordinary character, and inside a bracket expression, \\ is an ordinary character. (The latter is the one actual incompatibility between EREs and AREs.) Character-entry escapes (AREs only) exist to make it easier to specify non-printing and otherwise inconvenient characters in REs: 字符 \\a \\b \\B \\cX \\e \\f \\n \\r \\t \%uwxyz \\Ustuvwxyz \\v \\xhhh 意义 alert (bell) character, as in C backspace, as in C synonym for \\ to help reduce backslash doubling in some applications where there are multiple levels of backslash processing (where X is any character) the character whose low-order 5 bits are the same as those of X, and whose other bits are all zero the character whose collating-sequence name is `ESC', or failing that, the character with octal value 033 formfeed, as in C newline, as in C carriage return, as in C horizontal tab, as in C (where wxyz is exactly four hexadecimal digits) the Unicode character U+wxyz in the local byte ordering (where stuvwxyz is exactly eight hexadecimal digits) reserved for a somewhat-hypothetical Unicode extension to 32 bits vertical tab, as in C are all available. (where hhh is any sequence of hexadecimal digits) the character whose hexadecimal value is 0xhhh (a single character no matter how many hexadecimal digits are used). \\0 \\xy \\xyz the character whose value is 0 (where xy is exactly two octal digits, and is not a back reference (see below)) the character whose octal value is 0xy (where xyz is exactly three octal digits, and is not a back reference (see below)) the character whose octal value is 0xyz Hexadecimal digits are `0'-`9', `a'-`f', and `A'-`F'. Octal digits are `0'-`7'. The character-entry escapes are always taken as ordinary characters. For example, \\135 is ] in ASCII, but \\135 does not terminate a bracket expression. Beware, however, that some applications (e.g., C compilers) interpret such sequences

27

themselves before the regular-expression package gets to see them, which may require doubling (quadrupling, etc.) the `\\'. Class-shorthand escapes (AREs only) provide shorthands for certain commonly-used character classes: 缩写 \\d \\s \\w \\D \\S \\W 代表的完整表达式 [[:digit:]] [[:space:]] [[:alnum:]_] (note underscore) [^[:digit:]] [^[:space:]] [^[:alnum:]_] (note underscore) Within bracket expressions, `\\d', `\\s', and `\\w' lose their outer brackets, and `\\D', `\\S', and `\\W' are illegal. (So, for example, [a-c\\d] is equivalent to [a-c[:digit:]]. Also, [a-c\\D], which is equivalent to [a-c^[:digit:]], is illegal.) A constraint escape (AREs only) is a constraint, matching the empty string if specific conditions are met, written as an escape: 字符 \\A \\m \\M \\y \\Y \\Z \\m \\mnn 意义 matches only at the beginning of the string (see MATCHING, below, for how this differs from `^') matches only at the beginning of a word matches only at the end of a word matches only at the beginning or end of a word matches only at a point that is not the beginning or end of a word matches only at the end of the string (see MATCHING, below, for how this differs from `$') (where m is a nonzero digit) a back reference, see below (where m is a nonzero digit, and nn is some more digits, and the decimal value mnn is not greater than the number of closing capturing parentheses seen so far) a back reference, see below A word is defined as in the specification of [[:<:]] and [[:>:]] above. Constraint escapes are illegal within bracket expressions. A back reference (AREs only) matches the same string matched by the parenthesized subexpression specified by the number, so that (e.g.) ([bc])\\1 matches bb or cc but not `bc'. The subexpression must entirely precede the back reference in the RE. Subexpressions are numbered in the order of their leading parentheses. Non-capturing parentheses do not define subexpressions. 2 8

There is an inherent historical ambiguity between octal character-entry escapes and back references, which is resolved by heuristics, as hinted at above. A leading zero always indicates an octal escape. A single non-zero digit, not followed by another digit, is always taken as a back reference. A multi-digit sequence not starting with a zero is taken as a back reference if it comes after a suitable subexpression (i.e. the number is in the legal range for a back reference), and otherwise is taken as octal. ◆METASYNTAX(内嵌语法) In addition to the main syntax described above, there are some special forms and miscellaneous syntactic facilities available. Normally the flavor of RE being used is specified by application-dependent means. However, this can be overridden by a director. If an RE of any flavor begins with `***:', the rest of the RE is an ARE. If an RE of any flavor begins with `***=', the rest of the RE is taken to be a literal string, with all characters considered ordinary characters. An ARE may begin with embedded options: a sequence (?xyz) (where xyz is one or more alphabetic characters) specifies options affecting the rest of the RE. These supplement, and can override, any options specified by the application. The available option letters are: 字符 b c e i m n p q s t w x 意义 rest of RE is a BRE case-sensitive matching (usual default) rest of RE is an ERE case-insensitive matching (see MATCHING, below) historical synonym for n newline-sensitive matching (see MATCHING, below) partial newline-sensitive matching (see MATCHING, below) rest of RE is a literal string, all ordinary characters non-newline-sensitive matching (usual default) tight syntax (usual default; see below) inverse partial newline-sensitive matching (see MATCHING, below) expanded syntax (see below) Embedded options take effect at the ) terminating the sequence. They are available only at the start of an ARE, and may not be used later within it. In addition to the usual (tight) RE syntax, in which all characters are significant, there is an expanded syntax, available in all flavors of RE with the -expanded switch, or in AREs with the embedded x option. In the expanded syntax, white-space

29

characters are ignored and all characters between a # and the following newline (or the end of the RE) are ignored, permitting paragraphing and commenting a complex RE. There are three exceptions to that basic rule:

a white-space character or `#' preceded by `\\' is retained white space or `#' within a bracket expression is retained

white space and comments are illegal within multi-character symbols like the ARE `(?:' or the BRE `\\('

Expanded-syntax white-space characters are blank, tab, newline, and any character that belongs to the space character class.

Finally, in an ARE, outside bracket expressions, the sequence `(?#ttt)' (where ttt is any text not containing a `)') is a comment, completely ignored. Again, this is not allowed between the characters of multi-character symbols like `(?:'. Such comments are more a historical artifact than a useful facility, and their use is deprecated; use the expanded syntax instead.

None of these metasyntax extensions is available if the application (or an initial ***= director) has specified that the user's input be treated as a literal string rather than as an RE.

◆MATCHING(匹配)

In the event that an RE could match more than one substring of a given string, the RE matches the one starting earliest in the string. If the RE could match more than one substring starting at that point, its choice is determined by its preference: either the longest substring, or the shortest.

Most atoms, and all constraints, have no preference. A parenthesized RE has the same preference (possibly none) as the RE. A quantified atom with quantifier {m} or {m}? has the same preference (possibly none) as the atom itself. A quantified atom with other normal quantifiers (including {m,n} with m equal to n) prefers longest match. A quantified atom with other non-greedy quantifiers (including {m,n}? with m equal to n) prefers shortest match. A branch has the same preference as the first quantified atom in it which has a preference. An RE consisting of two or more branches connected by the | operator prefers longest match.

Subject to the constraints imposed by the rules for matching the whole RE, subexpressions also match the longest or shortest possible substrings, based on their preferences, with subexpressions starting earlier in the RE taking priority over ones starting later. Note that outer subexpressions thus take priority over their component subexpressions.

Note that the quantifiers {1,1} and {1,1}? can be used to force longest and shortest preference, respectively, on a subexpression or a whole RE.

3 0


Tcl脚本语言教程 - 图文(6).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:仪容礼仪规范

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: