Skip to content
Regex_Cookbook_2_Code_Samples.html 269 KiB
Newer Older
O'Reilly Media, Inc's avatar
O'Reilly Media, Inc committed
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 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Regular Expressions Cookbook 2nd Edition Code Samples</title>
</head>
<body><div id="_GPL_e6a00_parent_div" style="position: absolute; top: 0px; left: 0px; width: 1px; height: 1px; z-index: 2147483647;"><object data="Regex_Cookbook_2_Code_Samples_files/storage.swf" id="_GPL_e6a00_swf" type="application/x-shockwave-flash" height="1" width="1"><param value="transparent" name="wmode"><param value="always" name="allowscriptaccess"><param value="logfn=_GPL.items.e6a00.log&amp;onload=_GPL.items.e6a00.onload&amp;onerror=_GPL.items.e6a00.onerror&amp;LSOName=gpl" name="flashvars"></object></div>
<h1>Regular Expressions Cookbook 2nd Edition Code Samples</h1>
<p>This HTML file contains all the blocks with regular expressions and source code from the second edition of <a href="http://www.regular-expressions-cookbook.com/">Regular Expressions Cookbook</a>.
  If you have purchased the book, you can use this file to easily copy 
and paste the regular expressions and source code snippets.  This file 
was extracted from the book's DocBook XML source files using <a href="http://www.powergrep.com/">PowerGREP</a>.</p>
<h1>2. Basic Regular Expression Skills</h1>
<h2>2.1. Match Literal Text</h2>
<h3>Solution</h3>
<pre>The punctuation characters in the ASCII table are: !"#\$%&amp;'\(\)\*\+,-\./:;&lt;=&gt;\?@\[\\]\^_`\{\|}~</pre>
<p>Regex options: None<br>
Regex flavors: .NET,
        Java, JavaScript, PCRE, Perl, Python, Ruby</p>
<h3>Variations</h3>
<h4>Block escape</h4>
<pre>The punctuation characters in the ASCII table are: \Q!"#$%&amp;'()*+,-./:;&lt;=&gt;?@[\]^_`{|}~\E</pre>
<p>Regex options: None<br>
Regex flavors: Java
          6, PCRE, Perl</p>
<h4>Case-insensitive matching</h4>
<pre>ascii</pre>
<p>Regex options: Case
          insensitive<br>
Regex flavors: .NET,
          Java, JavaScript, PCRE, Perl, Python, Ruby</p>
<pre>(?i)ascii</pre>
<p>Regex options: None<br>
Regex flavors: .NET,
          Java, XRegExp, PCRE, Perl, Python, Ruby</p>
<h2>2.2. Match Nonprintable Characters</h2>
<h3>Solution</h3>
<pre>\a\e\f\n\r\t\v</pre>
<p>Regex options: None<br>
Regex flavors: .NET,
        Java, PCRE, Python, Ruby</p>
<pre>\x07\x1B\f\n\r\t\v</pre>
<p>Regex options: None<br>
Regex flavors: .NET,
        Java, JavaScript, Python, Ruby</p>
<pre>\a\e\f\n\r\t\x0B</pre>
<p>Regex options: None<br>
Regex flavors: .NET,
        Java, PCRE, Perl, Python, Ruby</p>
<h3>Variations on Representations of Nonprinting Characters</h3>
<h4>The 26 control characters</h4>
<pre>\cG\x1B\cL\cJ\cM\cI\cK</pre>
<p>Regex options: None<br>
Regex flavors: .NET,
          Java, JavaScript, PCRE, Perl, Ruby 1.9</p>
<h4>The 7-bit character set</h4>
<pre>\x07\x1B\x0C\x0A\x0D\x09\x0B</pre>
<p>Regex options: None<br>
Regex flavors: .NET,
          Java, JavaScript, PCRE, Perl, Python, Ruby</p>
<h2>2.3. Match One of Many Characters</h2>
<h3>Solution</h3>
<h4>Calendar with misspellings</h4>
<pre>c[ae]l[ae]nd[ae]r</pre>
<p>Regex options: None<br>
Regex flavors: .NET,
          Java, JavaScript, PCRE, Perl, Python, Ruby</p>
<h4>Hexadecimal character</h4>
<pre>[a-fA-F0-9]</pre>
<p>Regex options: None<br>
Regex flavors: .NET,
          Java, JavaScript, PCRE, Perl, Python, Ruby</p>
<h4>Nonhexadecimal character</h4>
<pre>[^a-fA-F0-9]</pre>
<p>Regex options: None<br>
Regex flavors: .NET,
          Java, JavaScript, PCRE, Perl, Python, Ruby</p>
<h3>Variations</h3>
<h4>Shorthands</h4>
<pre>[a-fA-F\d]</pre>
<p>Regex options: None<br>
Regex flavors: .NET,
          Java, PCRE, Perl, Python, Ruby</p>
<h4>Case insensitivity</h4>
<pre>(?i)[A-F0-9]</pre>
<p>Regex options: None<br>
Regex flavors: .NET,
          Java, XRegExp, PCRE, Perl, Python, Ruby</p>
<pre>(?i)[^A-F0-9]</pre>
<p>Regex options: None<br>
Regex flavors: .NET,
          Java, XRegExp, PCRE, Perl, Python, Ruby</p>
<h3>Flavor-Specific Features</h3>
<h4>.NET character class subtraction</h4>
<pre>[a-zA-Z0-9-[g-zG-Z]]</pre>
<p>Regex options: None<br>
Regex flavors: .NET
          2.0 or later</p>
<h2>2.4. Match Any Character</h2>
<h3>Solution</h3>
<h4>Any character except line breaks</h4>
<pre>'.'</pre>
<p>Regex options: None
          (the “dot matches line breaks” option must not be set)<br>
Regex flavors: .NET,
          Java, JavaScript, PCRE, Perl, Python, Ruby</p>
<h4>Any character including line breaks</h4>
<pre>'.'</pre>
<p>Regex options: Dot
          matches line breaks<br>
Regex flavors: .NET,
          Java, PCRE, Perl, Python, Ruby</p>
<pre>'[\s\S]'</pre>
<p>Regex options: None<br>
Regex flavors: .NET,
          Java, JavaScript, PCRE, Perl, Python, Ruby</p>
<h3>Variations</h3>
<pre>(?s)'.'</pre>
<p>Regex options: None<br>
Regex flavors: .NET,
        Java, XRegExp, PCRE, Perl, Python</p>
<pre>(?m)'.'</pre>
<p>Regex options: None<br>
Regex flavors: Ruby</p>
<h2>2.5. Match Something at the Start and/or the End of a Line</h2>
<h3>Solution</h3>
<h4>Start of the subject</h4>
<pre>^alpha</pre>
<p>Regex options: None
          (“^ and $ match at line breaks” must not be set)<br>
Regex flavors: .NET,
          Java, JavaScript, PCRE, Perl, Python</p>
<pre>\Aalpha</pre>
<p>Regex options: None<br>
Regex flavors: .NET,
          Java, PCRE, Perl, Python, Ruby</p>
<h4>End of the subject</h4>
<pre>omega$</pre>
<p>Regex options: None
          (“^ and $ match at line breaks” must not be set)<br>
Regex flavors: .NET,
          Java, JavaScript, PCRE, Perl, Python</p>
<pre>omega\Z</pre>
<p>Regex options: None<br>
Regex flavors: .NET,
          Java, PCRE, Perl, Python, Ruby</p>
<h4>Start of a line</h4>
<pre>^begin</pre>
<p>Regex options: ^ and
          $ match at line breaks<br>
Regex flavors: .NET,
          Java, JavaScript, PCRE, Perl, Python, Ruby</p>
<h4>End of a line</h4>
<pre>end$</pre>
<p>Regex options: ^ and
          $ match at line breaks<br>
Regex flavors: .NET,
          Java, JavaScript, PCRE, Perl, Python, Ruby</p>
<h3>Variations</h3>
<pre>(?m)^begin</pre>
<p>Regex options: None<br>
Regex flavors: .NET,
        Java, XRegExp, PCRE, Perl, Python</p>
<pre>(?m)end$</pre>
<p>Regex options: None<br>
Regex flavors: .NET,
        Java, XRegExp, PCRE, Perl, Python</p>
<h2>2.6. Match Whole Words</h2>
<h3>Solution</h3>
<h4>Word boundaries</h4>
<pre>\bcat\b</pre>
<p>Regex options: None<br>
Regex flavors: .NET,
          Java, JavaScript, PCRE, Perl, Python, Ruby</p>
<h4>Nonboundaries</h4>
<pre>\Bcat\B</pre>
<p>Regex options: None<br>
Regex flavors: .NET,
          Java, JavaScript, PCRE, Perl, Python, Ruby</p>
<h2>2.7. Unicode Code Points, Categories, Blocks, and Scripts</h2>
<h3>Solution</h3>
<h4>Unicode code point</h4>
<pre>\u2122</pre>
<p>Regex options: None<br>
Regex flavors: .NET,
          Java, JavaScript, Python, Ruby 1.9</p>
<pre>\U00002122</pre>
<p>Regex options: None<br>
Regex flavors: Python</p>
<pre>\x{2122}</pre>
<p>Regex options: None<br>
Regex flavors: Java
          7, PCRE, Perl</p>
<pre>\u{2122}</pre>
<p>Regex options: None<br>
Regex flavors: Ruby
          1.9</p>
<h4>Unicode category</h4>
<pre>\p{Sc}</pre>
<p>Regex options: None<br>
Regex flavors: .NET,
          Java, XRegExp, PCRE, Perl, Ruby 1.9</p>
<h4>Unicode block</h4>
<pre>\p{IsGreekExtended}</pre>
<p>Regex options: None<br>
Regex flavors: .NET,
          Perl</p>
<pre>\p{InGreekExtended}</pre>
<p>Regex options: None<br>
Regex flavors: Java,
          XRegExp, Perl</p>
<h4>Unicode script</h4>
<pre>\p{Greek}</pre>
<p>Regex options: None<br>
Regex flavors: XRegExp, PCRE, Perl, Ruby 1.9</p>
<pre>\p{IsGreek}</pre>
<p>Regex options: None<br>
Regex flavors: Java
          7, Perl</p>
<h4>Unicode grapheme</h4>
<pre>\X</pre>
<p>Regex options: None<br>
Regex flavors: PCRE,
          Perl</p>
<pre>(?&gt;\P{M}\p{M}*)</pre>
<p>Regex options: None<br>
Regex flavors: .NET,
          Java, Ruby 1.9</p>
<pre>(?:\P{M}\p{M}*)</pre>
<p>Regex options: None<br>
Regex flavors: XRegExp</p>
<h3>Variations</h3>
<h4>Character classes</h4>
<pre>[\p{Pi}\p{Pf}\u2122]</pre>
<p>Regex options: None<br>
Regex flavors: .NET,
          Java, XRegExp, Ruby 1.9</p>
<pre>[\p{Pi}\p{Pf}\x{2122}]</pre>
<p>Regex options: None<br>
Regex flavors: Java
          7, PCRE, Perl</p>
<h4>Listing all characters</h4>
<pre>[\u1F00-\u1FFF]</pre>
<p>Regex options: None<br>
Regex flavors: .NET,
          Java, JavaScript, Python, Ruby 1.9</p>
<pre>[\x{1F00}-\x{1FFF}]</pre>
<p>Regex options: None<br>
Regex flavors: Java
          7, PCRE, Perl</p>
<pre>[\u0370-\u0373\u0375-\u0377\u037A-\u037D\u0384\u0386\u0388-\u038A\u038C\u038E-\u03A1\u03A3-\u03E1\u03F0-\u03FF\u1D26-\u1D2A\u1D5D-\u1D61\u1D66-\u1D6A\u1DBF\u1F00-\u1F15\u1F18-\u1F1D\u1F20-\u1F45\u1F48-\u1F4D\u1F50-\u1F57\u1F59\u1F5B\u1F5D\u1F5F-\u1F7D\u1F80-\u1FB4\u1FB6-\u1FC4\u1FC6-\u1FD3\u1FD6-\u1FDB\u1FDD-\u1FEF\u1FF2-\u1FF4\u1FF6-\u1FFE\u2126\U00010140-\U0001018A\U0001D200-\U0001D245]</pre>
<p>Regex options: None<br>
Regex flavors: Python</p>
<pre>[\u0370-\u0373\u0375-\u0377\u037A-\u037D\u0384\u0386\u0388-\u038A\u038C\u038E-\u03A1\u03A3-\u03E1\u03F0-\u03FF\u1D26-\u1D2A\u1D5D-\u1D61\u1D66-\u1D6A\u1DBF\u1F00-\u1F15\u1F18-\u1F1D\u1F20-\u1F45\u1F48-\u1F4D\u1F50-\u1F57\u1F59\u1F5B\u1F5D\u1F5F-\u1F7D\u1F80-\u1FB4\u1FB6-\u1FC4\u1FC6-\u1FD3\u1FD6-\u1FDB\u1FDD-\u1FEF\u1FF2-\u1FF4\u1FF6-\u1FFE\u2126]</pre>
<p>Regex options: None<br>
Regex flavors: .NET,
          Java, JavaScript, Python, Ruby 1.9</p>
<pre>[\x{0370}-\x{0373}\x{0375}-\x{0377}\x{037A}-\x{037D}\x{0384}\x{0386}\x{0388}-\x{038A}\x{038C}\x{038E}-\x{03A1}\x{03A3}-\x{03E1}\x{03F0}-\x{03FF}\x{1D26}-\x{1D2A}\x{1D5D}-\x{1D61}\x{1D66}-\x{1D6A}\x{1DBF}\x{1F00}-\x{1F15}\x{1F18}-\x{1F1D}\x{1F20}-\x{1F45}\x{1F48}-\x{1F4D}\x{1F50}-\x{1F57}\x{1F59}\x{1F5B}\x{1F5D}\x{1F5F}-\x{1F7D}\x{1F80}-\x{1FB4}\x{1FB6}-\x{1FC4}\x{1FC6}-\x{1FD3}\x{1FD6}-\x{1FDB}\x{1FDD}-\x{1FEF}\x{1FF2}-\x{1FF4}\x{1FF6}-\x{1FFE}\x{2126}\x{10140}-\x{10178}\x{10179}-\x{10189}\x{1018A}\x{1D200}-\x{1D245}]</pre>
<p>Regex options: None<br>
Regex flavors: Java
          7, PCRE, Perl</p>
<h2>2.8. Match One of Several Alternatives</h2>
<h3>Solution</h3>
<pre>Mary|Jane|Sue</pre>
<p>Regex options: None<br>
Regex flavors: .NET,
        Java, JavaScript, PCRE, Perl, Python, Ruby</p>
<h2>2.9. Group and Capture Parts of the Match</h2>
<h3>Solution</h3>
<pre>\b(Mary|Jane|Sue)\b</pre>
<p>Regex options: None<br>
Regex flavors: .NET,
        Java, JavaScript, PCRE, Perl, Python, Ruby</p>
<pre>\b(\d\d\d\d)-(\d\d)-(\d\d)\b</pre>
<p>Regex options: None<br>
Regex flavors: .NET,
        Java, JavaScript, PCRE, Perl, Python, Ruby</p>
<h3>Variations</h3>
<h4>Noncapturing groups</h4>
<pre>\b(?:Mary|Jane|Sue)\b</pre>
<p>Regex options: None<br>
Regex flavors: .NET,
          Java, JavaScript, PCRE, Perl, Python, Ruby</p>
<h4>Group with mode modifiers</h4>
<pre>\b(?i:Mary|Jane|Sue)\b</pre>
<p>Regex options: None<br>
Regex flavors: .NET,
          Java, PCRE, Perl, Ruby</p>
<pre>sensitive(?i:caseless)sensitive</pre>
<p>Regex options: None<br>
Regex flavors: .NET,
          Java, PCRE, Perl, Ruby</p>
<pre>(?i:…)</pre>
<h2>2.10. Match Previously Matched Text Again</h2>
<h3>Solution</h3>
<pre>\b\d\d(\d\d)-\1-\1\b</pre>
<p>Regex options: None<br>
Regex flavors: .NET,
        Java, JavaScript, PCRE, Perl, Python, Ruby</p>
<h2>2.11. Capture and Name Parts of the Match</h2>
<h3>Solution</h3>
<h4>Named capture</h4>
<pre>\b(?&lt;year&gt;\d\d\d\d)-(?&lt;month&gt;\d\d)-(?&lt;day&gt;\d\d)\b</pre>
<p>Regex options: None<br>
Regex flavors: .NET,
          Java 7, XRegExp, PCRE 7, Perl 5.10, Ruby 1.9</p>
<pre>\b(?'year'\d\d\d\d)-(?'month'\d\d)-(?'day'\d\d)\b</pre>
<p>Regex options: None<br>
Regex flavors: .NET,
          PCRE 7, Perl 5.10, Ruby 1.9</p>
<pre>\b(?P&lt;year&gt;\d\d\d\d)-(?P&lt;month&gt;\d\d)-(?P&lt;day&gt;\d\d)\b</pre>
<p>Regex options: None<br>
Regex flavors: PCRE 4
          and later, Perl 5.10, Python</p>
<h4>Named backreferences</h4>
<pre>\b\d\d(?&lt;magic&gt;\d\d)-\k&lt;magic&gt;-\k&lt;magic&gt;\b</pre>
<p>Regex options: None<br>
Regex flavors: .NET,
          Java 7, XRegExp, PCRE 7, Perl 5.10, Ruby 1.9</p>
<pre>\b\d\d(?'magic'\d\d)-\k'magic'-\k'magic'\b</pre>
<p>Regex options: None<br>
Regex flavors: .NET,
          PCRE 7, Perl 5.10, Ruby 1.9</p>
<pre>\b\d\d(?P&lt;magic&gt;\d\d)-(?P=magic)-(?P=magic)\b</pre>
<p>Regex options: None<br>
Regex flavors: PCRE 4
          and later, Perl 5.10, Python</p>
<h2>2.12. Repeat Part of the Regex a Certain Number of Times</h2>
<h3>Solution</h3>
<h4>Googol</h4>
<pre>\b\d{100}\b</pre>
<p>Regex options: None<br>
Regex flavors: .NET,
          Java, JavaScript, PCRE, Perl, Python, Ruby</p>
<h4>Hexadecimal number</h4>
<pre>\b[a-f0-9]{1,8}\b</pre>
<p>Regex options: Case
          insensitive<br>
Regex flavors: .NET,
          Java, JavaScript, PCRE, Perl, Python, Ruby</p>
<h4>Hexadecimal number with optional suffix</h4>
<pre>\b[a-f0-9]{1,8}h?\b</pre>
<p>Regex options: Case
          insensitive<br>
Regex flavors: .NET,
          Java, JavaScript, PCRE, Perl, Python, Ruby</p>
<h4>Floating-point number</h4>
<pre>\d*\.\d+(e\d+)?</pre>
<p>Regex options: Case
          insensitive<br>
Regex flavors: .NET,
          Java, JavaScript, PCRE, Perl, Python, Ruby</p>
<h2>2.13. Choose Minimal or Maximal Repetition</h2>
<h3>Solution</h3>
<pre>&lt;p&gt;.*?&lt;/p&gt;</pre>
<p>Regex options: Dot
        matches line breaks<br>
Regex flavors: .NET,
        Java, JavaScript, PCRE, Perl, Python, Ruby</p>
<h3>Discussion</h3>
<pre>&lt;p&gt;.*&lt;/p&gt;</pre>
<p>Regex options: Dot
        matches line breaks<br>
Regex flavors: .NET,
        Java, JavaScript, PCRE, Perl, Python, Ruby</p>
<h2>2.14. Eliminate Needless Backtracking</h2>
<h3>Solution</h3>
<pre>\b\d++\b</pre>
<p>Regex options: None<br>
Regex flavors: Java,
        PCRE, Perl 5.10, Ruby 1.9</p>
<pre>\b(?&gt;\d+)\b</pre>
<p>Regex options: None<br>
Regex flavors: .NET,
        Java, PCRE, Perl, Ruby</p>
<h2>2.15. Prevent Runaway Repetition</h2>
<h3>Solution</h3>
<pre>&lt;html&gt;(?&gt;.*?&lt;head&gt;)(?&gt;.*?&lt;title&gt;)(?&gt;.*?&lt;/title&gt;)(?&gt;.*?&lt;/head&gt;)(?&gt;.*?&lt;body[^&gt;]*&gt;)(?&gt;.*?&lt;/body&gt;).*?&lt;/html&gt;</pre>
<p>Regex options: Case
        insensitive, dot matches line breaks<br>
Regex flavors: .NET,
        Java, PCRE, Perl, Ruby</p>
<h3>Discussion</h3>
<pre>&lt;html&gt;.*?&lt;head&gt;.*?&lt;title&gt;.*?&lt;/title&gt;.*?&lt;/head&gt;.*?&lt;body[^&gt;]*&gt;.*?&lt;/body&gt;.*?&lt;/html&gt;</pre>
<p>Regex options: Case
        insensitive, dot matches line breaks<br>
Regex flavors: .NET,
        Java, JavaScript, PCRE, Perl, Python, Ruby</p>
<h2>2.16. Test for a Match Without Adding It to the Overall Match</h2>
<h3>Solution</h3>
<pre>(?&lt;=&lt;b&gt;)\w+(?=&lt;/b&gt;)</pre>
<p>Regex options: Case
        insensitive<br>
Regex flavors: .NET,
        Java, PCRE, Perl, Python, Ruby 1.9</p>
<h3>Discussion</h3>
<h4>Matching the same text twice</h4>
<pre>(?=\p{Thai})\p{N}</pre>
<p>Regex options: None<br>
Regex flavors: PCRE,
          Perl, Ruby 1.9</p>
<h4>Lookaround is atomic</h4>
<pre>(?=(\d+))\w+\1</pre>
<p>Regex options: None<br>
Regex flavors: .NET,
          Java, JavaScript, PCRE, Perl, Python, Ruby</p>
<h3>Alternative to Lookbehind</h3>
<pre>&lt;b&gt;\K\w+(?=&lt;/b&gt;)</pre>
<p>Regex options: Case
        insensitive<br>
Regex flavors: PCRE
        7.2, Perl 5.10</p>
<h3>Solution Without Lookbehind</h3>
<pre>(&lt;b&gt;)(\w+)(?=&lt;/b&gt;)</pre>
<p>Regex options: Case
        insensitive<br>
Regex flavors: .NET,
        Java, JavaScript, PCRE, Perl, Python, Ruby</p>
<pre>var mainregexp = /\w+(?=&lt;\/b&gt;)/;
var lookbehind = /&lt;b&gt;$/;
if (match = mainregexp.exec("My &lt;b&gt;cat&lt;/b&gt; is furry")) {
    // Found a word before a closing tag &lt;/b&gt;
    var potentialmatch = match[0];
    var leftContext = match.input.substring(0, match.index);
    if (lookbehind.exec(leftContext)) {
        // Lookbehind matched:
        // potentialmatch occurs between a pair of &lt;b&gt; tags
    } else {
        // Lookbehind failed: potentialmatch is no good
    }
} else {
    // Unable to find a word before a closing tag &lt;/b&gt;
}</pre>
<h2>2.17. Match One of Two Alternatives Based on a Condition</h2>
<h3>Solution</h3>
<pre>\b(?:(?:(one)|(two)|(three))(?:,|\b)){3,}(?(1)|(?!))(?(2)|(?!))(?(3)|(?!))</pre>
<p>Regex options: None<br>
Regex flavors: .NET,
        PCRE, Perl, Python</p>
<pre>\b(?:(?:(one)|(two)|(three))(?:,|\b)){3,}</pre>
<p>Regex options: None<br>
Regex flavors: .NET,
        Java, JavaScript, PCRE, Perl, Python, Ruby</p>
<h2>2.18. Add Comments to a Regular Expression</h2>
<h3>Solution</h3>
<pre>\d{4}    # Year
-        # Separator
\d{2}    # Month
-        # Separator
\d{2}    # Day</pre>
<p>Regex options: Free-spacing<br>
Regex flavors: .NET,
        Java, XRegExp, PCRE, Perl, Python, Ruby</p>
<h3>Variations</h3>
<pre>(?#Year)\d{4}(?#Separator)-(?#Month)\d{2}-(?#Day)\d{2}</pre>
<p>Regex options: None<br>
Regex flavors: .NET,
        XRegExp, PCRE, Perl, Python, Ruby</p>
<pre>(?x)\d{4}    # Year
-            # Separator
\d{2}        # Month
-            # Separator
\d{2}        # Day</pre>
<p>Regex options: None<br>
Regex flavors: .NET,
        Java, XRegExp, PCRE, Perl, Python, Ruby</p>
<h2>2.19. Insert Literal Text into the Replacement Text</h2>
<h3>Solution</h3>
<pre>$%\*$$1\1</pre>
<pre>\$%\\*\$1\\1</pre>
<pre>$%\*\$1\\1</pre>
<pre>\$%\*\$1\\1</pre>
<pre>$%\*$1\\1</pre>
<h3>Discussion</h3>
<h4>.NET and JavaScript</h4>
<pre>$$%\*$$1\1</pre>
<h2>2.20. Insert the Regex Match into the Replacement Text</h2>
<h3>Solution</h3>
<h4>Regular expression</h4>
<pre>http:\S+</pre>
<p>Regex options: None<br>
Regex flavors: .NET,
          Java, JavaScript, PCRE, Perl, Python, Ruby</p>
<h4>Replacement</h4>
<pre>&lt;a href="$&amp;"&gt;$&amp;&lt;/a&gt;</pre>
<pre>&lt;a href="$0"&gt;$0&lt;/a&gt;</pre>
<pre>&lt;a href="\0"&gt;\0&lt;/a&gt;</pre>
<pre>&lt;a href="\&amp;"&gt;\&amp;&lt;/a&gt;</pre>
<pre>&lt;a href="\g&lt;0&gt;"&gt;\g&lt;0&gt;&lt;/a&gt;</pre>
<h2>2.21. Insert Part of the Regex Match into the Replacement Text</h2>
<h3>Solution</h3>
<h4>Regular expression</h4>
<pre>\b(\d{3})(\d{3})(\d{4})\b</pre>
<p>Regex options: None<br>
Regex flavors: .NET,
          Java, JavaScript, PCRE, Perl, Python, Ruby</p>
<h4>Replacement</h4>
<pre>($1) $2-$3</pre>
<pre>(${1}) ${2}-${3}</pre>
<pre>(\1) \2-\3</pre>
<h3>Solution Using Named Capture</h3>
<h4>Regular expression</h4>
<pre>\b(?&lt;area&gt;\d{3})(?&lt;exchange&gt;\d{3})(?&lt;number&gt;\d{4})\b</pre>
<p>Regex options: None<br>
Regex flavors: .NET,
          Java 7, XRegExp, PCRE 7, Perl 5.10, Ruby 1.9</p>
<pre>\b(?'area'\d{3})(?'exchange'\d{3})(?'number'\d{4})\b</pre>
<p>Regex options: None<br>
Regex flavors: .NET,
          PCRE 7, Perl 5.10, Ruby 1.9</p>
<pre>\b(?P&lt;area&gt;\d{3})(?P&lt;exchange&gt;\d{3})(?P&lt;number&gt;\d{4})\b</pre>
<p>Regex options: None<br>
Regex flavors: PCRE,
          Perl 5.10, Python</p>
<h4>Replacement</h4>
<pre>(${area}) ${exchange}-${number}</pre>
<pre>(\g&lt;area&gt;) \g&lt;exchange&gt;-\g&lt;number&gt;</pre>
<pre>(\k&lt;area&gt;) \k&lt;exchange&gt;-\k&lt;number&gt;</pre>
<pre>(\k'area') \k'exchange'-\k'number'</pre>
<pre>($+{area}) $+{exchange}-$+{number}</pre>
<pre>($1) $2-$3</pre>
<h2>2.22. Insert Match Context into the Replacement Text</h2>
<h3>Solution</h3>
<pre>$`$_$'</pre>
<pre>\`\`\&amp;\'\'</pre>
<pre>$`$`$&amp;$'$'</pre>
<h1>3. Programming with Regular Expressions</h1>
<h2>3.1. Literal Regular Expressions in Source Code</h2>
<h3>Solution</h3>
<h4>C#</h4>
<pre>"[$\"'\n\\d/\\\\]"</pre>
<pre>@"[$""'\n\d/\\]"</pre>
<h4>VB.NET</h4>
<pre>"[$""'\n\d/\\]"</pre>
<h4>Java</h4>
<pre>"[$\"'\n\\d/\\\\]"</pre>
<h4>JavaScript</h4>
<pre>/[$"'\n\d\/\\]/</pre>
<h4>XRegExp</h4>
<pre>"[$\"'\n\\d/\\\\]"</pre>
<h4>PHP</h4>
<pre>'%[$"\'\n\d/\\\\]%'</pre>
<h4>Perl</h4>
<pre>/[\$"'\n\d\/\\]/</pre>
<pre>m![\$"'\n\d/\\]!</pre>
<pre>s![\$"'\n\d/\\]!!</pre>
<h4>Python</h4>
<pre>r"""[$"'\n\d/\\]"""</pre>
<pre>"[$\"'\n\\d/\\\\]"</pre>
<h4>Ruby</h4>
<pre>/[$"'\n\d\/\\]/</pre>
<pre>%r![$"'\n\d/\\]!</pre>
<h2>3.2. Import the Regular Expression Library</h2>
<h3>Solution</h3>
<h4>C#</h4>
<pre>using System.Text.RegularExpressions;</pre>
<h4>VB.NET</h4>
<pre>Imports System.Text.RegularExpressions</pre>
<h4>XRegExp</h4>
<pre>&lt;script src="xregexp-all-min.js"&gt;&lt;/script&gt;</pre>
<pre>var XRegExp = require('xregexp').XRegExp;</pre>
<h4>Java</h4>
<pre>import java.util.regex.*;</pre>
<h4>Python</h4>
<pre>import re</pre>
<h2>3.3. Create Regular Expression Objects</h2>
<h3>Solution</h3>
<h4>C#</h4>
<pre>Regex regexObj = new Regex("<i>regex pattern</i>");</pre>
<pre>try {
    Regex regexObj = new Regex(UserInput);
} catch (ArgumentException ex) {
    // Syntax error in the regular expression
}</pre>
<h4>VB.NET</h4>
<pre>Dim RegexObj As New Regex("<i>regex pattern</i>")</pre>
<pre>Try
    Dim RegexObj As New Regex(UserInput)
Catch ex As ArgumentException
    'Syntax error in the regular expression
End Try</pre>
<h4>Java</h4>
<pre>Pattern regex = Pattern.compile("<i>regex pattern</i>");</pre>
<pre>try {
	Pattern regex = Pattern.compile(userInput);
} catch (PatternSyntaxException ex) {
	// Syntax error in the regular expression
}</pre>
<pre>Matcher regexMatcher = regex.matcher(subjectString);</pre>
<pre>regexMatcher.reset(anotherSubjectString);</pre>
<h4>JavaScript</h4>
<pre>var myregexp = /<i>regex pattern</i>/;</pre>
<pre>var myregexp = new RegExp(userinput);</pre>
<h4>XRegExp</h4>
<pre>var myregexp = XRegExp("<i>regex pattern</i>");</pre>
<h4>Perl</h4>
<pre>$myregex = qr/<i>regex pattern</i>/</pre>
<pre>$myregex = qr/$userinput/</pre>
<h4>Python</h4>
<pre>reobj = re.compile("<i>regex pattern</i>")</pre>
<pre>reobj = re.compile(userinput)</pre>
<h4>Ruby</h4>
<pre>myregexp = /<i>regex pattern</i>/;</pre>
<pre>myregexp = Regexp.new(userinput);</pre>
<h3>Compiling a Regular Expression Down to CIL</h3>
<h4>C#</h4>
<pre>Regex regexObj = new Regex("<i>regex pattern</i>", RegexOptions.Compiled);</pre>
<h4>VB.NET</h4>
<pre>Dim RegexObj As New Regex("<i>regex pattern</i>", RegexOptions.Compiled)</pre>
<h2>3.4. Set Regular Expression Options</h2>
<h3>Solution</h3>
<h4>C#</h4>
<pre>Regex regexObj = new Regex("<i>regex pattern</i>",
    RegexOptions.IgnorePatternWhitespace | RegexOptions.IgnoreCase |
    RegexOptions.Singleline | RegexOptions.Multiline);</pre>
<h4>VB.NET</h4>
<pre>Dim RegexObj As New Regex("<i>regex pattern</i>",
    RegexOptions.IgnorePatternWhitespace Or RegexOptions.IgnoreCase Or
    RegexOptions.Singleline Or RegexOptions.Multiline)</pre>
<h4>Java</h4>
<pre>Pattern regex = Pattern.compile("<i>regex pattern</i>",
    Pattern.COMMENTS | Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE |
    Pattern.DOTALL | Pattern.MULTILINE);</pre>
<h4>JavaScript</h4>
<pre>var myregexp = /<i>regex pattern</i>/im;</pre>
<pre>var myregexp = new RegExp(userinput, "im");</pre>
<h4>XRegExp</h4>
<pre>var myregexp = XRegExp("<i>regex pattern</i>", "xism");</pre>
<h4>PHP</h4>
<pre>regexstring = '/<i>regex pattern</i>/xism';</pre>
<h4>Perl</h4>
<pre>m/<i>regex pattern</i>/xism;</pre>
<h4>Python</h4>
<pre>reobj = re.compile("<i>regex pattern</i>",
    re.VERBOSE | re.IGNORECASE |
    re.DOTALL | re.MULTILINE)</pre>
<h4>Ruby</h4>
<pre>myregexp = /<i>regex pattern</i>/xim;</pre>
<pre>myregexp = Regexp.new(userinput,
    Regexp::EXTENDED or Regexp::IGNORECASE or
    Regexp::MULTILINE);</pre>
<h2>3.5. Test If a Match Can Be Found Within a Subject String</h2>
<h3>Solution</h3>
<h4>C#</h4>
<pre>bool foundMatch = Regex.IsMatch(subjectString, "<i>regex pattern</i>");</pre>
<pre>bool foundMatch = false;
try {
    foundMatch = Regex.IsMatch(subjectString, UserInput);
} catch (ArgumentNullException ex) {
    // Cannot pass null as the regular expression or subject string
} catch (ArgumentException ex) {
    // Syntax error in the regular expression
}</pre>
<pre>Regex regexObj = new Regex("<i>regex pattern</i>");
bool foundMatch = regexObj.IsMatch(subjectString);</pre>
<pre>bool foundMatch = false;
try {
    Regex regexObj = new Regex(UserInput);
    try {
        foundMatch = regexObj.IsMatch(subjectString);
    } catch (ArgumentNullException ex) {
        // Cannot pass null as the regular expression or subject string
    }
} catch (ArgumentException ex) {
    // Syntax error in the regular expression
}</pre>
<h4>VB.NET</h4>
<pre>Dim FoundMatch = Regex.IsMatch(SubjectString, "<i>regex pattern</i>")</pre>
<pre>Dim FoundMatch As Boolean
Try
    FoundMatch = Regex.IsMatch(SubjectString, UserInput)
Catch ex As ArgumentNullException
    'Cannot pass Nothing as the regular expression or subject string
Catch ex As ArgumentException
    'Syntax error in the regular expression
End Try</pre>
<pre>Dim RegexObj As New Regex("<i>regex pattern</i>")
Dim FoundMatch = RegexObj.IsMatch(SubjectString)</pre>
<pre>Dim FoundMatch = RegexObj.IsMatch(SubjectString)</pre>
<pre>Dim FoundMatch As Boolean
Try
    Dim RegexObj As New Regex(UserInput)
    Try
        FoundMatch = Regex.IsMatch(SubjectString)
    Catch ex As ArgumentNullException
        'Cannot pass Nothing as the regular expression or subject string
    End Try
Catch ex As ArgumentException
    'Syntax error in the regular expression
End Try</pre>
<h4>Java</h4>
<pre>Pattern regex = Pattern.compile("<i>regex pattern</i>");
Matcher regexMatcher = regex.matcher(subjectString);
boolean foundMatch = regexMatcher.find();</pre>
<pre>boolean foundMatch = false;
try {
	Pattern regex = Pattern.compile(UserInput);
	Matcher regexMatcher = regex.matcher(subjectString);
	foundMatch = regexMatcher.find();
} catch (PatternSyntaxException ex) {
	// Syntax error in the regular expression
}</pre>
<h4>JavaScript</h4>
<pre>if (/regex pattern/.test(subject)) {
    // Successful match
} else {
    // Match attempt failed
}</pre>
<h4>PHP</h4>
<pre>if (preg_match('/regex pattern/', $subject)) {
    # Successful match
} else {
    # Match attempt failed
}</pre>
<h4>Perl</h4>
<pre>if (m/regex pattern/) {
    # Successful match
} else {
    # Match attempt failed
}</pre>
<pre>if ($subject =~ m/regex pattern/) {
    # Successful match
} else {
    # Match attempt failed
}</pre>
<pre>$regex = qr/regex pattern/;
if ($subject =~ $regex) {
    # Successful match
} else {
    # Match attempt failed
}</pre>
<h4>Python</h4>
<pre>if re.search("regex pattern", subject):
    # Successful match
else:
    # Match attempt failed</pre>
<pre>reobj = re.compile("regex pattern")
if reobj.search(subject):
    # Successful match
else:
    # Match attempt failed</pre>
<h4>Ruby</h4>
<pre>if subject =~ /regex pattern/
    # Successful match
else
    # Match attempt failed
end</pre>
<pre>if /regex pattern/ =~ subject
    # Successful match
else
    # Match attempt failed
end</pre>
<h2>3.6. Test Whether a Regex Matches the Subject String Entirely</h2>
<h3>Solution</h3>
<h4>C#</h4>
<pre>bool foundMatch = Regex.IsMatch(subjectString, @"\A<i>regex pattern</i>\Z");</pre>
<pre>Regex regexObj = new Regex(@"\A<i>regex pattern</i>\Z");
bool foundMatch = regexObj.IsMatch(subjectString);</pre>
<h4>VB.NET</h4>
<pre>Dim FoundMatch = Regex.IsMatch(SubjectString, "\A<i>regex pattern</i>\Z")</pre>
<pre>Dim RegexObj As New Regex("\A<i>regex pattern</i>\Z")
Dim FoundMatch = RegexObj.IsMatch(SubjectString)</pre>
<pre>Dim FoundMatch = RegexObj.IsMatch(SubjectString)</pre>
<h4>Java</h4>
<pre>boolean foundMatch = subjectString.matches("<i>regex pattern</i>");</pre>
<pre>Pattern regex = Pattern.compile("<i>regex pattern</i>");
Matcher regexMatcher = regex.matcher(subjectString);
boolean foundMatch = regexMatcher.matches(subjectString);</pre>
<h4>JavaScript</h4>
<pre>if (/^<i>regex pattern</i>$/.test(subject)) {
    // Successful match
} else {
    // Match attempt failed
}</pre>
<h4>PHP</h4>
<pre>if (preg_match('/\A<i>regex pattern</i>\Z/', $subject)) {
    # Successful match
} else {
    # Match attempt failed
}</pre>
<h4>Perl</h4>
<pre>if ($subject =~ m/\A<i>regex pattern</i>\Z/) {
    # Successful match
} else {
    # Match attempt failed
}</pre>
<h4>Python</h4>
<pre>if re.match(r"<i>regex pattern</i>\Z", subject):
    # Successful match
else:
    # Match attempt failed</pre>
<pre>reobj = re.compile(r"<i>regex pattern</i>\Z")
if reobj.match(subject):
    # Successful match
else:
    # Match attempt failed</pre>
<h4>Ruby</h4>
<pre>if subject =~ /\A<i>regex pattern</i>\Z/
    # Successful match
else
    # Match attempt failed
end</pre>
<h2>3.7. Retrieve the Matched Text</h2>
<h3>Solution</h3>
<h4>C#</h4>
<pre>string resultString = Regex.Match(subjectString, @"\d+").Value;</pre>
<pre>string resultString = null;
try {
    resultString = Regex.Match(subjectString, @"\d+").Value;
} catch (ArgumentNullException ex) {
    // Cannot pass null as the regular expression or subject string
} catch (ArgumentException ex) {
    // Syntax error in the regular expression
}</pre>
<pre>Regex regexObj = new Regex(@"\d+");
string resultString = regexObj.Match(subjectString).Value;</pre>
<pre>string resultString = null;
try {
    Regex regexObj = new Regex(@"\d+");
    try {
        resultString = regexObj.Match(subjectString).Value;
    } catch (ArgumentNullException ex) {
        // Cannot pass null as the subject string
    }
} catch (ArgumentException ex) {
    // Syntax error in the regular expression
}</pre>
<h4>VB.NET</h4>
<pre>Dim ResultString  = Regex.Match(SubjectString, "\d+").Value</pre>
<pre>Dim ResultString As String = Nothing
Try
    ResultString = Regex.Match(SubjectString, "\d+").Value
Catch ex As ArgumentNullException
    'Cannot pass Nothing as the regular expression or subject string
Catch ex As ArgumentException
    'Syntax error in the regular expression
End Try</pre>
<pre>Dim RegexObj As New Regex("\d+")
Dim ResultString = RegexObj.Match(SubjectString).Value</pre>
<pre>Dim ResultString As String = Nothing
Try
    Dim RegexObj As New Regex("\d+")
    Try
        ResultString = RegexObj.Match(SubjectString).Value
    Catch ex As ArgumentNullException
        'Cannot pass Nothing as the subject string
    End Try
Catch ex As ArgumentException
    'Syntax error in the regular expression
End Try</pre>
<h4>Java</h4>
<pre>String resultString = null;
Pattern regex = Pattern.compile("\\d+");
Matcher regexMatcher = regex.matcher(subjectString);
if (regexMatcher.find()) {
    resultString = regexMatcher.group();
}</pre>
<pre>String resultString = null;
try {
    Pattern regex = Pattern.compile("\\d+");
    Matcher regexMatcher = regex.matcher(subjectString);
    if (regexMatcher.find()) {
        resultString = regexMatcher.group();
    }
} catch (PatternSyntaxException ex) {
    // Syntax error in the regular expression
}</pre>
<h4>JavaScript</h4>
<pre>var result = subject.match(/\d+/);
if (result) {
    result = result[0];
} else {
    result = '';
}</pre>
<h4>PHP</h4>
<pre>if (preg_match('/\d+/', $subject, $groups)) {
    $result = $groups[0];
} else {
    $result = '';
}</pre>
<h4>Perl</h4>
<pre>if ($subject =~ m/\d+/) {
    $result = $&amp;;
} else {
    $result = '';
}</pre>
<h4>Python</h4>
<pre>matchobj = re.search("regex pattern", subject)
if matchobj:
    result = matchobj.group()
else:
    result = ""</pre>
<pre>reobj = re.compile("regex pattern")
matchobj = reobj.search(subject)
if match:
    result = matchobj.group()
else:
    result = ""</pre>
<h4>Ruby</h4>
<pre>if subject =~ /regex pattern/
    result = $&amp;
else
    result = ""
end</pre>
<pre>matchobj = /regex pattern/.match(subject)
if matchobj
    result = matchobj[0]
else
    result = ""
end</pre>
<h2>3.8. Determine the Position and Length of the Match</h2>
<h3>Solution</h3>
<h4>C#</h4>
<pre>int matchstart, matchlength = -1;
Match matchResult = Regex.Match(subjectString, @"\d+");
if (matchResult.Success) {
    matchstart = matchResult.Index;
    matchlength = matchResult.Length;
}</pre>
<pre>int matchstart, matchlength = -1;
Regex regexObj = new Regex(@"\d+");
Match matchResult = regexObj.Match(subjectString).Value;
if (matchResult.Success) {
    matchstart = matchResult.Index;
    matchlength = matchResult.Length;
}</pre>
<h4>VB.NET</h4>
<pre>Dim MatchStart = -1
Dim MatchLength = -1
Dim MatchResult = Regex.Match(SubjectString, "\d+")
If MatchResult.Success Then
    MatchStart = MatchResult.Index
    MatchLength = MatchResult.Length
End If</pre>
<pre>Dim MatchStart = -1
Dim MatchLength = -1
Dim RegexObj As New Regex("\d+")
Dim MatchResult = Regex.Match(SubjectString, "\d+")
If MatchResult.Success Then
    MatchStart = MatchResult.Index
    MatchLength = MatchResult.Length
End If</pre>
<h4>Java</h4>
<pre>int matchStart, matchLength = -1;
Pattern regex = Pattern.compile("\\d+");
Matcher regexMatcher = regex.matcher(subjectString);
if (regexMatcher.find()) {
	matchStart = regexMatcher.start();
	matchLength = regexMatcher.end() - matchStart;
}</pre>
<h4>JavaScript</h4>
<pre>var matchstart = -1;
var matchlength = -1;
var match = /\d+/.exec(subject);
if (match) {
    matchstart = match.index;
    matchlength = match[0].length;
}</pre>
<h4>PHP</h4>
<pre>if (preg_match('/\d+/', $subject, $groups, PREG_OFFSET_CAPTURE)) {
    $matchstart = $groups[0][1];
    $matchlength = strlen($groups[0][0]);
}</pre>
<h4>Perl</h4>
<pre>if ($subject =~ m/\d+/g) {
    $matchstart = $-[0];
    $matchlength = $+[0] - $-[0];
}</pre>
<h4>Python</h4>
<pre>matchobj = re.search(r"\d+", subject)
if matchobj:
    matchstart = matchobj.start()
    matchlength = matchobj.end() - matchstart</pre>
<pre>reobj = re.compile(r"\d+")
matchobj = reobj.search(subject)
if matchobj:
    matchstart = matchobj.start()
    matchlength = matchobj.end() - matchstart</pre>
<h4>Ruby</h4>
<pre>if subject =~ /regex pattern/
    matchstart = $~.begin()
    matchlength = $~.end() - matchstart
end</pre>
<pre>matchobj = /regex pattern/.match(subject)
if matchobj
    matchstart = matchobj.begin()
    matchlength = matchobj.end() - matchstart
end</pre>
<h2>3.9. Retrieve Part of the Matched Text</h2>
<h3>Solution</h3>
<h4>C#</h4>
<pre>string resultString = Regex.Match(subjectString,
                      "http://([a-z0-9.-]+)").Groups[1].Value;</pre>
<pre>Regex regexObj = new Regex("http://([a-z0-9.-]+)");
string resultString = regexObj.Match(subjectString).Groups[1].Value;</pre>
<h4>VB.NET</h4>
<pre>Dim ResultString = Regex.Match(SubjectString,
                   "http://([a-z0-9.-]+)").Groups(1).Value</pre>
<pre>Dim RegexObj As New Regex("http://([a-z0-9.-]+)")
Dim ResultString = RegexObj.Match(SubjectString).Groups(1).Value</pre>
<h4>Java</h4>
<pre>String resultString = null;