1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>DC - Syntax of Regular Expressions</title>
<link rel="stylesheet" type="text/css" href="doublecmd.css">
<link rel="shortcut icon" href="../../pixmaps/common/favicon.ico">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<a name="topofpage"></a>
<div class="header"><a href="index.html"><img src="../../pixmaps/common/dclogo2017.png" alt="Double Commander" height="48" width="374"></a>
<div id="global-nav" class="nav"><a title="Double Commander Home Page" href="https://doublecmd.sourceforge.io/" target="_blank">Homepage</a> | <a title="Double Commander Wiki" href="https://github.com/doublecmd/doublecmd/wiki" target="_blank">Wiki</a></div>
</div>
<div class="footer"><div class="nav"><a title="Index" href="index.html">Index</a> | <a title="Previous page" href="cmds.html">Previous</a> | <a title="Next page" href="lua.html">Next</a></div></div>
<div class="dchelpage">
<div>
<h1>2.14. Syntax of Regular Expressions</h1>
</div>
<div>
<h2>Content</h2>
<dl>
<dt>1. <a href="#preface">Introduction</a></dt>
<dt>2. <a href="#compare">Simple matches</a></dt>
<dt>3. <a href="#esc">Escape sequences</a></dt>
<dt>4. <a href="#symbols">Character classes</a></dt>
<dt>5. <a href="#meta">Metacharacters</a></dt>
<dd>
<dl>
<dt>5.1. <a href="#meta-line">Metacharacters - line separators</a></dt>
<dt>5.2. <a href="#meta-std">Metacharacters - predefined classes</a></dt>
<dt>5.3. <a href="#meta-words">Metacharacters - word boundaries</a></dt>
<dt>5.4. <a href="#meta-dup">Metacharacters - iterators</a></dt>
<dt>5.5. <a href="#meta-vars">Metacharacters - alternatives</a></dt>
<dt>5.6. <a href="#meta-sub">Metacharacters - subexpressions</a></dt>
<dt>5.7. <a href="#meta-back">Metacharacters - backreferences</a></dt>
</dl>
</dd>
<dt>6. <a href="#look">Assertions (lookahead and lookbehind assertions)</a></dt>
<dt>7. <a href="#noncapt">Non-capturing groups</a></dt>
<dt>8. <a href="#agroups">Atomic groups</a></dt>
<dt>9. <a href="#unicode">Unicode categories</a></dt>
<dt>10. <a href="#modifiers">Modifiers</a></dt>
</dl>
</div>
<p>Double Commander uses the free library <a href="https://regex.sorokin.engineer/en/latest/" target="_blank">TRegExpr</a> by Andrey Sorokin.</p>
<p>Most of the explanations are from the help file for this library.</p>
<div>
<h2><a name="preface">1. Introduction</a></h2>
<p>Regular Expressions are a widely-used method of specifying patterns of text to search for. Special characters (<span class="bold">metacharacters</span>) allow us to specify, for instance, that a particular string we are looking for occurs at the beginning or end of a line, or contains <span class="bold">n</span> recurrences of a certain character or group of characters.</p>
<p>Double Commander supports regular expressions in the following functions:</p>
<ul>
<li><a href="findfiles.html">Find files</a> (file names or content search)</li>
<li>In the <a href="multirename.html">Multi-Rename Tool</a></li>
<li>In the internal editor</li>
<li>In the internal viewer</li>
</ul>
<p>The TRegExp library supports two modes of operation: ANSI and Unicode. When searching in text files, Double Commander uses both (depending on the file encoding). When searching by name, Unicode is used.</p>
<p class="navback"><a href="#topofpage">(back to top)</a></p>
</div>
<div>
<h2><a name="compare">2. Simple matches</a></h2>
<p>Any single character matches itself, unless it is a metacharacter with a special meaning described below.</p>
<p>A series of characters matches that series of characters in the target string, so the pattern <code>bluh</code> would match <code>bluh</code> in the target string.</p>
<p>We can cause characters that normally function as metacharacters or <span class="bold">escape sequences</span> to be interpreted literally by "escaping" them by preceding them with a backslash <code>\</code>, for instance: metacharacter <code>^</code> match beginning of string, but <code>\^</code> match character <code>^</code>, <code>\\</code> match <code>\</code> and so on.</p>
<p>Here are some examples:</p>
<table>
<tr class="rowcategorytitle"><th colspan="2">Example of simple match</th></tr>
<tr class="rowsubtitle"><th class="namecolumn">Expression</th><th class="categorydesccolumn">Result</th></tr>
<tr>
<td class="varcell"><div class="firstcolumnaleft">foobar</div></td>
<td class="hintcell">matches string <code>foobar</code></td>
</tr>
<tr>
<td class="varcell"><div class="firstcolumnaleft">\^FooBarPtr</div></td>
<td class="hintcell">matches <code>^FooBarPtr</code></td>
</tr>
</table>
<p class="navback"><a href="#topofpage">(back to top)</a></p>
</div>
<div>
<h2><a name="esc">3. Escape sequences</a></h2>
<p>Characters may be specified using a escape sequences syntax much like that used in C and Perl: <code>\n</code> matches a newline, <code>\t</code> a tab, etc.</p>
<p>More generally, <code>\xnn</code>, where <code>nn</code> is a string of hexadecimal digits, matches the character whose ASCII value is <code>nn</code>.</p>
<p>If you need wide (Unicode) character code, you can use <code>\x{nnnn}</code>, where <code>nnnn</code> – one or more hexadecimal digits.</p>
<table>
<tr class="rowcategorytitle"><th colspan="2">Escape sequences</th></tr>
<tr class="rowsubtitle"><th class="namecolumn">Expression</th><th class="categorydesccolumn">Result</th></tr>
<tr>
<td class="varcell"><div class="firstcolumnaleft">\xnn</div></td>
<td class="hintcell">char with hex code <code>nn</code></td>
</tr>
<tr>
<td class="varcell"><div class="firstcolumnaleft">\x{nnnn}</div></td>
<td class="hintcell">char with hex code <code>nnnn</code> (one byte for plain text and two bytes for Unicode)</td>
</tr>
<tr>
<td class="varcell"><div class="firstcolumnaleft">\t</div></td>
<td class="hintcell">tab (HT/TAB), same as <code>\x09</code></td>
</tr>
<tr>
<td class="varcell"><div class="firstcolumnaleft">\n</div></td>
<td class="hintcell">newline (NL/LF), same as <code>\x0a</code></td>
</tr>
<tr>
<td class="varcell"><div class="firstcolumnaleft">\r</div></td>
<td class="hintcell">carriage return (CR), same as <code>\x0d</code></td>
</tr>
<tr>
<td class="varcell"><div class="firstcolumnaleft">\f</div></td>
<td class="hintcell">form feed (FF), same as <code>\x0c</code></td>
</tr>
<tr>
<td class="varcell"><div class="firstcolumnaleft">\a</div></td>
<td class="hintcell">alarm (bell) (BEL), same as <code>\x07</code></td>
</tr>
<tr>
<td class="varcell"><div class="firstcolumnaleft">\e</div></td>
<td class="hintcell">escape (ESC), same as <code>\x1b</code></td>
</tr>
</table>
<p>Here are some examples:</p>
<table>
<tr class="rowcategorytitle"><th colspan="2">Example of escape sequences</th></tr>
<tr class="rowsubtitle"><th class="namecolumn">Expression</th><th class="categorydesccolumn">Result</th></tr>
<tr>
<td class="varcell"><div class="firstcolumnaleft">foo\x20bar</div></td>
<td class="hintcell">matches <code>foo bar</code> (note space in the middle)</td>
</tr>
<tr>
<td class="varcell"><div class="firstcolumnaleft">\tfoobar</div></td>
<td class="hintcell">matches <code>foobar</code> predefined by tab</td>
</tr>
</table>
<p class="navback"><a href="#topofpage">(back to top)</a></p>
</div>
<div>
<h2><a name="symbols">4. Character classes</a></h2>
<p>You can specify a character class, by enclosing a list of characters in <code>[]</code>, which will match any <span class="bold">one</span> character from the list.</p>
<p>If the first character after the <code>[</code> is <code>^</code>, the class matches any character <span class="bold">not</span> in the list.</p>
<p>Within a list, the <code>-</code> character is used to specify a <span class="bold">range</span>, so that <code>a-z</code> represents all characters between <code>a</code> and <code>z</code>, inclusive.</p>
<p>If you want <code>-</code> itself to be a member of a class, put it at the start or end of the list, or escape it with a backslash.</p>
<p>If you want <code>]</code> you may place it at the start of list or escape it with a backslash.</p>
<table>
<tr class="rowcategorytitle"><th colspan="2">Character classes</th></tr>
<tr class="rowsubtitle"><th class="namecolumn">Expression</th><th class="categorydesccolumn">Result</th></tr>
<tr>
<td class="varcell"><div class="firstcolumnaleft">[-az]</div></td>
<td class="hintcell">matches <code>a</code>, <code>z</code> and <code>-</code></td>
</tr>
<tr>
<td class="varcell"><div class="firstcolumnaleft">[az-]</div></td>
<td class="hintcell">matches <code>a</code>, <code>z</code> and <code>-</code></td>
</tr>
<tr>
<td class="varcell"><div class="firstcolumnaleft">[a\-z]</div></td>
<td class="hintcell">matches <code>a</code>, <code>z</code> and <code>-</code></td>
</tr>
<tr>
<td class="varcell"><div class="firstcolumnaleft">[a-z]</div></td>
<td class="hintcell">matches all twenty six small characters from <code>a</code> to <code>z</code></td>
</tr>
<tr>
<td class="varcell"><div class="firstcolumnaleft">[\n-\x0D]</div></td>
<td class="hintcell">matches any of <code>\x10</code>, <code>\x11</code>, <code>\x12</code>, <code>\x13</code></td>
</tr>
<tr>
<td class="varcell"><div class="firstcolumnaleft">[\d-t]</div></td>
<td class="hintcell">matches any digit, <code>-</code> or <code>t</code></td>
</tr>
<tr>
<td class="varcell"><div class="firstcolumnaleft">[]-a]</div></td>
<td class="hintcell">matches any char from <code>]</code>..<code>a</code></td>
</tr>
</table>
<p>Here are some examples:</p>
<table>
<tr class="rowcategorytitle"><th colspan="2">Example of character classes</th></tr>
<tr class="rowsubtitle"><th class="namecolumn">Expression</th><th class="categorydesccolumn">Result</th></tr>
<tr>
<td class="varcell"><div class="firstcolumnaleft">foob[aeiou]r</div></td>
<td class="hintcell">finds strings <code>foobar</code>, <code>foober</code> etc. but not <code>foobbr</code>, <code>foobcr</code> etc.</td>
</tr>
<tr>
<td class="varcell"><div class="firstcolumnaleft">foob[^aeiou]r</div></td>
<td class="hintcell">find strings <code>foobbr</code>, <code>foobcr</code> etc. but not <code>foobar</code>, <code>foober</code> etc.</td>
</tr>
</table>
<p class="navback"><a href="#topofpage">(back to top)</a></p>
</div>
<div>
<h2><a name="meta">5. Metacharacters</a></h2>
<p>Metacharacters are special characters which are the essence of Regular Expressions.</p>
<p>There are different types of metacharacters, described below.</p>
<p class="navback"><a href="#topofpage">(back to top)</a></p>
</div>
<div>
<h2><a name="meta-line">5.1. Metacharacters - line separators</a></h2>
<p>Some expressions help to detect line separation.</p>
<table>
<tr class="rowcategorytitle"><th colspan="2">Line separators</th></tr>
<tr class="rowsubtitle"><th class="namecolumn">Expression</th><th class="categorydesccolumn">Result</th></tr>
<tr>
<td class="varcell"><div class="firstcolumnaleft">^</div></td>
<td class="hintcell">start of line</td>
</tr>
<tr>
<td class="varcell"><div class="firstcolumnaleft">$</div></td>
<td class="hintcell">end of line</td>
</tr>
<tr>
<td class="varcell"><div class="firstcolumnaleft">\A</div></td>
<td class="hintcell">start of text</td>
</tr>
<tr>
<td class="varcell"><div class="firstcolumnaleft">\Z</div></td>
<td class="hintcell">end of text</td>
</tr>
<tr>
<td class="varcell"><div class="firstcolumnaleft">.</div></td>
<td class="hintcell">any character in line</td>
</tr>
</table>
<p>Here are some examples:</p>
<table>
<tr class="rowcategorytitle"><th colspan="2">Example with line separators</th></tr>
<tr class="rowsubtitle"><th class="namecolumn">Expression</th><th class="categorydesccolumn">Result</th></tr>
<tr>
<td class="varcell"><div class="firstcolumnaleft">^foobar</div></td>
<td class="hintcell">matches string <code>foobar</code> only if it's at the beginning of line</td>
</tr>
<tr>
<td class="varcell"><div class="firstcolumnaleft">foobar$</div></td>
<td class="hintcell">matches string <code>foobar</code> only if it's at the end of line</td>
</tr>
<tr>
<td class="varcell"><div class="firstcolumnaleft">^foobar$</div></td>
<td class="hintcell">matches string <code>foobar</code> only if it's the only string in line</td>
</tr>
<tr>
<td class="varcell"><div class="firstcolumnaleft">foob.r</div></td>
<td class="hintcell">matches strings like <code>foobar</code>, <code>foobbr</code>, <code>foob1r</code> and so on</td>
</tr>
</table>
<p>The <code>^</code> metacharacter by default is only guaranteed to match at the beginning of the input string/text, the <code>$</code> metacharacter only at the end. Embedded line separators will not be matched by <code>^</code> or <code>$</code>.</p>
<p>You may, however, wish to treat a string as a multi-line buffer, such that the <code>^</code> will match after any line separator within the string, and <code>$</code> will match before any line separator. You can do this by switching On the <a href="#modifiers">modifier m</a>.</p>
<p>The <code>\A</code> and <code>\Z</code> are just like <code>^</code> and <code>$</code>, except that they won't match multiple times when the <a href="#modifiers">modifier m</a> is used, while <code>^</code> and <code>$</code> will match at every internal line separator.</p>
<p>The <code>.</code> metacharacter by default matches any character, but if you switch Off the <a href="#modifiers">modifier s</a>, then <code>.</code> won't match embedded line separators.</p>
<p>TRegExpr works with line separators as recommended at Unicode technical standards (<a href="https://www.unicode.org/reports/tr18/" target="_blank">Technical Standard #18</a>):</p>
<ul>
<li><p><code>^</code> is at the beginning of a input string, and, if <a href="#modifiers">modifier m</a> is On, also immediately following any occurrence of <code>\x0D\x0A</code> or <code>\x0A</code> or <code>\x0D</code> (with Unicode support: also <code>\x2028</code> or <code>\x2029</code> or <code>\x0B</code> or <code>\x0C</code> or <code>\x85</code>). Note that there is no empty line within the sequence <code>\x0D\x0A</code>.</p></li>
<li><p><code>$</code> is at the end of a input string, and, if <a href="#modifiers">modifier m</a> is On, also immediately preceding any occurrence of <code>\x0D\x0A</code> or <code>\x0A</code> or <code>\x0D</code> (with Unicode support: also <code>\x2028</code> or <code>\x2029</code> or <code>\x0B</code> or <code>\x0C</code> or <code>\x85</code>). Note that there is no empty line within the sequence <code>\x0D\x0A</code>.</p></li>
<li><p><code>.</code> matches any character, but if you switch Off <a href="#modifiers">modifier s</a> then <code>.</code> doesn't match <code>\x0D\x0A</code> and <code>\x0A</code> and <code>\x0D</code> (with Unicode support: also <code>\x2028</code> and <code>\x2029</code> and <code>\x0B</code> and <code>\x0C</code> and <code>\x85</code>).</p></li>
</ul>
<p>Note that <code>^.*$</code> (an empty line pattern) does not match the empty string within the sequence <code>\x0D\x0A</code>, but matches the empty string within the sequence <code>\x0A\x0D</code>.</p>
<p class="navback"><a href="#topofpage">(back to top)</a></p>
</div>
<div>
<h2><a name="meta-std">5.2. Metacharacters - predefined classes</a></h2>
<p>Some expressions help to detect group of characters.</p>
<table>
<tr class="rowcategorytitle"><th colspan="2">Predefined classes</th></tr>
<tr class="rowsubtitle"><th class="namecolumn">Expression</th><th class="categorydesccolumn">Result</th></tr>
<tr>
<td class="varcell"><div class="firstcolumnaleft">\w</div></td>
<td class="hintcell">an alphanumeric character (including <code>_</code>), i.e. <code>[0-9A-Za-z_]</code></td>
</tr>
<tr>
<td class="varcell"><div class="firstcolumnaleft">\W</div></td>
<td class="hintcell">a nonalphanumeric</td>
</tr>
<tr>
<td class="varcell"><div class="firstcolumnaleft">\d</div></td>
<td class="hintcell">a numeric character</td>
</tr>
<tr>
<td class="varcell"><div class="firstcolumnaleft">\D</div></td>
<td class="hintcell">a non-numeric</td>
</tr>
<tr>
<td class="varcell"><div class="firstcolumnaleft">\s</div></td>
<td class="hintcell">any space (same as <code>[ \t\n\r\f]</code>)</td>
</tr>
<tr>
<td class="varcell"><div class="firstcolumnaleft">\S</div></td>
<td class="hintcell">a non space</td>
</tr>
</table>
<p>You may use <code>\w</code>, <code>\d</code> and <code>\s</code> within custom <span class="bold">character classes</span>.</p>
<p>Here are some examples:</p>
<table>
<tr class="rowcategorytitle"><th colspan="2">Example of predefined classes</th></tr>
<tr class="rowsubtitle"><th class="namecolumn">Expression</th><th class="categorydesccolumn">Result</th></tr>
<tr>
<td class="varcell"><div class="firstcolumnaleft">foob\dr</div></td>
<td class="hintcell">matches strings like <code>foob1r</code>, <code>foob6r</code> and so on but not <code>foobar</code>, <code>foobbr</code> and so on</td>
</tr>
<tr>
<td class="varcell"><div class="firstcolumnaleft">foob[\w\s]r</div></td>
<td class="hintcell">matches strings like <code>foobar</code>, <code>foob r</code>, <code>foobbr</code> and so on but not <code>foob=r</code> and so on</td>
</tr>
</table>
<p class="navback"><a href="#topofpage">(back to top)</a></p>
</div>
<div>
<h2><a name="meta-words">5.3. Metacharacters - word boundaries</a></h2>
<p>A word boundary (<code>\b</code>) is a spot between two characters that has a <code>\w</code> on one side of it and a <code>\W</code> on the other side of it (in either order), counting the imaginary characters off the beginning and end of the string as matching a <code>\W</code>.</p>
<table>
<tr class="rowcategorytitle"><th colspan="2">Word boundaries</th></tr>
<tr class="rowsubtitle"><th class="namecolumn">Expression</th><th class="categorydesccolumn">Result</th></tr>
<tr>
<td class="varcell"><div class="firstcolumnaleft">\b</div></td>
<td class="hintcell">match a word boundary</td>
</tr>
<tr>
<td class="varcell"><div class="firstcolumnaleft">\B</div></td>
<td class="hintcell">match a non-(word boundary)</td>
</tr>
</table>
<p class="navback"><a href="#topofpage">(back to top)</a></p>
</div>
<div>
<h2><a name="meta-dup">5.4. Metacharacters - iterators</a></h2>
<p>Any item of a regular expression may be followed by another type of metacharacters – iterators.</p>
<p>Using these metacharacters you can specify the number of occurrences of the previous character, metacharacter or subexpression.</p>
<table>
<tr class="rowcategorytitle"><th colspan="2">Iterators</th></tr>
<tr class="rowsubtitle"><th class="namecolumn">Expression</th><th class="categorydesccolumn">Result</th></tr>
<tr>
<td class="varcell"><div class="firstcolumnaleft">*</div></td>
<td class="hintcell">zero or more ("greedy"), similar to <code>{0,}</code></td>
</tr>
<tr>
<td class="varcell"><div class="firstcolumnaleft">+</div></td>
<td class="hintcell">one or more ("greedy"), similar to <code>{1,}</code></td>
</tr>
<tr>
<td class="varcell"><div class="firstcolumnaleft">?</div></td>
<td class="hintcell">zero or one ("greedy"), similar to <code>{0,1}</code></td>
</tr>
<tr>
<td class="varcell"><div class="firstcolumnaleft">{n}</div></td>
<td class="hintcell">exactly <code>n</code> times ("greedy")</td>
</tr>
<tr>
<td class="varcell"><div class="firstcolumnaleft">{n,}</div></td>
<td class="hintcell">at least <code>n</code> times ("greedy")</td>
</tr>
<tr>
<td class="varcell"><div class="firstcolumnaleft">{n,m}</div></td>
<td class="hintcell">at least <code>n</code> but not more than <code>m</code> times ("greedy")</td>
</tr>
<tr>
<td class="varcell"><div class="firstcolumnaleft">*?</div></td>
<td class="hintcell">zero or more ("non-greedy"), similar to <code>{0,}?</code></td>
</tr>
<tr>
<td class="varcell"><div class="firstcolumnaleft">+?</div></td>
<td class="hintcell">one or more ("non-greedy"), similar to <code>{1,}?</code></td>
</tr>
<tr>
<td class="varcell"><div class="firstcolumnaleft">??</div></td>
<td class="hintcell">zero or one ("non-greedy"), similar to <code>{0,1}?</code></td>
</tr>
<tr>
<td class="varcell"><div class="firstcolumnaleft">{n}?</div></td>
<td class="hintcell">exactly <code>n</code> times ("non-greedy")</td>
</tr>
<tr>
<td class="varcell"><div class="firstcolumnaleft">{n,}?</div></td>
<td class="hintcell">at least <code>n</code> times ("non-greedy")</td>
</tr>
<tr>
<td class="varcell"><div class="firstcolumnaleft">{n,m}?</div></td>
<td class="hintcell">at least <code>n</code> but not more than <code>m</code> times ("non-greedy") </td>
</tr>
</table>
<p>So, digits in curly brackets of the form <code>{n,m}</code>, specify the <span class="bold">minimum</span> number of times to match the item <code>n</code> and the <span class="bold">maximum</span> <code>m</code>.</p>
<p>The form <code>{n}</code> is equivalent to <code>{n,n}</code> and matches exactly <code>n</code> times.</p>
<p>The form <code>{n,}</code> matches <code>n</code> or more times.</p>
<p>There is no limit to the size of <code>n</code> or <code>m</code>, but large numbers will slow down execution and chew up more memory.</p>
<p>If a curly bracket occurs in any other context, it is treated as a regular character.</p>
<p>Here are some examples:</p>
<table>
<tr class="rowcategorytitle"><th colspan="2">Example of iterators</th></tr>
<tr class="rowsubtitle"><th class="namecolumn">Expression</th><th class="categorydesccolumn">Result</th></tr>
<tr>
<td class="varcell"><div class="firstcolumnaleft">foob.*r</div></td>
<td class="hintcell">matches strings like <code>foobar</code>, <code>foobalkjdflkj9r</code> and <code>foobr</code></td>
</tr>
<tr>
<td class="varcell"><div class="firstcolumnaleft">foob.+r</div></td>
<td class="hintcell">matches strings like <code>foobar</code>, <code>foobalkjdflkj9r</code> but not <code>foobr</code></td>
</tr>
<tr>
<td class="varcell"><div class="firstcolumnaleft">foob.?r</div></td>
<td class="hintcell">matches strings like <code>foobar</code>, <code>foobbr</code> and <code>foobr</code> but not <code>foobalkj9r</code></td>
</tr>
<tr>
<td class="varcell"><div class="firstcolumnaleft">fooba{2}r</div></td>
<td class="hintcell">matches the string <code>foobaar</code></td>
</tr>
<tr>
<td class="varcell"><div class="firstcolumnaleft">fooba{2,}r</div></td>
<td class="hintcell">matches strings like <code>foobaar</code>, <code>foobaaar</code>, <code>foobaaaar</code> etc.</td>
</tr>
<tr>
<td class="varcell"><div class="firstcolumnaleft">fooba{2,3}r</div></td>
<td class="hintcell">matches strings like <code>foobaar</code>, or <code>foobaaar</code> but not <code>foobaaaar</code></td>
</tr>
</table>
<p>A little explanation about "greediness".</p>
<p>"Greedy" takes as many as possible, "non-greedy" takes as few as possible.</p>
<p>For example, <code>b+</code> and <code>b*</code> applied to string <code>abbbbc</code> return <code>bbbb</code>, <code>b+?</code> returns <code>b</code>, <code>b*?</code> returns empty string, <code>b{2,3}?</code> returns <code>bb</code>, <code>b{2,3}</code> returns <code>bbb</code>.</p>
<p>You can switch all iterators into "non-greedy" mode (see the <a href="#modifiers">modifier g</a>).</p>
<p class="navback"><a href="#topofpage">(back to top)</a></p>
</div>
<div>
<h2><a name="meta-vars">5.5. Metacharacters - alternatives</a></h2>
<p>You can specify a series of <span class="bold">alternatives</span> for a pattern using <code>|</code> to separate them, so that <code>fee|fie|foe</code> will match any of <code>fee</code>, <code>fie</code>, or <code>foe</code> in the target string (as would <code>f(e|i|o)e</code>).</p>
<p>The first alternative includes everything from the last pattern delimiter (<code>(</code>, <code>[</code>, or the beginning of the pattern) up to the first <code>|</code>, and the last alternative contains everything from the last <code>|</code> to the next pattern delimiter.</p>
<p>For this reason, it's common practice to include alternatives in parentheses, to minimize confusion about where they start and end.</p>
<p>Alternatives are tried from left to right, so the first alternative found for which the entire expression matches, is the one that is chosen.</p>
<p>This means that alternatives are not necessarily greedy.</p>
<p>For example: when matching <code>foo|foot</code> against <code>barefoot</code>, only the <code>foo</code> part will match, as that is the first alternative tried, and it successfully matches the target string. (This might not seem important, but it is important when you are capturing matched text using parentheses.)</p>
<p>Also remember that <code>|</code> is interpreted as a literal within square brackets, so if you write <code>[fee|fie|foe]</code> you're really only matching <code>[feio|]</code>.</p>
<p>Example:</p>
<table>
<tr class="rowcategorytitle"><th colspan="2">Example of alternatives</th></tr>
<tr class="rowsubtitle"><th class="namecolumn">Expression</th><th class="categorydesccolumn">Result</th></tr>
<tr>
<td class="varcell"><div class="firstcolumnaleft">foo(bar|foo)</div></td>
<td class="hintcell">matches strings <code>foobar</code> or <code>foofoo</code></td>
</tr>
</table>
<p class="navback"><a href="#topofpage">(back to top)</a></p>
</div>
<div>
<h2><a name="meta-sub">5.6. Metacharacters - subexpressions</a></h2>
<p>The bracketing construct <code>( ... )</code> may also be used for define regular expression subexpressions.</p>
<p>After search you can call any subexpression, also you can use subexpressions as masks.</p>
<p>Subexpressions are numbered based on the left to right order of their opening parenthesis.</p>
<p>First subexpression has number 1, up to 90 subexpressions are supported (whole regular expression match has number 0 – you can substitute it as <code>$0</code> or <code>$&</code>).</p>
<p>Here are some examples:</p>
<table>
<tr class="rowcategorytitle"><th colspan="2">Subexpressions</th></tr>
<tr class="rowsubtitle"><th class="namecolumn">Expression</th><th class="categorydesccolumn">Result</th></tr>
<tr>
<td class="varcell"><div class="firstcolumnaleft">(foobar){8,10}</div></td>
<td class="hintcell">matches strings which contain 8, 9 or 10 instances of the <code>foobar</code></td>
</tr>
<tr>
<td class="varcell"><div class="firstcolumnaleft">foob([0-9]|a+)r</div></td>
<td class="hintcell">matches <code>foob0r</code>, <code>foob1r</code> , <code>foobar</code>, <code>foobaar</code>, <code>foobaar</code> etc.</td>
</tr>
</table>
<br>
<p><span class="bold">Notes about "Replace with" templates:</span></p>
<ul>
<li>If you want to place raw <code>$</code> or <code>\</code> into template, then use prefix <code>\</code>.<br>
Example: <code>1\$ is $2\\rub\\</code> will return <code>1$ is <subexpr2>\rub\</code>.</li>
<li>If you want to place raw digit after <code>$n</code>, then you must delimit <code>n</code> with curly braces <code>{}</code>.<br>
For example, <code>a$12bc</code> will return <code>a<subexpr12>bc</code>, but <code>a${1}2bc</code> will return <code>a<subexpr1>2bc</code>.</li>
</ul>
<p>Example:</p>
<p>Let's invert date <code>21.01.2018</code> > <code>2018.01.21</code>:<br>
search for: <code>(\d{2})\.(\d{2})\.(\d{4})</code><br>
replace with: <code>$3.$2.$1</code></p>
<p class="navback"><a href="#topofpage">(back to top)</a></p>
</div>
<div>
<h2><a name="meta-back">5.7. Metacharacters - backreferences</a></h2>
<p>Metacharacters <code>\1</code> through <code>\9</code> are interpreted as backreferences. <code>\n</code> matches previously matched subexpression <code>n</code>.</p>
<p>Here are some examples:</p>
<table>
<tr class="rowcategorytitle"><th colspan="2">Examples of backreferences</th></tr>
<tr class="rowsubtitle"><th class="namecolumn">Expression</th><th class="categorydesccolumn">Result</th></tr>
<tr>
<td class="varcell"><div class="firstcolumnaleft">(.)\1+</div></td>
<td class="hintcell">matches <code>aaaa</code> and <code>cc</code></td>
</tr>
<tr>
<td class="varcell"><div class="firstcolumnaleft">(.+)\1+</div></td>
<td class="hintcell">also match <code>abab</code> and <code>123123</code></td>
</tr>
<tr>
<td class="varcell"><div class="firstcolumnaleft">(['"]?)(\d+)\1</div></td>
<td class="hintcell">matches <code>"13"</code> (in double quotes), or <code>'4'</code> (in single quotes) or <code>77</code> (without quotes) etc</td>
</tr>
</table>
<p class="navback"><a href="#topofpage">(back to top)</a></p>
</div>
<div>
<h2><a name="look">6. Assertions (lookahead and lookbehind assertions)</a></h2>
<p>Positive lookahead assertion: <code>foo(?=bar)</code> matches <code>foo</code> only before <code>bar</code>, and <code>bar</code> is excluded from the match.</p>
<p>Negative lookahead assertion: <code>foo(?!bar)</code> matches <code>foo</code> only if it’s not followed by <code>bar</code>.</p>
<p>Positive lookbehind assertion: <code>(?<=foo)bar</code> matches <code>bar</code> only after <code>foo</code>, and <code>foo</code> is excluded from the match.</p>
<p>Negative lookbehind assertion: <code>(?<!foo)bar</code> matches <code>bar</code> only if it’s not prefixed with <code>foo</code>.</p>
<p>Limitations:</p>
<ul>
<li><p>Brackets for lookahead must be at the very ending of expression, and brackets for lookbehind must be at the very beginning. So assertions between choices <code>|</code>, or inside groups, are not supported.</p></li>
<li><p>For lookbehind <code>(?<!foo)bar</code>, regular expression <code>foo</code> must be of fixed length, ie contains only operations of fixed length matches. Quantifiers are not allowed, except braces with the repeated numbers <code>{n}</code> or <code>{n,n}</code>. Char-classes are allowed here, dot is allowed, <code>\b</code> and <code>\B</code> are allowed. Groups and choices are not allowed.</p></li>
<li><p>For other 3 assertion kinds, expression in brackets can be of any complexity.</p></li>
</ul>
<p class="navback"><a href="#topofpage">(back to top)</a></p>
</div>
<div>
<h2><a name="noncapt">7. Non-capturing groups</a></h2>
<p>Syntax: <code>(?:expr)</code>.</p>
<p>Such groups do not have the "index" and are invisible for backreferences. Non-capturing groups are used when you want to group a subexpression, but you do not want to save it as a matched/captured portion of the string. The use of non-capturing groups allows to speed up the work of a regular expression.</p>
<table>
<tr class="rowcategorytitle"><th colspan="2">Non-capturing groups</th></tr>
<tr class="rowsubtitle"><th class="namecolumn">Expression</th><th class="categorydesccolumn">Result</th></tr>
<tr>
<td class="varcell"><div class="firstcolumnaleft">(https?|ftp)://([^/\r\n]+)</div></td>
<td class="hintcell">in <code>https://doublecmd.sourceforge.io</code> matches <code>https</code> and <code>doublecmd.sourceforge.io</code></td>
</tr>
<tr>
<td class="varcell"><div class="firstcolumnaleft">(?:https?|ftp)://([^/\r\n]+)</div></td>
<td class="hintcell">in <code>https://doublecmd.sourceforge.io</code> matches only <code>doublecmd.sourceforge.io</code></td>
</tr>
</table>
<p class="navback"><a href="#topofpage">(back to top)</a></p>
</div>
<div>
<h2><a name="agroups">8. Atomic groups</a></h2>
<p>Syntax: <code>(?>expr|expr|...)</code>.</p>
<p>Atomic groups are special case of non-capturing groups: this grouping disables backtracking for a parenthesized group if part of the pattern has already been found. Atomic group works faster, useful when optimizing groups with many different expressions.</p>
<p>For example, <code>a(bc|b)c</code> matches <code>abcc</code> and <code>abc</code>, <code>a(?>bc|b)c</code> matches <code>abcc</code> but not <code>abc</code> because the engine is forbidden from backtracking and try with setting the group as <code>b</code>.</p>
<p class="navback"><a href="#topofpage">(back to top)</a></p>
</div>
<div>
<h2><a name="unicode">9. Unicode categories</a></h2>
<p>Unicode standard has names for character categories. These are 2-letter strings. For example <code>Lu</code> is uppercase letters, <code>Ll</code> is lowercase letters. And 1-letter bigger category <code>L</code> is all letters.</p>
<table>
<tr class="rowcategorytitle"><th colspan="2">Unicode categories</th></tr>
<tr class="rowsubtitle"><th class="namecolumn">Category</th><th class="categorydesccolumn">Description</th></tr>
<tr><td class="varcell"><div class="firstcolumnaleft">L</div></td><td class="hintcell">Letter</td></tr>
<tr><td class="varcell"><div class="firstcolumnaleft">Lu</div></td><td class="hintcell">Uppercase Letter</td></tr>
<tr><td class="varcell"><div class="firstcolumnaleft">Ll</div></td><td class="hintcell">Lowercase Letter</td></tr>
<tr><td class="varcell"><div class="firstcolumnaleft">Lt</div></td><td class="hintcell">Titlecase Letter</td></tr>
<tr><td class="varcell"><div class="firstcolumnaleft">Lm</div></td><td class="hintcell">Modifier Letter</td></tr>
<tr><td class="varcell"><div class="firstcolumnaleft">Lo</div></td><td class="hintcell">Other Letter</td></tr>
<tr><td colspan="2"></td></tr>
<tr><td class="varcell"><div class="firstcolumnaleft">M</div></td><td class="hintcell">Mark</td></tr>
<tr><td class="varcell"><div class="firstcolumnaleft">Mn</div></td><td class="hintcell">Non-Spacing Mark</td></tr>
<tr><td class="varcell"><div class="firstcolumnaleft">Mc</div></td><td class="hintcell">Spacing Combining Mark</td></tr>
<tr><td class="varcell"><div class="firstcolumnaleft">Me</div></td><td class="hintcell">Enclosing Mark</td></tr>
<tr><td colspan="2"></td></tr>
<tr><td class="varcell"><div class="firstcolumnaleft">N</div></td><td class="hintcell">Number</td></tr>
<tr><td class="varcell"><div class="firstcolumnaleft">Nd</div></td><td class="hintcell">Decimal Digit Number</td></tr>
<tr><td class="varcell"><div class="firstcolumnaleft">Nl</div></td><td class="hintcell">Letter Number</td></tr>
<tr><td class="varcell"><div class="firstcolumnaleft">No</div></td><td class="hintcell">Other Number</td></tr>
<tr><td colspan="2"></td></tr>
<tr><td class="varcell"><div class="firstcolumnaleft">P</div></td><td class="hintcell">Punctuation</td></tr>
<tr><td class="varcell"><div class="firstcolumnaleft">Pc</div></td><td class="hintcell">Connector Punctuation</td></tr>
<tr><td class="varcell"><div class="firstcolumnaleft">Pd</div></td><td class="hintcell">Dash Punctuation</td></tr>
<tr><td class="varcell"><div class="firstcolumnaleft">Ps</div></td><td class="hintcell">Open Punctuation</td></tr>
<tr><td class="varcell"><div class="firstcolumnaleft">Pe</div></td><td class="hintcell">Close Punctuation</td></tr>
<tr><td class="varcell"><div class="firstcolumnaleft">Pi</div></td><td class="hintcell">Initial Punctuation</td></tr>
<tr><td class="varcell"><div class="firstcolumnaleft">Pf</div></td><td class="hintcell">Final Punctuation</td></tr>
<tr><td class="varcell"><div class="firstcolumnaleft">Po</div></td><td class="hintcell">Other Punctuation</td></tr>
<tr><td colspan="2"></td></tr>
<tr><td class="varcell"><div class="firstcolumnaleft">S</div></td><td class="hintcell">Symbol</td></tr>
<tr><td class="varcell"><div class="firstcolumnaleft">Sm</div></td><td class="hintcell">Math Symbol</td></tr>
<tr><td class="varcell"><div class="firstcolumnaleft">Sc</div></td><td class="hintcell">Currency Symbol</td></tr>
<tr><td class="varcell"><div class="firstcolumnaleft">Sk</div></td><td class="hintcell">Modifier Symbol</td></tr>
<tr><td class="varcell"><div class="firstcolumnaleft">So</div></td><td class="hintcell">Other Symbol</td></tr>
<tr><td colspan="2"></td></tr>
<tr><td class="varcell"><div class="firstcolumnaleft">Z</div></td><td class="hintcell">Separator</td></tr>
<tr><td class="varcell"><div class="firstcolumnaleft">Zs</div></td><td class="hintcell">Space Separator</td></tr>
<tr><td class="varcell"><div class="firstcolumnaleft">Zl</div></td><td class="hintcell">Line Separator</td></tr>
<tr><td class="varcell"><div class="firstcolumnaleft">Zp</div></td><td class="hintcell">Paragraph Separator</td></tr>
<tr><td colspan="2"></td></tr>
<tr><td class="varcell"><div class="firstcolumnaleft">C</div></td><td class="hintcell">Other</td></tr>
<tr><td class="varcell"><div class="firstcolumnaleft">Cc</div></td><td class="hintcell">Control</td></tr>
<tr><td class="varcell"><div class="firstcolumnaleft">Cf</div></td><td class="hintcell">Format</td></tr>
<tr><td class="varcell"><div class="firstcolumnaleft">Cs</div></td><td class="hintcell">Surrogate</td></tr>
<tr><td class="varcell"><div class="firstcolumnaleft">Co</div></td><td class="hintcell">Private Use</td></tr>
<tr><td class="varcell"><div class="firstcolumnaleft">Cn</div></td><td class="hintcell">Unassigned</td></tr>
</table>
<p>Meta-character <code>\p</code> denotes one Unicode char of specified category. Syntaxes: <code>\pL</code> and <code>\p{L}</code> for 1-letter name, <code>\p{Lu}</code> for 2-letter names.</p>
<p>Meta-character <code>\P</code> is inverted, it denotes one Unicode char not in the specified category.</p>
<p>These meta-characters are supported withing character classes too.</p>
<p class="navback"><a href="#topofpage">(back to top)</a></p>
</div>
<div>
<h2><a name="modifiers">10. Modifiers</a></h2>
<p>Syntax for one modifier: <code>(?i)</code> to turn on, and <code>(?-i)</code> to turn off. Many modifiers are allowed like this: <code>(?msgxr-imsgxr)</code>.</p>
<p>Modifiers are for changing behaviour of regular expressions. Modifier affects only that part of regular expression that follows <code>(?imsgxr-imsgxr)</code> operator.</p>
<p>Any of these modifiers may be embedded within the regular expression itself. If modifier is inlined into subexpression, then it effects only into this subexpression.</p>
<dl>
<dt><tt>i</tt></dt>
<dd>Do case-insensitive pattern matching (using installed in your system locale settings). Disabled by default.</dd>
<dt><tt>m</tt></dt>
<dd>Treat string as multiple lines. That is, change <code>^</code> and <code>$</code> from matching at only the very start or end of the string to the start or end of any line anywhere within the string, see also <a href="#meta-line">Line separators</a>. Disabled by default.</dd>
<dt><tt>s</tt></dt>
<dd>Treat string as single line. That is, change <code>.</code> to match any character whatsoever, even a line separator (see also <a href="#meta-line">Line separators</a>), which it normally would not match. Enabled by default.</dd>
<dt><tt>g</tt></dt>
<dd>Non standard modifier. Switching it Off you'll switch all following operators into non-greedy mode (by default this modifier is On). So, if modifier <code>g</code> is Off then <code>+</code> works as <code>+?</code>, <code>*</code> as <code>*?</code> and so on.</dd>
<dt><tt>x</tt></dt>
<dd>Extend your pattern's legibility by permitting whitespace and comments (see explanation below). Disabled by default.</dd>
<dt><tt>r</tt></dt>
<dd>Non-standard modifier. If is set then range <code>а-я</code> additional include Russian letter <code>ё</code>, <code>А-Я</code> additional include <code>Ё</code>, and <code>а-Я</code> include all Russian alphabet. Enabled by default.</dd>
<dt><tt>#</tt></dt>
<dd><tt>(?#text)</tt>: A comment, the <code>text</code> is ignored. Note that TRegExpr closes the comment as soon as it sees a <code>)</code>, so there is no way to put a literal <code>)</code> in the comment.</dd>
</dl>
<p>Here are some examples:</p>
<table>
<tr class="rowcategorytitle"><th colspan="2">Examples of Perl extensions</th></tr>
<tr class="rowsubtitle"><th class="namecolumn">Expression</th><th class="categorydesccolumn">Result</th></tr>
<tr>
<td class="varcell"><div class="firstcolumnaleft">(?i)Saint-Petersburg</div></td>
<td class="hintcell">matches <code>Saint-petersburg</code> and <code>Saint-Petersburg</code></td>
</tr>
<tr>
<td class="varcell"><div class="firstcolumnaleft">(?i)Saint-(?-i)Petersburg</div></td>
<td class="hintcell">matches <code>Saint-Petersburg</code> but not <code>Saint-petersburg</code></td>
</tr>
<tr>
<td class="varcell"><div class="firstcolumnaleft">(?i)(Saint-)?Petersburg</div></td>
<td class="hintcell">matches <code>Saint-petersburg</code> and <code>saint-petersburg</code></td>
</tr>
<tr>
<td class="varcell"><div class="firstcolumnaleft">((?i)Saint-)?Petersburg</div></td>
<td class="hintcell">matches <code>saint-Petersburg</code> but not <code>saint-petersburg</code></td>
</tr>
</table>
<br>
<p>The modifier <code>x</code> itself needs a little more explanation.</p>
<p>It tells to ignore whitespace that is neither backslashed nor within a character class.</p>
<p>You can use this to break up your regular expression into (slightly) more readable parts.</p>
<p>The <code>#</code> character is also treated as a metacharacter introducing a comment, for example:</p>
<pre class="code">
(
(abc) # comment 1
| # You can use spaces to format r.e. - TRegExpr ignores it
(efg) # comment 2
)</pre>
<p>This also means that if you want real whitespace or <code>#</code> characters in the pattern (outside a character class, where they are unaffected by <code>x</code>), that you'll either have to escape them or encode them using octal or hex escapes.</p>
<p>Taken together, these features go a long way towards making regular expressions text more readable.</p>
<p class="navback"><a href="#topofpage">(back to top)</a></p>
<br>
</div>
</div>
<div class="footer"><div class="nav"><a title="Index" href="index.html">Index</a> | <a title="Previous page" href="cmds.html">Previous</a> | <a title="Next page" href="lua.html">Next</a></div></div>
<div class="checker">
<a href="https://validator.w3.org/check?uri=referer" target="_blank"><img src="https://www.w3.org/Icons/valid-html40" alt="Valid HTML 4.0 Transitional" height="31" width="88"></a>
<a href="https://jigsaw.w3.org/css-validator/check/referer" target="_blank"><img style="border:0;width:88px;height:31px" src="https://jigsaw.w3.org/css-validator/images/vcss" alt="CSS Valid!"></a>
</div>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
</body>
</html>
|