github的一些开源项目
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

618 lines
21 KiB

  1. .TH PCRE2SYNTAX 3 "12 October 2023" "PCRE2 10.43"
  2. .SH NAME
  3. PCRE2 - Perl-compatible regular expressions (revised API)
  4. .SH "PCRE2 REGULAR EXPRESSION SYNTAX SUMMARY"
  5. .rs
  6. .sp
  7. The full syntax and semantics of the regular expressions that are supported by
  8. PCRE2 are described in the
  9. .\" HREF
  10. \fBpcre2pattern\fP
  11. .\"
  12. documentation. This document contains a quick-reference summary of the syntax.
  13. .
  14. .
  15. .SH "QUOTING"
  16. .rs
  17. .sp
  18. \ex where x is non-alphanumeric is a literal x
  19. \eQ...\eE treat enclosed characters as literal
  20. .sp
  21. Note that white space inside \eQ...\eE is always treated as literal, even if
  22. PCRE2_EXTENDED is set, causing most other white space to be ignored.
  23. .
  24. .
  25. .SH "BRACED ITEMS"
  26. .rs
  27. .sp
  28. With one exception, wherever brace characters { and } are required to enclose
  29. data for constructions such as \eg{2} or \ek{name}, space and/or horizontal tab
  30. characters that follow { or precede } are allowed and are ignored. In the case
  31. of quantifiers, they may also appear before or after the comma. The exception
  32. is \eu{...} which is not Perl-compatible and is recognized only when
  33. PCRE2_EXTRA_ALT_BSUX is set. This is an ECMAScript compatibility feature, and
  34. follows ECMAScript's behaviour.
  35. .
  36. .
  37. .SH "ESCAPED CHARACTERS"
  38. .rs
  39. .sp
  40. This table applies to ASCII and Unicode environments. An unrecognized escape
  41. sequence causes an error.
  42. .sp
  43. \ea alarm, that is, the BEL character (hex 07)
  44. \ecx "control-x", where x is a non-control ASCII character
  45. \ee escape (hex 1B)
  46. \ef form feed (hex 0C)
  47. \en newline (hex 0A)
  48. \er carriage return (hex 0D)
  49. \et tab (hex 09)
  50. \e0dd character with octal code 0dd
  51. \eddd character with octal code ddd, or backreference
  52. \eo{ddd..} character with octal code ddd..
  53. \eN{U+hh..} character with Unicode code point hh.. (Unicode mode only)
  54. \exhh character with hex code hh
  55. \ex{hh..} character with hex code hh..
  56. .sp
  57. If PCRE2_ALT_BSUX or PCRE2_EXTRA_ALT_BSUX is set ("ALT_BSUX mode"), the
  58. following are also recognized:
  59. .sp
  60. \eU the character "U"
  61. \euhhhh character with hex code hhhh
  62. \eu{hh..} character with hex code hh.. but only for EXTRA_ALT_BSUX
  63. .sp
  64. When \ex is not followed by {, from zero to two hexadecimal digits are read,
  65. but in ALT_BSUX mode \ex must be followed by two hexadecimal digits to be
  66. recognized as a hexadecimal escape; otherwise it matches a literal "x".
  67. Likewise, if \eu (in ALT_BSUX mode) is not followed by four hexadecimal digits
  68. or (in EXTRA_ALT_BSUX mode) a sequence of hex digits in curly brackets, it
  69. matches a literal "u".
  70. .P
  71. Note that \e0dd is always an octal code. The treatment of backslash followed by
  72. a non-zero digit is complicated; for details see the section
  73. .\" HTML <a href="pcre2pattern.html#digitsafterbackslash">
  74. .\" </a>
  75. "Non-printing characters"
  76. .\"
  77. in the
  78. .\" HREF
  79. \fBpcre2pattern\fP
  80. .\"
  81. documentation, where details of escape processing in EBCDIC environments are
  82. also given. \eN{U+hh..} is synonymous with \ex{hh..} in PCRE2 but is not
  83. supported in EBCDIC environments. Note that \eN not followed by an opening
  84. curly bracket has a different meaning (see below).
  85. .
  86. .
  87. .SH "CHARACTER TYPES"
  88. .rs
  89. .sp
  90. . any character except newline;
  91. in dotall mode, any character whatsoever
  92. \eC one code unit, even in UTF mode (best avoided)
  93. \ed a decimal digit
  94. \eD a character that is not a decimal digit
  95. \eh a horizontal white space character
  96. \eH a character that is not a horizontal white space character
  97. \eN a character that is not a newline
  98. \ep{\fIxx\fP} a character with the \fIxx\fP property
  99. \eP{\fIxx\fP} a character without the \fIxx\fP property
  100. \eR a newline sequence
  101. \es a white space character
  102. \eS a character that is not a white space character
  103. \ev a vertical white space character
  104. \eV a character that is not a vertical white space character
  105. \ew a "word" character
  106. \eW a "non-word" character
  107. \eX a Unicode extended grapheme cluster
  108. .sp
  109. \eC is dangerous because it may leave the current matching point in the middle
  110. of a UTF-8 or UTF-16 character. The application can lock out the use of \eC by
  111. setting the PCRE2_NEVER_BACKSLASH_C option. It is also possible to build PCRE2
  112. with the use of \eC permanently disabled.
  113. .P
  114. By default, \ed, \es, and \ew match only ASCII characters, even in UTF-8 mode
  115. or in the 16-bit and 32-bit libraries. However, if locale-specific matching is
  116. happening, \es and \ew may also match characters with code points in the range
  117. 128-255. If the PCRE2_UCP option is set, the behaviour of these escape
  118. sequences is changed to use Unicode properties and they match many more
  119. characters, but there are some option settings that can restrict individual
  120. sequences to matching only ASCII characters.
  121. .P
  122. Property descriptions in \ep and \eP are matched caselessly; hyphens,
  123. underscores, and white space are ignored, in accordance with Unicode's "loose
  124. matching" rules.
  125. .
  126. .
  127. .SH "GENERAL CATEGORY PROPERTIES FOR \ep and \eP"
  128. .rs
  129. .sp
  130. C Other
  131. Cc Control
  132. Cf Format
  133. Cn Unassigned
  134. Co Private use
  135. Cs Surrogate
  136. .sp
  137. L Letter
  138. Ll Lower case letter
  139. Lm Modifier letter
  140. Lo Other letter
  141. Lt Title case letter
  142. Lu Upper case letter
  143. Lc Ll, Lu, or Lt
  144. L& Ll, Lu, or Lt
  145. .sp
  146. M Mark
  147. Mc Spacing mark
  148. Me Enclosing mark
  149. Mn Non-spacing mark
  150. .sp
  151. N Number
  152. Nd Decimal number
  153. Nl Letter number
  154. No Other number
  155. .sp
  156. P Punctuation
  157. Pc Connector punctuation
  158. Pd Dash punctuation
  159. Pe Close punctuation
  160. Pf Final punctuation
  161. Pi Initial punctuation
  162. Po Other punctuation
  163. Ps Open punctuation
  164. .sp
  165. S Symbol
  166. Sc Currency symbol
  167. Sk Modifier symbol
  168. Sm Mathematical symbol
  169. So Other symbol
  170. .sp
  171. Z Separator
  172. Zl Line separator
  173. Zp Paragraph separator
  174. Zs Space separator
  175. .
  176. .
  177. .SH "PCRE2 SPECIAL CATEGORY PROPERTIES FOR \ep and \eP"
  178. .rs
  179. .sp
  180. Xan Alphanumeric: union of properties L and N
  181. Xps POSIX space: property Z or tab, NL, VT, FF, CR
  182. Xsp Perl space: property Z or tab, NL, VT, FF, CR
  183. Xuc Universally-named character: one that can be
  184. represented by a Universal Character Name
  185. Xwd Perl word: property Xan or underscore
  186. .sp
  187. Perl and POSIX space are now the same. Perl added VT to its space character set
  188. at release 5.18.
  189. .
  190. .
  191. .SH "BINARY PROPERTIES FOR \ep AND \eP"
  192. .rs
  193. .sp
  194. Unicode defines a number of binary properties, that is, properties whose only
  195. values are true or false. You can obtain a list of those that are recognized by
  196. \ep and \eP, along with their abbreviations, by running this command:
  197. .sp
  198. pcre2test -LP
  199. .
  200. .
  201. .
  202. .SH "SCRIPT MATCHING WITH \ep AND \eP"
  203. .rs
  204. .sp
  205. Many script names and their 4-letter abbreviations are recognized in
  206. \ep{sc:...} or \ep{scx:...} items, or on their own with \ep (and also \eP of
  207. course). You can obtain a list of these scripts by running this command:
  208. .sp
  209. pcre2test -LS
  210. .
  211. .
  212. .
  213. .SH "THE BIDI_CLASS PROPERTY FOR \ep AND \eP"
  214. .rs
  215. .sp
  216. \ep{Bidi_Class:<class>} matches a character with the given class
  217. \ep{BC:<class>} matches a character with the given class
  218. .sp
  219. The recognized classes are:
  220. .sp
  221. AL Arabic letter
  222. AN Arabic number
  223. B paragraph separator
  224. BN boundary neutral
  225. CS common separator
  226. EN European number
  227. ES European separator
  228. ET European terminator
  229. FSI first strong isolate
  230. L left-to-right
  231. LRE left-to-right embedding
  232. LRI left-to-right isolate
  233. LRO left-to-right override
  234. NSM non-spacing mark
  235. ON other neutral
  236. PDF pop directional format
  237. PDI pop directional isolate
  238. R right-to-left
  239. RLE right-to-left embedding
  240. RLI right-to-left isolate
  241. RLO right-to-left override
  242. S segment separator
  243. WS which space
  244. .
  245. .
  246. .SH "CHARACTER CLASSES"
  247. .rs
  248. .sp
  249. [...] positive character class
  250. [^...] negative character class
  251. [x-y] range (can be used for hex characters)
  252. [[:xxx:]] positive POSIX named set
  253. [[:^xxx:]] negative POSIX named set
  254. .sp
  255. alnum alphanumeric
  256. alpha alphabetic
  257. ascii 0-127
  258. blank space or tab
  259. cntrl control character
  260. digit decimal digit
  261. graph printing, excluding space
  262. lower lower case letter
  263. print printing, including space
  264. punct printing, excluding alphanumeric
  265. space white space
  266. upper upper case letter
  267. word same as \ew
  268. xdigit hexadecimal digit
  269. .sp
  270. In PCRE2, POSIX character set names recognize only ASCII characters by default,
  271. but some of them use Unicode properties if PCRE2_UCP is set. You can use
  272. \eQ...\eE inside a character class.
  273. .
  274. .
  275. .SH "QUANTIFIERS"
  276. .rs
  277. .sp
  278. ? 0 or 1, greedy
  279. ?+ 0 or 1, possessive
  280. ?? 0 or 1, lazy
  281. * 0 or more, greedy
  282. *+ 0 or more, possessive
  283. *? 0 or more, lazy
  284. + 1 or more, greedy
  285. ++ 1 or more, possessive
  286. +? 1 or more, lazy
  287. {n} exactly n
  288. {n,m} at least n, no more than m, greedy
  289. {n,m}+ at least n, no more than m, possessive
  290. {n,m}? at least n, no more than m, lazy
  291. {n,} n or more, greedy
  292. {n,}+ n or more, possessive
  293. {n,}? n or more, lazy
  294. {,m} zero up to m, greedy
  295. {,m}+ zero up to m, possessive
  296. {,m}? zero up to m, lazy
  297. .
  298. .
  299. .SH "ANCHORS AND SIMPLE ASSERTIONS"
  300. .rs
  301. .sp
  302. \eb word boundary
  303. \eB not a word boundary
  304. ^ start of subject
  305. also after an internal newline in multiline mode
  306. (after any newline if PCRE2_ALT_CIRCUMFLEX is set)
  307. \eA start of subject
  308. $ end of subject
  309. also before newline at end of subject
  310. also before internal newline in multiline mode
  311. \eZ end of subject
  312. also before newline at end of subject
  313. \ez end of subject
  314. \eG first matching position in subject
  315. .
  316. .
  317. .SH "REPORTED MATCH POINT SETTING"
  318. .rs
  319. .sp
  320. \eK set reported start of match
  321. .sp
  322. From release 10.38 \eK is not permitted by default in lookaround assertions,
  323. for compatibility with Perl. However, if the PCRE2_EXTRA_ALLOW_LOOKAROUND_BSK
  324. option is set, the previous behaviour is re-enabled. When this option is set,
  325. \eK is honoured in positive assertions, but ignored in negative ones.
  326. .
  327. .
  328. .SH "ALTERNATION"
  329. .rs
  330. .sp
  331. expr|expr|expr...
  332. .
  333. .
  334. .SH "CAPTURING"
  335. .rs
  336. .sp
  337. (...) capture group
  338. (?<name>...) named capture group (Perl)
  339. (?'name'...) named capture group (Perl)
  340. (?P<name>...) named capture group (Python)
  341. (?:...) non-capture group
  342. (?|...) non-capture group; reset group numbers for
  343. capture groups in each alternative
  344. .sp
  345. In non-UTF modes, names may contain underscores and ASCII letters and digits;
  346. in UTF modes, any Unicode letters and Unicode decimal digits are permitted. In
  347. both cases, a name must not start with a digit.
  348. .
  349. .
  350. .SH "ATOMIC GROUPS"
  351. .rs
  352. .sp
  353. (?>...) atomic non-capture group
  354. (*atomic:...) atomic non-capture group
  355. .
  356. .
  357. .SH "COMMENT"
  358. .rs
  359. .sp
  360. (?#....) comment (not nestable)
  361. .
  362. .
  363. .SH "OPTION SETTING"
  364. .rs
  365. Changes of these options within a group are automatically cancelled at the end
  366. of the group.
  367. .sp
  368. (?a) all ASCII options
  369. (?aD) restrict \ed to ASCII in UCP mode
  370. (?aS) restrict \es to ASCII in UCP mode
  371. (?aW) restrict \ew to ASCII in UCP mode
  372. (?aP) restrict all POSIX classes to ASCII in UCP mode
  373. (?aT) restrict POSIX digit classes to ASCII in UCP mode
  374. (?i) caseless
  375. (?J) allow duplicate named groups
  376. (?m) multiline
  377. (?n) no auto capture
  378. (?r) restrict caseless to either ASCII or non-ASCII
  379. (?s) single line (dotall)
  380. (?U) default ungreedy (lazy)
  381. (?x) ignore white space except in classes or \eQ...\eE
  382. (?xx) as (?x) but also ignore space and tab in classes
  383. (?-...) unset the given option(s)
  384. (?^) unset imnrsx options
  385. .sp
  386. (?aP) implies (?aT) as well, though this has no additional effect. However, it
  387. means that (?-aP) is really (?-PT) which disables all ASCII restrictions for
  388. POSIX classes.
  389. .P
  390. Unsetting x or xx unsets both. Several options may be set at once, and a
  391. mixture of setting and unsetting such as (?i-x) is allowed, but there may be
  392. only one hyphen. Setting (but no unsetting) is allowed after (?^ for example
  393. (?^in). An option setting may appear at the start of a non-capture group, for
  394. example (?i:...).
  395. .P
  396. The following are recognized only at the very start of a pattern or after one
  397. of the newline or \eR options with similar syntax. More than one of them may
  398. appear. For the first three, d is a decimal number.
  399. .sp
  400. (*LIMIT_DEPTH=d) set the backtracking limit to d
  401. (*LIMIT_HEAP=d) set the heap size limit to d * 1024 bytes
  402. (*LIMIT_MATCH=d) set the match limit to d
  403. (*NOTEMPTY) set PCRE2_NOTEMPTY when matching
  404. (*NOTEMPTY_ATSTART) set PCRE2_NOTEMPTY_ATSTART when matching
  405. (*NO_AUTO_POSSESS) no auto-possessification (PCRE2_NO_AUTO_POSSESS)
  406. (*NO_DOTSTAR_ANCHOR) no .* anchoring (PCRE2_NO_DOTSTAR_ANCHOR)
  407. (*NO_JIT) disable JIT optimization
  408. (*NO_START_OPT) no start-match optimization (PCRE2_NO_START_OPTIMIZE)
  409. (*UTF) set appropriate UTF mode for the library in use
  410. (*UCP) set PCRE2_UCP (use Unicode properties for \ed etc)
  411. .sp
  412. Note that LIMIT_DEPTH, LIMIT_HEAP, and LIMIT_MATCH can only reduce the value of
  413. the limits set by the caller of \fBpcre2_match()\fP or \fBpcre2_dfa_match()\fP,
  414. not increase them. LIMIT_RECURSION is an obsolete synonym for LIMIT_DEPTH. The
  415. application can lock out the use of (*UTF) and (*UCP) by setting the
  416. PCRE2_NEVER_UTF or PCRE2_NEVER_UCP options, respectively, at compile time.
  417. .
  418. .
  419. .SH "NEWLINE CONVENTION"
  420. .rs
  421. .sp
  422. These are recognized only at the very start of the pattern or after option
  423. settings with a similar syntax.
  424. .sp
  425. (*CR) carriage return only
  426. (*LF) linefeed only
  427. (*CRLF) carriage return followed by linefeed
  428. (*ANYCRLF) all three of the above
  429. (*ANY) any Unicode newline sequence
  430. (*NUL) the NUL character (binary zero)
  431. .
  432. .
  433. .SH "WHAT \eR MATCHES"
  434. .rs
  435. .sp
  436. These are recognized only at the very start of the pattern or after option
  437. setting with a similar syntax.
  438. .sp
  439. (*BSR_ANYCRLF) CR, LF, or CRLF
  440. (*BSR_UNICODE) any Unicode newline sequence
  441. .
  442. .
  443. .SH "LOOKAHEAD AND LOOKBEHIND ASSERTIONS"
  444. .rs
  445. .sp
  446. (?=...) )
  447. (*pla:...) ) positive lookahead
  448. (*positive_lookahead:...) )
  449. .sp
  450. (?!...) )
  451. (*nla:...) ) negative lookahead
  452. (*negative_lookahead:...) )
  453. .sp
  454. (?<=...) )
  455. (*plb:...) ) positive lookbehind
  456. (*positive_lookbehind:...) )
  457. .sp
  458. (?<!...) )
  459. (*nlb:...) ) negative lookbehind
  460. (*negative_lookbehind:...) )
  461. .sp
  462. Each top-level branch of a lookbehind must have a limit for the number of
  463. characters it matches. If any branch can match a variable number of characters,
  464. the maximum for each branch is limited to a value set by the caller of
  465. \fBpcre2_compile()\fP or defaulted. The default is set when PCRE2 is built
  466. (ultimate default 255). If every branch matches a fixed number of characters,
  467. the limit for each branch is 65535 characters.
  468. .
  469. .
  470. .SH "NON-ATOMIC LOOKAROUND ASSERTIONS"
  471. .rs
  472. .sp
  473. These assertions are specific to PCRE2 and are not Perl-compatible.
  474. .sp
  475. (?*...) )
  476. (*napla:...) ) synonyms
  477. (*non_atomic_positive_lookahead:...) )
  478. .sp
  479. (?<*...) )
  480. (*naplb:...) ) synonyms
  481. (*non_atomic_positive_lookbehind:...) )
  482. .
  483. .
  484. .SH "SCRIPT RUNS"
  485. .rs
  486. .sp
  487. (*script_run:...) ) script run, can be backtracked into
  488. (*sr:...) )
  489. .sp
  490. (*atomic_script_run:...) ) atomic script run
  491. (*asr:...) )
  492. .
  493. .
  494. .SH "BACKREFERENCES"
  495. .rs
  496. .sp
  497. \en reference by number (can be ambiguous)
  498. \egn reference by number
  499. \eg{n} reference by number
  500. \eg+n relative reference by number (PCRE2 extension)
  501. \eg-n relative reference by number
  502. \eg{+n} relative reference by number (PCRE2 extension)
  503. \eg{-n} relative reference by number
  504. \ek<name> reference by name (Perl)
  505. \ek'name' reference by name (Perl)
  506. \eg{name} reference by name (Perl)
  507. \ek{name} reference by name (.NET)
  508. (?P=name) reference by name (Python)
  509. .
  510. .
  511. .SH "SUBROUTINE REFERENCES (POSSIBLY RECURSIVE)"
  512. .rs
  513. .sp
  514. (?R) recurse whole pattern
  515. (?n) call subroutine by absolute number
  516. (?+n) call subroutine by relative number
  517. (?-n) call subroutine by relative number
  518. (?&name) call subroutine by name (Perl)
  519. (?P>name) call subroutine by name (Python)
  520. \eg<name> call subroutine by name (Oniguruma)
  521. \eg'name' call subroutine by name (Oniguruma)
  522. \eg<n> call subroutine by absolute number (Oniguruma)
  523. \eg'n' call subroutine by absolute number (Oniguruma)
  524. \eg<+n> call subroutine by relative number (PCRE2 extension)
  525. \eg'+n' call subroutine by relative number (PCRE2 extension)
  526. \eg<-n> call subroutine by relative number (PCRE2 extension)
  527. \eg'-n' call subroutine by relative number (PCRE2 extension)
  528. .
  529. .
  530. .SH "CONDITIONAL PATTERNS"
  531. .rs
  532. .sp
  533. (?(condition)yes-pattern)
  534. (?(condition)yes-pattern|no-pattern)
  535. .sp
  536. (?(n) absolute reference condition
  537. (?(+n) relative reference condition (PCRE2 extension)
  538. (?(-n) relative reference condition (PCRE2 extension)
  539. (?(<name>) named reference condition (Perl)
  540. (?('name') named reference condition (Perl)
  541. (?(name) named reference condition (PCRE2, deprecated)
  542. (?(R) overall recursion condition
  543. (?(Rn) specific numbered group recursion condition
  544. (?(R&name) specific named group recursion condition
  545. (?(DEFINE) define groups for reference
  546. (?(VERSION[>]=n.m) test PCRE2 version
  547. (?(assert) assertion condition
  548. .sp
  549. Note the ambiguity of (?(R) and (?(Rn) which might be named reference
  550. conditions or recursion tests. Such a condition is interpreted as a reference
  551. condition if the relevant named group exists.
  552. .
  553. .
  554. .SH "BACKTRACKING CONTROL"
  555. .rs
  556. .sp
  557. All backtracking control verbs may be in the form (*VERB:NAME). For (*MARK) the
  558. name is mandatory, for the others it is optional. (*SKIP) changes its behaviour
  559. if :NAME is present. The others just set a name for passing back to the caller,
  560. but this is not a name that (*SKIP) can see. The following act immediately they
  561. are reached:
  562. .sp
  563. (*ACCEPT) force successful match
  564. (*FAIL) force backtrack; synonym (*F)
  565. (*MARK:NAME) set name to be passed back; synonym (*:NAME)
  566. .sp
  567. The following act only when a subsequent match failure causes a backtrack to
  568. reach them. They all force a match failure, but they differ in what happens
  569. afterwards. Those that advance the start-of-match point do so only if the
  570. pattern is not anchored.
  571. .sp
  572. (*COMMIT) overall failure, no advance of starting point
  573. (*PRUNE) advance to next starting character
  574. (*SKIP) advance to current matching position
  575. (*SKIP:NAME) advance to position corresponding to an earlier
  576. (*MARK:NAME); if not found, the (*SKIP) is ignored
  577. (*THEN) local failure, backtrack to next alternation
  578. .sp
  579. The effect of one of these verbs in a group called as a subroutine is confined
  580. to the subroutine call.
  581. .
  582. .
  583. .SH "CALLOUTS"
  584. .rs
  585. .sp
  586. (?C) callout (assumed number 0)
  587. (?Cn) callout with numerical data n
  588. (?C"text") callout with string data
  589. .sp
  590. The allowed string delimiters are ` ' " ^ % # $ (which are the same for the
  591. start and the end), and the starting delimiter { matched with the ending
  592. delimiter }. To encode the ending delimiter within the string, double it.
  593. .
  594. .
  595. .SH "SEE ALSO"
  596. .rs
  597. .sp
  598. \fBpcre2pattern\fP(3), \fBpcre2api\fP(3), \fBpcre2callout\fP(3),
  599. \fBpcre2matching\fP(3), \fBpcre2\fP(3).
  600. .
  601. .
  602. .SH AUTHOR
  603. .rs
  604. .sp
  605. .nf
  606. Philip Hazel
  607. Retired from University Computing Service
  608. Cambridge, England.
  609. .fi
  610. .
  611. .
  612. .SH REVISION
  613. .rs
  614. .sp
  615. .nf
  616. Last updated: 12 October 2023
  617. Copyright (c) 1997-2023 University of Cambridge.
  618. .fi