re_syntax.n
上传用户:rrhhcc
上传日期:2015-12-11
资源大小:54129k
文件大小:25k
源码类别:

通讯编程

开发平台:

Visual C++

  1. '"
  2. '" Copyright (c) 1998 Sun Microsystems, Inc.
  3. '" Copyright (c) 1999 Scriptics Corporation
  4. '"
  5. '" See the file "license.terms" for information on usage and redistribution
  6. '" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  7. '" 
  8. '" RCS: @(#) $Id: re_syntax.n,v 1.3.32.1 2006/04/12 02:19:53 das Exp $
  9. '"
  10. .so man.macros
  11. .TH re_syntax n "8.1" Tcl "Tcl Built-In Commands"
  12. .BS
  13. .SH NAME
  14. re_syntax - Syntax of Tcl regular expressions.
  15. .BE
  16. .SH DESCRIPTION
  17. .PP
  18. A fIregular expressionfR describes strings of characters.
  19. It's a pattern that matches certain strings and doesn't match others.
  20. .SH "DIFFERENT FLAVORS OF REs"
  21. Regular expressions (``RE''s), as defined by POSIX, come in two
  22. flavors: fIextendedfR REs (``EREs'') and fIbasicfR REs (``BREs'').
  23. EREs are roughly those of the traditional fIegrepfR, while BREs are
  24. roughly those of the traditional fIedfR.  This implementation adds
  25. a third flavor, fIadvancedfR REs (``AREs''), basically EREs with
  26. some significant extensions.
  27. .PP
  28. This manual page primarily describes AREs.  BREs mostly exist for
  29. backward compatibility in some old programs; they will be discussed at
  30. the end.  POSIX EREs are almost an exact subset of AREs.  Features of
  31. AREs that are not present in EREs will be indicated.
  32. .SH "REGULAR EXPRESSION SYNTAX"
  33. .PP
  34. Tcl regular expressions are implemented using the package written by
  35. Henry Spencer, based on the 1003.2 spec and some (not quite all) of
  36. the Perl5 extensions (thanks, Henry!).  Much of the description of
  37. regular expressions below is copied verbatim from his manual entry.
  38. .PP
  39. An ARE is one or more fIbranchesfR,
  40. separated by `fB|fR',
  41. matching anything that matches any of the branches.
  42. .PP
  43. A branch is zero or more fIconstraintsfR or fIquantified atomsfR,
  44. concatenated.
  45. It matches a match for the first, followed by a match for the second, etc;
  46. an empty branch matches the empty string.
  47. .PP
  48. A quantified atom is an fIatomfR possibly followed
  49. by a single fIquantifierfR.
  50. Without a quantifier, it matches a match for the atom.
  51. The quantifiers,
  52. and what a so-quantified atom matches, are:
  53. .RS 2
  54. .TP 6
  55. fB*fR
  56. a sequence of 0 or more matches of the atom
  57. .TP
  58. fB+fR
  59. a sequence of 1 or more matches of the atom
  60. .TP
  61. fB?fR
  62. a sequence of 0 or 1 matches of the atom
  63. .TP
  64. fB{fImfB}fR
  65. a sequence of exactly fImfR matches of the atom
  66. .TP
  67. fB{fImfB,}fR
  68. a sequence of fImfR or more matches of the atom
  69. .TP
  70. fB{fImfB,fInfB}fR
  71. a sequence of fImfR through fInfR (inclusive) matches of the atom;
  72. fImfR may not exceed fInfR
  73. .TP
  74. fB*?  +?  ??  {fImfB}?  {fImfB,}?  {fImfB,fInfB}?fR
  75. fInon-greedyfR quantifiers,
  76. which match the same possibilities,
  77. but prefer the smallest number rather than the largest number
  78. of matches (see MATCHING)
  79. .RE
  80. .PP
  81. The forms using
  82. fB{fR and fB}fR
  83. are known as fIboundfRs.
  84. The numbers
  85. fImfR and fInfR are unsigned decimal integers
  86. with permissible values from 0 to 255 inclusive.
  87. .PP
  88. An atom is one of:
  89. .RS 2
  90. .TP 6
  91. fB(fIrefB)fR
  92. (where fIrefR is any regular expression)
  93. matches a match for
  94. fIrefR, with the match noted for possible reporting
  95. .TP
  96. fB(?:fIrefB)fR
  97. as previous,
  98. but does no reporting
  99. (a ``non-capturing'' set of parentheses)
  100. .TP
  101. fB()fR
  102. matches an empty string,
  103. noted for possible reporting
  104. .TP
  105. fB(?:)fR
  106. matches an empty string,
  107. without reporting
  108. .TP
  109. fB[fIcharsfB]fR
  110. a fIbracket expressionfR,
  111. matching any one of the fIcharsfR (see BRACKET EXPRESSIONS for more detail)
  112. .TP
  113.  fB.fR
  114. matches any single character
  115. .TP
  116. fBefIkfR
  117. (where fIkfR is a non-alphanumeric character)
  118. matches that character taken as an ordinary character,
  119. e.g. ee matches a backslash character
  120. .TP
  121. fBefIcfR
  122. where fIcfR is alphanumeric
  123. (possibly followed by other characters),
  124. an fIescapefR (AREs only),
  125. see ESCAPES below
  126. .TP
  127. fB{fR
  128. when followed by a character other than a digit,
  129. matches the left-brace character `fB{fR';
  130. when followed by a digit, it is the beginning of a
  131. fIboundfR (see above)
  132. .TP
  133. fIxfR
  134. where fIxfR is
  135. a single character with no other significance, matches that character.
  136. .RE
  137. .PP
  138. A fIconstraintfR matches an empty string when specific conditions
  139. are met.
  140. A constraint may not be followed by a quantifier.
  141. The simple constraints are as follows; some more constraints are
  142. described later, under ESCAPES.
  143. .RS 2
  144. .TP 8
  145. fB^fR
  146. matches at the beginning of a line
  147. .TP
  148. fB$fR
  149. matches at the end of a line
  150. .TP
  151. fB(?=fIrefB)fR
  152. fIpositive lookaheadfR (AREs only), matches at any point
  153. where a substring matching fIrefR begins
  154. .TP
  155. fB(?!fIrefB)fR
  156. fInegative lookaheadfR (AREs only), matches at any point
  157. where no substring matching fIrefR begins
  158. .RE
  159. .PP
  160. The lookahead constraints may not contain back references (see later),
  161. and all parentheses within them are considered non-capturing.
  162. .PP
  163. An RE may not end with `fBefR'.
  164. .SH "BRACKET EXPRESSIONS"
  165. A fIbracket expressionfR is a list of characters enclosed in `fB[|]fR'.
  166. It normally matches any single character from the list (but see below).
  167. If the list begins with `fB^fR',
  168. it matches any single character
  169. (but see below) fInotfR from the rest of the list.
  170. .PP
  171. If two characters in the list are separated by `fB-fR',
  172. this is shorthand
  173. for the full fIrangefR of characters between those two (inclusive) in the
  174. collating sequence,
  175. e.g.
  176. fB[0-9]fR
  177. in ASCII matches any decimal digit.
  178. Two ranges may not share an
  179. endpoint, so e.g.
  180. fBa-c-efR
  181. is illegal.
  182. Ranges are very collating-sequence-dependent,
  183. and portable programs should avoid relying on them.
  184. .PP
  185. To include a literal
  186. fB]fR
  187. or
  188. fB-fR
  189. in the list,
  190. the simplest method is to
  191. enclose it in
  192. fB[.fR and fB.]fR
  193. to make it a collating element (see below).
  194. Alternatively,
  195. make it the first character
  196. (following a possible `fB^fR'),
  197. or (AREs only) precede it with `fBefR'.
  198. Alternatively, for `fB-fR',
  199. make it the last character,
  200. or the second endpoint of a range.
  201. To use a literal
  202. fB-fR
  203. as the first endpoint of a range,
  204. make it a collating element
  205. or (AREs only) precede it with `fBefR'.
  206. With the exception of these, some combinations using
  207. fB[fR
  208. (see next
  209. paragraphs), and escapes,
  210. all other special characters lose their
  211. special significance within a bracket expression.
  212. .PP
  213. Within a bracket expression, a collating element (a character,
  214. a multi-character sequence that collates as if it were a single character,
  215. or a collating-sequence name for either)
  216. enclosed in
  217. fB[.fR and fB.]fR
  218. stands for the
  219. sequence of characters of that collating element.
  220. The sequence is a single element of the bracket expression's list.
  221. A bracket expression in a locale that has
  222. multi-character collating elements
  223. can thus match more than one character.
  224. .VS 8.2
  225. So (insidiously), a bracket expression that starts with fB^fR
  226. can match multi-character collating elements even if none of them
  227. appear in the bracket expression!
  228. (fINote:fR Tcl currently has no multi-character collating elements.
  229. This information is only for illustration.)
  230. .PP
  231. For example, assume the collating sequence includes a fBchfR
  232. multi-character collating element.
  233. Then the RE fB[[.ch.]]*cfR (zero or more fBchfP's followed by fBcfP)
  234. matches the first five characters of `fBchchccfR'.
  235. Also, the RE fB[^c]bfR matches all of `fBchbfR'
  236. (because fB[^c]fR matches the multi-character fBchfR).
  237. .VE 8.2
  238. .PP
  239. Within a bracket expression, a collating element enclosed in
  240. fB[=fR
  241. and
  242. fB=]fR
  243. is an equivalence class, standing for the sequences of characters
  244. of all collating elements equivalent to that one, including itself.
  245. (If there are no other equivalent collating elements,
  246. the treatment is as if the enclosing delimiters were `fB[.fR'&
  247. and `fB.]fR'.)
  248. For example, if
  249. fBofR
  250. and
  251. fBo'o^'fR
  252. are the members of an equivalence class,
  253. then `fB[[=o=]]fR', `fB[[=o'o^'=]]fR',
  254. and `fB[oo'o^']fR'&
  255. are all synonymous.
  256. An equivalence class may not be an endpoint
  257. of a range.
  258. .VS 8.2
  259. (fINote:fR 
  260. Tcl currently implements only the Unicode locale.
  261. It doesn't define any equivalence classes.
  262. The examples above are just illustrations.)
  263. .VE 8.2
  264. .PP
  265. Within a bracket expression, the name of a fIcharacter classfR enclosed
  266. in
  267. fB[:fR
  268. and
  269. fB:]fR
  270. stands for the list of all characters
  271. (not all collating elements!)
  272. belonging to that
  273. class.
  274. Standard character classes are:
  275. .PP
  276. .RS
  277. .ne 5
  278. .ta 3c
  279. .nf
  280. fBalphafR A letter. 
  281. fBupperfR An upper-case letter. 
  282. fBlowerfR A lower-case letter. 
  283. fBdigitfR A decimal digit. 
  284. fBxdigitfR A hexadecimal digit. 
  285. fBalnumfR An alphanumeric (letter or digit). 
  286. fBprintfR An alphanumeric (same as alnum).
  287. fBblankfR A space or tab character.
  288. fBspacefR A character producing white space in displayed text. 
  289. fBpunctfR A punctuation character. 
  290. fBgraphfR A character with a visible representation. 
  291. fBcntrlfR A control character. 
  292. .fi
  293. .RE
  294. .PP
  295. A locale may provide others.
  296. .VS 8.2
  297. (Note that the current Tcl implementation has only one locale:
  298. the Unicode locale.)
  299. .VE 8.2
  300. A character class may not be used as an endpoint of a range.
  301. .PP
  302. There are two special cases of bracket expressions:
  303. the bracket expressions
  304. fB[[:<:]]fR
  305. and
  306. fB[[:>:]]fR
  307. are constraints, matching empty strings at
  308. the beginning and end of a word respectively.
  309. '" note, discussion of escapes below references this definition of word
  310. A word is defined as a sequence of
  311. word characters
  312. that is neither preceded nor followed by
  313. word characters.
  314. A word character is an
  315. fIalnumfR
  316. character
  317. or an underscore
  318. (fB_fR).
  319. These special bracket expressions are deprecated;
  320. users of AREs should use constraint escapes instead (see below).
  321. .SH ESCAPES
  322. Escapes (AREs only), which begin with a
  323. fBefR
  324. followed by an alphanumeric character,
  325. come in several varieties:
  326. character entry, class shorthands, constraint escapes, and back references.
  327. A
  328. fBefR
  329. followed by an alphanumeric character but not constituting
  330. a valid escape is illegal in AREs.
  331. In EREs, there are no escapes:
  332. outside a bracket expression,
  333. a
  334. fBefR
  335. followed by an alphanumeric character merely stands for that
  336. character as an ordinary character,
  337. and inside a bracket expression,
  338. fBefR
  339. is an ordinary character.
  340. (The latter is the one actual incompatibility between EREs and AREs.)
  341. .PP
  342. Character-entry escapes (AREs only) exist to make it easier to specify
  343. non-printing and otherwise inconvenient characters in REs:
  344. .RS 2
  345. .TP 5
  346. fBeafR
  347. alert (bell) character, as in C
  348. .TP
  349. fBebfR
  350. backspace, as in C
  351. .TP
  352. fBeBfR
  353. synonym for
  354. fBefR
  355. to help reduce backslash doubling in some
  356. applications where there are multiple levels of backslash processing
  357. .TP
  358. fBecfIXfR
  359. (where X is any character) the character whose
  360. low-order 5 bits are the same as those of
  361. fIXfR,
  362. and whose other bits are all zero
  363. .TP
  364. fBeefR
  365. the character whose collating-sequence name
  366. is `fBESCfR',
  367. or failing that, the character with octal value 033
  368. .TP
  369. fBeffR
  370. formfeed, as in C
  371. .TP
  372. fBenfR
  373. newline, as in C
  374. .TP
  375. fBerfR
  376. carriage return, as in C
  377. .TP
  378. fBetfR
  379. horizontal tab, as in C
  380. .TP
  381. fBeufIwxyzfR
  382. (where
  383. fIwxyzfR
  384. is exactly four hexadecimal digits)
  385. the Unicode character
  386. fBU+fIwxyzfR
  387. in the local byte ordering
  388. .TP
  389. fBeUfIstuvwxyzfR
  390. (where
  391. fIstuvwxyzfR
  392. is exactly eight hexadecimal digits)
  393. reserved for a somewhat-hypothetical Unicode extension to 32 bits
  394. .TP
  395. fBevfR
  396. vertical tab, as in C
  397. are all available.
  398. .TP
  399. fBexfIhhhfR
  400. (where
  401. fIhhhfR
  402. is any sequence of hexadecimal digits)
  403. the character whose hexadecimal value is
  404. fB0xfIhhhfR
  405. (a single character no matter how many hexadecimal digits are used).
  406. .TP
  407. fBe0fR
  408. the character whose value is
  409. fB0fR
  410. .TP
  411. fBefIxyfR
  412. (where
  413. fIxyfR
  414. is exactly two octal digits,
  415. and is not a
  416. fIback referencefR (see below))
  417. the character whose octal value is
  418. fB0fIxyfR
  419. .TP
  420. fBefIxyzfR
  421. (where
  422. fIxyzfR
  423. is exactly three octal digits,
  424. and is not a
  425. back reference (see below))
  426. the character whose octal value is
  427. fB0fIxyzfR
  428. .RE
  429. .PP
  430. Hexadecimal digits are `fB0fR'-`fB9fR', `fBafR'-`fBffR',
  431. and `fBAfR'-`fBFfR'.
  432. Octal digits are `fB0fR'-`fB7fR'.
  433. .PP
  434. The character-entry escapes are always taken as ordinary characters.
  435. For example,
  436. fBe135fR
  437. is
  438. fB]fR
  439. in ASCII,
  440. but
  441. fBe135fR
  442. does not terminate a bracket expression.
  443. Beware, however, that some applications (e.g., C compilers) interpret 
  444. such sequences themselves before the regular-expression package
  445. gets to see them, which may require doubling (quadrupling, etc.) the `fBefR'.
  446. .PP
  447. Class-shorthand escapes (AREs only) provide shorthands for certain commonly-used
  448. character classes:
  449. .RS 2
  450. .TP 10
  451. fBedfR
  452. fB[[:digit:]]fR
  453. .TP
  454. fBesfR
  455. fB[[:space:]]fR
  456. .TP
  457. fBewfR
  458. fB[[:alnum:]_]fR
  459. (note underscore)
  460. .TP
  461. fBeDfR
  462. fB[^[:digit:]]fR
  463. .TP
  464. fBeSfR
  465. fB[^[:space:]]fR
  466. .TP
  467. fBeWfR
  468. fB[^[:alnum:]_]fR
  469. (note underscore)
  470. .RE
  471. .PP
  472. Within bracket expressions, `fBedfR', `fBesfR',
  473. and `fBewfR'&
  474. lose their outer brackets,
  475. and `fBeDfR', `fBeSfR',
  476. and `fBeWfR'&
  477. are illegal.
  478. .VS 8.2
  479. (So, for example, fB[a-ced]fR is equivalent to fB[a-c[:digit:]]fR.
  480. Also, fB[a-ceD]fR, which is equivalent to fB[a-c^[:digit:]]fR, is illegal.)
  481. .VE 8.2
  482. .PP
  483. A constraint escape (AREs only) is a constraint,
  484. matching the empty string if specific conditions are met,
  485. written as an escape:
  486. .RS 2
  487. .TP 6
  488. fBeAfR
  489. matches only at the beginning of the string
  490. (see MATCHING, below, for how this differs from `fB^fR')
  491. .TP
  492. fBemfR
  493. matches only at the beginning of a word
  494. .TP
  495. fBeMfR
  496. matches only at the end of a word
  497. .TP
  498. fBeyfR
  499. matches only at the beginning or end of a word
  500. .TP
  501. fBeYfR
  502. matches only at a point that is not the beginning or end of a word
  503. .TP
  504. fBeZfR
  505. matches only at the end of the string
  506. (see MATCHING, below, for how this differs from `fB$fR')
  507. .TP
  508. fBefImfR
  509. (where
  510. fImfR
  511. is a nonzero digit) a fIback referencefR, see below
  512. .TP
  513. fBefImnnfR
  514. (where
  515. fImfR
  516. is a nonzero digit, and
  517. fInnfR
  518. is some more digits,
  519. and the decimal value
  520. fImnnfR
  521. is not greater than the number of closing capturing parentheses seen so far)
  522. a fIback referencefR, see below
  523. .RE
  524. .PP
  525. A word is defined as in the specification of
  526. fB[[:<:]]fR
  527. and
  528. fB[[:>:]]fR
  529. above.
  530. Constraint escapes are illegal within bracket expressions.
  531. .PP
  532. A back reference (AREs only) matches the same string matched by the parenthesized
  533. subexpression specified by the number,
  534. so that (e.g.)
  535. fB([bc])e1fR
  536. matches
  537. fBbbfR
  538. or
  539. fBccfR
  540. but not `fBbcfR'.
  541. The subexpression must entirely precede the back reference in the RE.
  542. Subexpressions are numbered in the order of their leading parentheses.
  543. Non-capturing parentheses do not define subexpressions.
  544. .PP
  545. There is an inherent historical ambiguity between octal character-entry 
  546. escapes and back references, which is resolved by heuristics,
  547. as hinted at above.
  548. A leading zero always indicates an octal escape.
  549. A single non-zero digit, not followed by another digit,
  550. is always taken as a back reference.
  551. A multi-digit sequence not starting with a zero is taken as a back 
  552. reference if it comes after a suitable subexpression
  553. (i.e. the number is in the legal range for a back reference),
  554. and otherwise is taken as octal.
  555. .SH "METASYNTAX"
  556. In addition to the main syntax described above, there are some special
  557. forms and miscellaneous syntactic facilities available.
  558. .PP
  559. Normally the flavor of RE being used is specified by
  560. application-dependent means.
  561. However, this can be overridden by a fIdirectorfR.
  562. If an RE of any flavor begins with `fB***:fR',
  563. the rest of the RE is an ARE.
  564. If an RE of any flavor begins with `fB***=fR',
  565. the rest of the RE is taken to be a literal string,
  566. with all characters considered ordinary characters.
  567. .PP
  568. An ARE may begin with fIembedded optionsfR:
  569. a sequence
  570. fB(?fIxyzfB)fR
  571. (where
  572. fIxyzfR
  573. is one or more alphabetic characters)
  574. specifies options affecting the rest of the RE.
  575. These supplement, and can override,
  576. any options specified by the application.
  577. The available option letters are:
  578. .RS 2
  579. .TP 3
  580. fBbfR
  581. rest of RE is a BRE
  582. .TP 3
  583. fBcfR
  584. case-sensitive matching (usual default)
  585. .TP 3
  586. fBefR
  587. rest of RE is an ERE
  588. .TP 3
  589. fBifR
  590. case-insensitive matching (see MATCHING, below)
  591. .TP 3
  592. fBmfR
  593. historical synonym for
  594. fBnfR
  595. .TP 3
  596. fBnfR
  597. newline-sensitive matching (see MATCHING, below)
  598. .TP 3
  599. fBpfR
  600. partial newline-sensitive matching (see MATCHING, below)
  601. .TP 3
  602. fBqfR
  603. rest of RE is a literal (``quoted'') string, all ordinary characters
  604. .TP 3
  605. fBsfR
  606. non-newline-sensitive matching (usual default)
  607. .TP 3
  608. fBtfR
  609. tight syntax (usual default; see below)
  610. .TP 3
  611. fBwfR
  612. inverse partial newline-sensitive (``weird'') matching (see MATCHING, below)
  613. .TP 3
  614. fBxfR
  615. expanded syntax (see below)
  616. .RE
  617. .PP
  618. Embedded options take effect at the
  619. fB)fR
  620. terminating the sequence.
  621. They are available only at the start of an ARE,
  622. and may not be used later within it.
  623. .PP
  624. In addition to the usual (fItightfR) RE syntax, in which all characters are
  625. significant, there is an fIexpandedfR syntax,
  626. available in all flavors of RE
  627. with the fB-expandedfR switch, or in AREs with the embedded x option.
  628. In the expanded syntax,
  629. white-space characters are ignored
  630. and all characters between a
  631. fB#fR
  632. and the following newline (or the end of the RE) are ignored,
  633. permitting paragraphing and commenting a complex RE.
  634. There are three exceptions to that basic rule:
  635. .RS 2
  636. .PP
  637. a white-space character or `fB#fR' preceded by `fBefR' is retained
  638. .PP
  639. white space or `fB#fR' within a bracket expression is retained
  640. .PP
  641. white space and comments are illegal within multi-character symbols
  642. like the ARE `fB(?:fR' or the BRE `fBe(fR'
  643. .RE
  644. .PP
  645. Expanded-syntax white-space characters are blank, tab, newline, and
  646. .VS 8.2
  647. any character that belongs to the fIspacefR character class.
  648. .VE 8.2
  649. .PP
  650. Finally, in an ARE,
  651. outside bracket expressions, the sequence `fB(?#fItttfB)fR'
  652. (where
  653. fItttfR
  654. is any text not containing a `fB)fR')
  655. is a comment,
  656. completely ignored.
  657. Again, this is not allowed between the characters of
  658. multi-character symbols like `fB(?:fR'.
  659. Such comments are more a historical artifact than a useful facility,
  660. and their use is deprecated;
  661. use the expanded syntax instead.
  662. .PP
  663. fINonefR of these metasyntax extensions is available if the application
  664. (or an initial
  665. fB***=fR
  666. director)
  667. has specified that the user's input be treated as a literal string
  668. rather than as an RE.
  669. .SH MATCHING
  670. In the event that an RE could match more than one substring of a given
  671. string,
  672. the RE matches the one starting earliest in the string.
  673. If the RE could match more than one substring starting at that point,
  674. its choice is determined by its fIpreferencefR:
  675. either the longest substring, or the shortest.
  676. .PP
  677. Most atoms, and all constraints, have no preference.
  678. A parenthesized RE has the same preference (possibly none) as the RE.
  679. A quantified atom with quantifier
  680. fB{fImfB}fR
  681. or
  682. fB{fImfB}?fR
  683. has the same preference (possibly none) as the atom itself.
  684. A quantified atom with other normal quantifiers (including
  685. fB{fImfB,fInfB}fR
  686. with
  687. fImfR
  688. equal to
  689. fInfR)
  690. prefers longest match.
  691. A quantified atom with other non-greedy quantifiers (including
  692. fB{fImfB,fInfB}?fR
  693. with
  694. fImfR
  695. equal to
  696. fInfR)
  697. prefers shortest match.
  698. A branch has the same preference as the first quantified atom in it
  699. which has a preference.
  700. An RE consisting of two or more branches connected by the
  701. fB|fR
  702. operator prefers longest match.
  703. .PP
  704. Subject to the constraints imposed by the rules for matching the whole RE,
  705. subexpressions also match the longest or shortest possible substrings,
  706. based on their preferences,
  707. with subexpressions starting earlier in the RE taking priority over
  708. ones starting later.
  709. Note that outer subexpressions thus take priority over
  710. their component subexpressions.
  711. .PP
  712. Note that the quantifiers
  713. fB{1,1}fR
  714. and
  715. fB{1,1}?fR
  716. can be used to force longest and shortest preference, respectively,
  717. on a subexpression or a whole RE.
  718. .PP
  719. Match lengths are measured in characters, not collating elements.
  720. An empty string is considered longer than no match at all.
  721. For example,
  722. fBbb*fR
  723. matches the three middle characters of `fBabbbcfR',
  724. fB(week|wee)(night|knights)fR
  725. matches all ten characters of `fBweeknightsfR',
  726. when
  727. fB(.*).*fR
  728. is matched against
  729. fBabcfR
  730. the parenthesized subexpression
  731. matches all three characters, and
  732. when
  733. fB(a*)*fR
  734. is matched against
  735. fBbcfR
  736. both the whole RE and the parenthesized
  737. subexpression match an empty string.
  738. .PP
  739. If case-independent matching is specified,
  740. the effect is much as if all case distinctions had vanished from the
  741. alphabet.
  742. When an alphabetic that exists in multiple cases appears as an
  743. ordinary character outside a bracket expression, it is effectively
  744. transformed into a bracket expression containing both cases,
  745. so that
  746. fBxfR
  747. becomes `fB[xX]fR'.
  748. When it appears inside a bracket expression, all case counterparts
  749. of it are added to the bracket expression, so that
  750. fB[x]fR
  751. becomes
  752. fB[xX]fR
  753. and
  754. fB[^x]fR
  755. becomes `fB[^xX]fR'.
  756. .PP
  757. If newline-sensitive matching is specified, fB.fR
  758. and bracket expressions using
  759. fB^fR
  760. will never match the newline character
  761. (so that matches will never cross newlines unless the RE
  762. explicitly arranges it)
  763. and
  764. fB^fR
  765. and
  766. fB$fR
  767. will match the empty string after and before a newline
  768. respectively, in addition to matching at beginning and end of string
  769. respectively.
  770. ARE
  771. fBeAfR
  772. and
  773. fBeZfR
  774. continue to match beginning or end of string fIonlyfR.
  775. .PP
  776. If partial newline-sensitive matching is specified,
  777. this affects fB.fR
  778. and bracket expressions
  779. as with newline-sensitive matching, but not
  780. fB^fR
  781. and `fB$fR'.
  782. .PP
  783. If inverse partial newline-sensitive matching is specified,
  784. this affects
  785. fB^fR
  786. and
  787. fB$fR
  788. as with
  789. newline-sensitive matching,
  790. but not fB.fR
  791. and bracket expressions.
  792. This isn't very useful but is provided for symmetry.
  793. .SH "LIMITS AND COMPATIBILITY"
  794. No particular limit is imposed on the length of REs.
  795. Programs intended to be highly portable should not employ REs longer
  796. than 256 bytes,
  797. as a POSIX-compliant implementation can refuse to accept such REs.
  798. .PP
  799. The only feature of AREs that is actually incompatible with
  800. POSIX EREs is that
  801. fBefR
  802. does not lose its special
  803. significance inside bracket expressions.
  804. All other ARE features use syntax which is illegal or has
  805. undefined or unspecified effects in POSIX EREs;
  806. the
  807. fB***fR
  808. syntax of directors likewise is outside the POSIX
  809. syntax for both BREs and EREs.
  810. .PP
  811. Many of the ARE extensions are borrowed from Perl, but some have
  812. been changed to clean them up, and a few Perl extensions are not present.
  813. Incompatibilities of note include `fBebfR', `fBeBfR',
  814. the lack of special treatment for a trailing newline,
  815. the addition of complemented bracket expressions to the things
  816. affected by newline-sensitive matching,
  817. the restrictions on parentheses and back references in lookahead constraints,
  818. and the longest/shortest-match (rather than first-match) matching semantics.
  819. .PP
  820. The matching rules for REs containing both normal and non-greedy quantifiers
  821. have changed since early beta-test versions of this package.
  822. (The new rules are much simpler and cleaner,
  823. but don't work as hard at guessing the user's real intentions.)
  824. .PP
  825. Henry Spencer's original 1986 fIregexpfR package,
  826. still in widespread use (e.g., in pre-8.1 releases of Tcl),
  827. implemented an early version of today's EREs.
  828. There are four incompatibilities between fIregexpfR's near-EREs
  829. (`RREs' for short) and AREs.
  830. In roughly increasing order of significance:
  831. .PP
  832. .RS
  833. In AREs,
  834. fBefR
  835. followed by an alphanumeric character is either an
  836. escape or an error,
  837. while in RREs, it was just another way of writing the 
  838. alphanumeric.
  839. This should not be a problem because there was no reason to write
  840. such a sequence in RREs.
  841. .PP
  842. fB{fR
  843. followed by a digit in an ARE is the beginning of a bound,
  844. while in RREs,
  845. fB{fR
  846. was always an ordinary character.
  847. Such sequences should be rare,
  848. and will often result in an error because following characters
  849. will not look like a valid bound.
  850. .PP
  851. In AREs,
  852. fBefR
  853. remains a special character within `fB[|]fR',
  854. so a literal
  855. fBefR
  856. within
  857. fB[|]fR
  858. must be written `fBeefR'.
  859. fBeefR
  860. also gives a literal
  861. fBefR
  862. within
  863. fB[|]fR
  864. in RREs,
  865. but only truly paranoid programmers routinely doubled the backslash.
  866. .PP
  867. AREs report the longest/shortest match for the RE,
  868. rather than the first found in a specified search order.
  869. This may affect some RREs which were written in the expectation that
  870. the first match would be reported.
  871. (The careful crafting of RREs to optimize the search order for fast
  872. matching is obsolete (AREs examine all possible matches
  873. in parallel, and their performance is largely insensitive to their
  874. complexity) but cases where the search order was exploited to deliberately 
  875. find a match which was fInotfR the longest/shortest will need rewriting.)
  876. .RE
  877. .SH "BASIC REGULAR EXPRESSIONS"
  878. BREs differ from EREs in several respects.  `fB|fR', `fB+fR',
  879. and
  880. fB?fR
  881. are ordinary characters and there is no equivalent
  882. for their functionality.
  883. The delimiters for bounds are
  884. fBe{fR
  885. and `fBe}fR',
  886. with
  887. fB{fR
  888. and
  889. fB}fR
  890. by themselves ordinary characters.
  891. The parentheses for nested subexpressions are
  892. fBe(fR
  893. and `fBe)fR',
  894. with
  895. fB(fR
  896. and
  897. fB)fR
  898. by themselves ordinary characters.
  899. fB^fR
  900. is an ordinary character except at the beginning of the
  901. RE or the beginning of a parenthesized subexpression,
  902. fB$fR
  903. is an ordinary character except at the end of the
  904. RE or the end of a parenthesized subexpression,
  905. and
  906. fB*fR
  907. is an ordinary character if it appears at the beginning of the
  908. RE or the beginning of a parenthesized subexpression
  909. (after a possible leading `fB^fR').
  910. Finally,
  911. single-digit back references are available,
  912. and
  913. fBe<fR
  914. and
  915. fBe>fR
  916. are synonyms for
  917. fB[[:<:]]fR
  918. and
  919. fB[[:>:]]fR
  920. respectively;
  921. no other escapes are available.
  922. .SH "SEE ALSO"
  923. RegExp(3), regexp(n), regsub(n), lsearch(n), switch(n), text(n)
  924. .SH KEYWORDS
  925. match, regular expression, string