File: perlre.html

package info (click to toggle)
perl-doc-html 5.26.0-4
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 39,400 kB
  • sloc: xml: 36; makefile: 7
file content (3029 lines) | stat: -rw-r--r-- 249,120 bytes parent folder | download
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <title>perlre - perldoc.perl.org</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  <meta http-equiv="Content-Language" content="en-gb">
  <link rel="search" type="application/opensearchdescription+xml" title="Search perldoc.perl.org" href="/static/search.xml"/>
  <link href="static/css-20100830.css" rel="stylesheet" rev="stylesheet" type="text/css" media="screen">
  <link href="static/exploreperl.css" rel="stylesheet" rev="stylesheet" type="text/css">
</head>

<body onLoad="perldoc.startup();" onPageShow="if (event.persisted) perldoc.startup();">
    <div id="page">
      
      <div id="header">
	<div id="homepage_link">
	  <a href="index.html"></a>
	</div>
	<div id="strapline">
	  Perl Programming Documentation
	</div>
	<div id="download_link" class="download">
	  <a href="http://www.perl.org/get.html">Download Perl</a>
	</div>
	<div id="explore_link" class="download">
	  <a id="explore_anchor" href="#">Explore</a>
	</div>
      </div>
      
      <div id="body">
        <div id="left_column">
          <div class="side_group">
            
	    <div class="side_panel doc_panel">
              <p>Manual</p>
              <ul>
                <li><a href="index-overview.html">Overview</a>
                <li><a href="index-tutorials.html">Tutorials</a>
                <li><a href="index-faq.html">FAQs</a>
                <li><a href="index-history.html">History / Changes</a>
                <li><a href="index-licence.html">License</a>
              </ul>
            </div>
            <div class="side_panel doc_panel">
              <p>Reference</p>
              <ul>
                <li><a href="index-language.html">Language</a>
                <li><a href="index-functions.html">Functions</a>
                <li><a href="perlop.html">Operators</a>
                <li><a href="perlvar.html">Special Variables</a>
                <li><a href="index-pragmas.html">Pragmas</a>
                <li><a href="index-utilities.html">Utilities</a>
                <li><a href="index-internals.html">Internals</a>
                <li><a href="index-platforms.html">Platform Specific</a>
              </ul>
            </div>
            <div class="side_panel doc_panel">
              <p>Modules</p>
              <ul>
		<li>
		
                
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		
                  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		
                  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		
                  
		
                  
		    
		  
		
                  
		
                  
		
		
                    <a href="index-modules-A.html">A</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-B.html">B</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-C.html">C</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-D.html">D</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-E.html">E</a>
                    
                      
                        <li>
                      
                    
                
                    <a href="index-modules-F.html">F</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-G.html">G</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-H.html">H</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-I.html">I</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-L.html">L</a>
                    
                      
                        <li>
                      
                    
                
                    <a href="index-modules-M.html">M</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-N.html">N</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-O.html">O</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-P.html">P</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-S.html">S</a>
                    
                      
                        <li>
                      
                    
                
                    <a href="index-modules-T.html">T</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-U.html">U</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-X.html">X</a>
                    
                
              </ul>
            </div>
            
	      <div class="side_panel doc_panel">
		<p>Tools</p>
		<ul>
		  <li><a href="preferences.html">Preferences</a>
		</ul>
	      </div>
            
          </div>
        </div>
        <div id="centre_column">
          <div id="content_header">
            <div id="title_bar">
              <div id="page_name">
                <h1>perlre</h1>
              </div>
              <div id="perl_version">
                Perl 5 version 26.0 documentation
              </div>
              <div class="page_links" id="page_links_top">
                <a href="#" onClick="toolbar.goToTop();return false;">Go to top</a>
		
              </div>
	      <div class="page_links" id="page_links_bottom">
		
                  <a href="#" id="page_index_toggle">Show page index</a> &bull;
		
                <a href="#" id="recent_pages_toggle">Show recent pages</a>		
	      </div>
	      <div id="search_form">
		<form action="search.html" method="GET" id="search">
		  <input type="text" name="q" id="search_box" alt="Search">
		</form>
	      </div>
            </div>
            <div id="breadcrumbs">
                
    <a href="index.html">Home</a> &gt;
    
      
        <a href="index-language.html">Language reference</a> &gt;
      
    
    perlre
  

            </div>
          </div>
          <div id="content_body">
	    <!--[if lt IE 7]>
 <div class="noscript">
   <p>
     <strong>It looks like you're using Internet Explorer 6. This is a very old
     browser which does not offer full support for modern websites.</strong>
   </p>
   <p>
     Unfortunately this means that this website will not work on
     your computer.
   </p>
   <p>
     Don't miss out though! To view the site (and get a better experience from
     many other websites), simply upgrade to
     <a href="http://www.microsoft.com/windows/Internet-explorer/default.aspx">Internet
Explorer 8</a>
     or download an alternative browser such as
     <a href="http://www.mozilla.com/en-US/firefox/firefox.html">Firefox</a>,
     <a href="http://www.apple.com/safari/download/">Safari</a>, or
     <a href="http://www.google.co.uk/chrome">Google Chrome</a>.
   </p>
   <p>
     All of these browsers are free. If you're using a PC at work, you may
     need to contact your IT administrator.
   </p>
 </div>
<![endif]-->
	    <noscript>
	      <div class="noscript">
	      <p>
                <strong>Please note: Many features of this site require JavaScript. You appear to have JavaScript disabled,
	        or are running a non-JavaScript capable web browser.</strong>
	      </p>
	      <p>
		To get the best experience, please enable JavaScript or download a modern web browser such as <a href="http://www.microsoft.com/windows/Internet-explorer/default.aspx">Internet Explorer 8</a>, <a href="http://www.mozilla.com/en-US/firefox/firefox.html">Firefox</a>, <a href="http://www.apple.com/safari/download/">Safari</a>, or <a href="http://www.google.co.uk/chrome">Google Chrome</a>.
              </p>
	      </div>
	    </noscript>

	    <div id="recent_pages" class="hud_container">
	      <div id="recent_pages_header" class="hud_header">
		<div id="recent_pages_close" class="hud_close"><a href="#" onClick="recentPages.hide();return false;"></a></div>
		<div id="recent_pages_title" class="hud_title"><span class="hud_span_top">Recently read</span></div>
		<div id="recent_pages_topright" class="hud_topright"></div>
	      </div>
	      <div id="recent_pages_content" class="hud_content">
	      </div>
	      <div id="recent_pages_footer" class="hud_footer">
		<div id="recent_pages_bottomleft" class="hud_bottomleft"></div>
		<div id="recent_pages_bottom" class="hud_bottom"><span class="hud_span_bottom"></span></div>
		<div id="recent_pages_resize" class="hud_resize"></div>
	      </div>
	    </div>
  
	    <div id="from_search"></div>
            <h1>perlre</h1>


  <!--    -->
<ul><li><a href="#NAME">NAME
  </a><li><a href="#DESCRIPTION">DESCRIPTION</a><ul><li><a href="#The-Basics">The Basics
  </a><li><a href="#Modifiers">Modifiers</a><li><a href="#Regular-Expressions">Regular Expressions</a><li><a href="#Quoting-metacharacters">Quoting metacharacters</a><li><a href="#Extended-Patterns">Extended Patterns</a><li><a href="#Backtracking">Backtracking
 </a><li><a href="#Special-Backtracking-Control-Verbs">Special Backtracking Control Verbs</a><li><a href="#Warning-on-%5c1-Instead-of-%241">Warning on \1 Instead of $1</a><li><a href="#Repeated-Patterns-Matching-a-Zero-length-Substring">Repeated Patterns Matching a Zero-length Substring</a><li><a href="#Combining-RE-Pieces">Combining RE Pieces</a><li><a href="#Creating-Custom-RE-Engines">Creating Custom RE Engines</a><li><a href="#Embedded-Code-Execution-Frequency">Embedded Code Execution Frequency</a><li><a href="#PCRE%2fPython-Support">PCRE/Python Support</a></ul><li><a href="#BUGS">BUGS</a><li><a href="#SEE-ALSO">SEE ALSO</a></ul><a name="NAME"></a><h1>NAME
  </h1>
<p>perlre - Perl regular expressions</p>
<a name="DESCRIPTION"></a><h1>DESCRIPTION</h1>
<p>This page describes the syntax of regular expressions in Perl.</p>
<p>If you haven't used regular expressions before, a tutorial introduction
is available in <a href="perlretut.html">perlretut</a>.  If you know just a little about them,
a quick-start introduction is available in <a href="perlrequick.html">perlrequick</a>.</p>
<p>Except for <a href="#The-Basics">The Basics</a> section, this page assumes you are familiar
with regular expression basics, like what is a "pattern", what does it
look like, and how it is basically used.  For a reference on how they
are used, plus various examples of the same, see discussions of <code class="inline"><a class="l_k" href="functions/m.html">m//</a></code>,
<code class="inline"><a class="l_k" href="functions/s.html">s///</a></code>, <code class="inline"><a class="l_k" href="functions/qr.html">qr//</a></code> and <code class="inline"><span class="q">&quot;??&quot;</span></code>
 in <a href="perlop.html#Regexp-Quote-Like-Operators">Regexp Quote-Like Operators in perlop</a>.</p>
<p>New in v5.22, <a href="re.html#'strict'-mode">use re &#39;strict&#39; </a> applies stricter
rules than otherwise when compiling regular expression patterns.  It can
find things that, while legal, may not be what you intended.</p>
<a name="The-Basics"></a><h2>The Basics
  </h2>
<p>Regular expressions are strings with the very particular syntax and
meaning described in this document and auxiliary documents referred to
by this one.  The strings are called "patterns".  Patterns are used to
determine if some other string, called the "target", has (or doesn't
have) the characteristics specified by the pattern.  We call this
"matching" the target string against the pattern.  Usually the match is
done by having the target be the first operand, and the pattern be the
second operand, of one of the two binary operators <code class="inline">=~</code>
 and <code class="inline">!~</code>
,
listed in <a href="perlop.html#Binding-Operators">Binding Operators in perlop</a>; and the pattern will have been
converted from an ordinary string by one of the operators in
<a href="perlop.html#Regexp-Quote-Like-Operators">Regexp Quote-Like Operators in perlop</a>, like so:</p>
<pre class="verbatim"><ol><li> <span class="i">$foo</span> =~ <span class="q">m/abc/</span></li></ol></pre><p>This evaluates to true if and only if the string in the variable <code class="inline"><span class="i">$foo</span></code>

contains somewhere in it, the sequence of characters "a", "b", then "c".
(The <code class="inline">=~ <span class="q">m</span></code>
, or match operator, is described in
<a href="perlop.html#m%2fPATTERN%2fmsixpodualngc">m/PATTERN/msixpodualngc in perlop</a>.)</p>
<p>Patterns that aren't already stored in some variable must be delimitted,
at both ends, by delimitter characters.  These are often, as in the
example above, forward slashes, and the typical way a pattern is written
in documentation is with those slashes.  In most cases, the delimitter
is the same character, fore and aft, but there are a few cases where a
character looks like it has a mirror-image mate, where the opening
version is the beginning delimiter, and the closing one is the ending
delimiter, like</p>
<pre class="verbatim"><ol><li> <span class="i">$foo</span> =~ <span class="q">m&lt;abc&gt;</span></li></ol></pre><p>Most times, the pattern is evaluated in double-quotish context, but it
is possible to choose delimiters to force single-quotish, like</p>
<pre class="verbatim"><ol><li> <span class="i">$foo</span> =~ <span class="q">m&#39;abc&#39;</span></li></ol></pre><p>If the pattern contains its delimiter within it, that delimiter must be
escaped.  Prefixing it with a backslash (<i>e.g.</i>, <code class="inline"><span class="q">&quot;/foo\/bar/&quot;</span></code>
)
serves this purpose.</p>
<p>Any single character in a pattern matches that same character in the
target string, unless the character is a <i>metacharacter</i> with a special
meaning described in this document.  A sequence of non-metacharacters
matches the same sequence in the target string, as we saw above with
<code class="inline"><a class="l_k" href="functions/m.html">m/abc/</a></code>.</p>
<p>Only a few characters (all of them being ASCII punctuation characters)
are metacharacters.  The most commonly used one is a dot <code class="inline"><span class="q">&quot;.&quot;</span></code>
, which
normally matches almost any character (including a dot itself).</p>
<p>You can cause characters that normally function as metacharacters to be
interpreted literally by prefixing them with a <code class="inline"><span class="q">&quot;\&quot;</span></code>
, just like the
pattern's delimiter must be escaped if it also occurs within the
pattern.  Thus, <code class="inline"><span class="q">&quot;\.&quot;</span></code>
 matches just a literal dot, <code class="inline"><span class="q">&quot;.&quot;</span></code>
 instead of
its normal meaning.  This means that the backslash is also a
metacharacter, so <code class="inline"><span class="q">&quot;\\&quot;</span></code>
 matches a single <code class="inline"><span class="q">&quot;\&quot;</span></code>
.  And a sequence that
contains an escaped metacharacter matches the same sequence (but without
the escape) in the target string.  So, the pattern <code class="inline"><span class="q">/blur\\fl/</span></code>
 would
match any target string that contains the sequence <code class="inline"><span class="q">&quot;blur\fl&quot;</span></code>
.</p>
<p>The metacharacter <code class="inline"><span class="q">&quot;|&quot;</span></code>
 is used to match one thing or another.  Thus</p>
<pre class="verbatim"><ol><li> <span class="i">$foo</span> =~ <span class="q">m/this|that/</span></li></ol></pre><p>is TRUE if and only if <code class="inline"><span class="i">$foo</span></code>
 contains either the sequence <code class="inline"><span class="q">&quot;this&quot;</span></code>
 or
the sequence <code class="inline"><span class="q">&quot;that&quot;</span></code>
.  Like all metacharacters, prefixing the <code class="inline"><span class="q">&quot;|&quot;</span></code>

with a backslash makes it match the plain punctuation character; in its
case, the VERTICAL LINE.</p>
<pre class="verbatim"><ol><li> <span class="i">$foo</span> =~ <span class="q">m/this\|that/</span></li></ol></pre><p>is TRUE if and only if <code class="inline"><span class="i">$foo</span></code>
 contains the sequence <code class="inline"><span class="q">&quot;this|that&quot;</span></code>
.</p>
<p>You aren't limited to just a single <code class="inline"><span class="q">&quot;|&quot;</span></code>
.</p>
<pre class="verbatim"><ol><li> <span class="i">$foo</span> =~ <span class="q">m/fee|fie|foe|fum/</span></li></ol></pre><p>is TRUE if and only if <code class="inline"><span class="i">$foo</span></code>
 contains any of those 4 sequences from
the children's story "Jack and the Beanstalk".</p>
<p>As you can see, the <code class="inline"><span class="q">&quot;|&quot;</span></code>
 binds less tightly than a sequence of
ordinary characters.  We can override this by using the grouping
metacharacters, the parentheses <code class="inline"><span class="q">&quot;(&quot;</span></code>
 and <code class="inline"><span class="q">&quot;)&quot;</span></code>
.</p>
<pre class="verbatim"><ol><li> <span class="i">$foo</span> =~ <span class="q">m/th(is|at) thing/</span></li></ol></pre><p>is TRUE if and only if <code class="inline"><span class="i">$foo</span></code>
 contains either the sequence <code class="inline"><span class="q">&quot;this</span>
<span class="q">thing&quot;</span></code>
 or the sequence <code class="inline"><span class="q">&quot;that thing&quot;</span></code>
.  The portions of the string
that match the portions of the pattern enclosed in parentheses are
normally made available separately for use later in the pattern,
substitution, or program.  This is called "capturing", and it can get
complicated.  See <a href="#Capture-groups">Capture groups</a>.</p>
<p>The first alternative includes everything from the last pattern
delimiter (<code class="inline"><span class="q">&quot;(&quot;</span></code>
, <code class="inline"><span class="q">&quot;(?:&quot;</span></code>
 (described later), <i>etc</i>. or the beginning
of the pattern) up to the first <code class="inline"><span class="q">&quot;|&quot;</span></code>
, and the last alternative
contains everything from the last <code class="inline"><span class="q">&quot;|&quot;</span></code>
 to the next closing pattern
delimiter.  That's why 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. This means that alternatives are not necessarily greedy. For
example: when matching <code class="inline"><span class="w">foo</span>|<span class="w">foot</span></code>
 against <code class="inline"><span class="q">&quot;barefoot&quot;</span></code>
, only the <code class="inline"><span class="q">&quot;foo&quot;</span></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>Besides taking away the special meaning of a metacharacter, a prefixed
backslash changes some letter and digit characters away from matching
just themselves to instead have special meaning.  These are called
"escape sequences", and all such are described in <a href="perlrebackslash.html">perlrebackslash</a>.  A
backslash sequence (of a letter or digit) that doesn't currently have
special meaning to Perl will raise a warning if warnings are enabled,
as those are reserved for potential future use.</p>
<p>One such sequence is <code class="inline">\<span class="w">b</span></code>
, which matches a boundary of some sort.
<code class="inline">\<span class="i">b</span><span class="s">{</span><span class="w">wb</span><span class="s">}</span></code>
 and a few others give specialized types of boundaries.
(They are all described in detail starting at
<a href="perlrebackslash.html#%5cb%7b%7d%2c-%5cb%2c-%5cB%7b%7d%2c-%5cB">\b{}, \b, \B{}, \B in perlrebackslash</a>.)  Note that these don't match
characters, but the zero-width spaces between characters.  They are an
example of a <a href="#Assertions">zero-width assertion</a>.  Consider again,</p>
<pre class="verbatim"><ol><li> <span class="i">$foo</span> =~ <span class="q">m/fee|fie|foe|fum/</span></li></ol></pre><p>It evaluates to TRUE if, besides those 4 words, any of the sequences
"feed", "field", "Defoe", "fume", and many others are in <code class="inline"><span class="i">$foo</span></code>
.  By
judicious use of <code class="inline">\<span class="w">b</span></code>
 (or better (because it is designed to handle
natural language) <code class="inline">\<span class="i">b</span><span class="s">{</span><span class="w">wb</span><span class="s">}</span></code>
), we can make sure that only the Giant's
words are matched:</p>
<pre class="verbatim"><ol><li> <span class="i">$foo</span> =~ <span class="q">m/\b(fee|fie|foe|fum)\b/</span></li><li> <span class="i">$foo</span> =~ <span class="q">m/\b{wb}(fee|fie|foe|fum)\b{wb}/</span></li></ol></pre><p>The final example shows that the characters <code class="inline"><span class="q">&quot;{&quot;</span></code>
 and <code class="inline"><span class="q">&quot;}&quot;</span></code>
 are
metacharacters.</p>
<p>Another use for escape sequences is to specify characters that cannot
(or which you prefer not to) be written literally.  These are described
in detail in <a href="perlrebackslash.html#Character-Escapes">Character Escapes in perlrebackslash</a>, but the next three
paragraphs briefly describe some of them.</p>
<p>Various control characters can be written in C language style: <code class="inline"><span class="q">&quot;\n&quot;</span></code>

matches a newline, <code class="inline"><span class="q">&quot;\t&quot;</span></code>
 a tab, <code class="inline"><span class="q">&quot;\r&quot;</span></code>
 a carriage return, <code class="inline"><span class="q">&quot;\f&quot;</span></code>
 a
form feed, <i>etc</i>.</p>
<p>More generally, <code class="inline">\<i>nnn</i></code>, where <i>nnn</i> is a string of three octal
digits, matches the character whose native code point is <i>nnn</i>.  You
can easily run into trouble if you don't have exactly three digits.  So
always use three, or since Perl 5.14, you can use <code class="inline">\<span class="i">o</span><span class="s">{</span>...<span class="s">}</span></code>
 to specify
any number of octal digits.</p>
<p>Similarly, <code class="inline">\x<i>nn</i></code>, where <i>nn</i> are hexadecimal digits, matches the
character whose native ordinal is <i>nn</i>.  Again, not using exactly two
digits is a recipe for disaster, but you can use <code class="inline">\<span class="i">x</span><span class="s">{</span>...<span class="s">}</span></code>
 to specify
any number of hex digits.</p>
<p>Besides being a metacharacter, the <code class="inline"><span class="q">&quot;.&quot;</span></code>
 is an example of a "character
class", something that can match any single character of a given set of
them.  In its case, the set is just about all possible characters.  Perl
predefines several character classes besides the <code class="inline"><span class="q">&quot;.&quot;</span></code>
; there is a
separate reference page about just these, <a href="perlrecharclass.html">perlrecharclass</a>.</p>
<p>You can define your own custom character classes, by putting into your
pattern in the appropriate place(s), a list of all the characters you
want in the set.  You do this by enclosing the list within <code class="inline"><span class="s">[</span><span class="s">]</span></code>
 bracket
characters.  These are called "bracketed character classes" when we are
being precise, but often the word "bracketed" is dropped.  (Dropping it
usually doesn't cause confusion.)  This means that the <code class="inline"><span class="q">&quot;[&quot;</span></code>
 character
is another metacharacter.  It doesn't match anything just by itelf; it
is used only to tell Perl that what follows it is a bracketed character
class.  If you want to match a literal left square bracket, you must
escape it, like <code class="inline"><span class="q">&quot;\[&quot;</span></code>
.  The matching <code class="inline"><span class="q">&quot;]&quot;</span></code>
 is also a metacharacter;
again it doesn't match anything by itself, but just marks the end of
your custom class to Perl.  It is an example of a "sometimes
metacharacter".  It isn't a metacharacter if there is no corresponding
<code class="inline"><span class="q">&quot;[&quot;</span></code>
, and matches its literal self:</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;]&quot;</span> =~ <span class="q">/]/</span><span class="sc">;</span>  <span class="c"># prints 1</span></li></ol></pre><p>The list of characters within the character class gives the set of
characters matched by the class.  <code class="inline"><span class="q">&quot;[abc]&quot;</span></code>
 matches a single "a" or "b"
or "c".  But if the first character after the <code class="inline"><span class="q">&quot;[&quot;</span></code>
 is <code class="inline"><span class="q">&quot;^&quot;</span></code>
, the
class matches any character not in the list.  Within a list, the <code class="inline"><span class="q">&quot;-&quot;</span></code>

character specifies a range of characters, so that <code class="inline"><span class="w">a</span>-z</code>
 represents all
characters between "a" and "z", inclusive.  If you want either <code class="inline"><span class="q">&quot;-&quot;</span></code>
 or
<code class="inline"><span class="q">&quot;]&quot;</span></code>
 itself to be a member of a class, put it at the start of the list
(possibly after a <code class="inline"><span class="q">&quot;^&quot;</span></code>
), or escape it with a backslash.  <code class="inline"><span class="q">&quot;-&quot;</span></code>
 is
also taken literally when it is at the end of the list, just before the
closing <code class="inline"><span class="q">&quot;]&quot;</span></code>
.  (The following all specify the same class of three
characters: <code class="inline"><span class="s">[</span>-<span class="w">az</span><span class="s">]</span></code>
, <code class="inline"><span class="s">[</span><span class="w">az</span>-<span class="s">]</span></code>
, and <code class="inline"><span class="s">[</span><span class="w">a</span>\-z<span class="s">]</span></code>
.  All are different from
<code class="inline"><span class="s">[</span><span class="w">a</span>-z<span class="s">]</span></code>
, which specifies a class containing twenty-six characters, even
on EBCDIC-based character sets.)</p>
<p>There is lots more to bracketed character classes; full details are in
<a href="perlrecharclass.html#Bracketed-Character-Classes">Bracketed Character Classes in perlrecharclass</a>.</p>
<a name="Metacharacters"></a><h3>Metacharacters

        </h3>
<p><a href="#The-Basics">The Basics</a> introduced some of the metacharacters.  This section
gives them all.  Most of them have the same meaning as in the <i>egrep</i>
command.</p>
<p>Only the <code class="inline"><span class="q">&quot;\&quot;</span></code>
 is always a metacharacter.  The others are metacharacters
just sometimes.  The following tables lists all of them, summarizes
their use, and gives the contexts where they are metacharacters.
Outside those contexts or if prefixed by a <code class="inline"><span class="q">&quot;\&quot;</span></code>
, they match their
corresponding punctuation character.  In some cases, their meaning
varies depending on various pattern modifiers that alter the default
behaviors.  See <a href="#Modifiers">Modifiers</a>.</p>
<pre class="verbatim"><ol><li>            <span class="w">PURPOSE</span>                                  <span class="w">WHERE</span></li><li> \   <span class="w">Escape</span> <span class="w">the</span> <a class="l_k" href="functions/next.html">next</a> <span class="j">character</span>                    <span class="w">Always</span><span class="cm">,</span> <span class="w">except</span> <a class="l_k" href="functions/when.html">when</a></li><li>                                                  <span class="w">escaped</span> <span class="w">by</span> <span class="w">another</span> \</li><li> ^   <span class="w">Match</span> <span class="w">the</span> <span class="w">beginning</span> <span class="w">of</span> <span class="w">the</span> <span class="w">string</span>            <span class="w">Not</span> <span class="w">in</span> <span class="s">[</span><span class="s">]</span></li><li>       <span class="s">(</span><a class="l_k" href="functions/or.html">or</a> <span class="w">line</span><span class="cm">,</span> <a class="l_k" href="functions/if.html">if</a> <span class="q">/m is used)</span></li><li> <span class="q"> ^   Complement the [] class                      At the beginning of []</span></li><li> <span class="q"> .   Match any single character except newline    Not in []</span></li><li>       <span class="q">       (under /s</span><span class="cm">,</span> <span class="w">includes</span> <span class="w">newline</span><span class="s">)</span></li><li> <span class="i">$   Match</span> <span class="w">the</span> <span class="w">end</span> <span class="w">of</span> <span class="w">the</span> <span class="w">string</span>                  <span class="w">Not</span> <span class="w">in</span> <span class="s">[</span><span class="s">]</span><span class="cm">,</span> <span class="w">but</span> <span class="i">can</span></li><li>       <span class="s">(</span><a class="l_k" href="functions/or.html">or</a> <span class="w">before</span> <span class="w">newline</span> <span class="w">at</span> <span class="w">the</span> <span class="w">end</span> <span class="w">of</span> <span class="w">the</span>       <span class="w">mean</span> <span class="w">interpolate</span> <span class="w">a</span></li><li>       <span class="w">string</span><span class="sc">;</span> <a class="l_k" href="functions/or.html">or</a> <span class="w">before</span> <span class="w">any</span> <span class="w">newline</span> <a class="l_k" href="functions/if.html">if</a> <span class="q">/m is     scalar</span></li><li>       <span class="q">       used)</span></li><li> <span class="q"> |   Alternation                                  Not in []</span></li><li> <span class="q"> ()  Grouping                                     Not in []</span></li><li> <span class="q"> [   Start Bracketed Character class              Not in []</span></li><li> <span class="q"> ]   End Bracketed Character class                Only in [], and</span></li><li>                                                    <span class="q">                                                    not first</span></li><li> <span class="q"> *   Matches the preceding element 0 or more      Not in []</span></li><li>       <span class="q">       times</span></li><li> <span class="q"> +   Matches the preceding element 1 or more      Not in []</span></li><li>       <span class="q">       times</span></li><li> <span class="q"> ?   Matches the preceding element 0 or 1         Not in []</span></li><li>       <span class="q">       times</span></li><li> <span class="q"> {   Starts a sequence that gives number(s)       Not in []</span></li><li>       <span class="q">       of times the preceding element can be</span></li><li>       <span class="q">       matched</span></li><li> <span class="q"> {   when following certain escape sequences</span></li><li>       <span class="q">       starts a modifier to the meaning of the</span></li><li>       <span class="q">       sequence</span></li><li> <span class="q"> }   End sequence started by {</span></li><li> <span class="q"> -   Indicates a range                            Only in [] interior</span></li></ol></pre><p>Notice that most of the metacharacters lose their special meaning when
they occur in a bracketed character class, except <code class="inline"><span class="q">&quot;^&quot;</span></code>
 has a different
meaning when it is at the beginning of such a class.  And <code class="inline"><span class="q">&quot;-&quot;</span></code>
 and <code class="inline"><span class="q">&quot;]&quot;</span></code>

are metacharacters only at restricted positions within bracketed
character classes; while <code class="inline"><span class="q">&quot;}&quot;</span></code>
 is a metacharacter only when closing a
special construct started by <code class="inline"><span class="q">&quot;{&quot;</span></code>
.</p>
<p>In double-quotish context, as is usually the case,  you need to be
careful about <code class="inline"><span class="q">&quot;$&quot;</span></code>
 and the non-metacharacter <code class="inline"><span class="q">&quot;@&quot;</span></code>
.  Those could
interpolate variables, which may or may not be what you intended.</p>
<p>These rules were designed for compactness of expression, rather than
legibility and maintainability.  The <a href="#%2fx-and-%2fxx">/x and /xx</a> pattern
modifiers allow you to insert white space to improve readability.  And
use of <code class="inline"><a href="re.html#'strict'-mode">re 'strict'</a></code> adds extra checking to
catch some typos that might silently compile into something unintended.</p>
<p>By default, the <code class="inline"><span class="q">&quot;^&quot;</span></code>
 character is guaranteed to match only the
beginning of the string, the <code class="inline"><span class="q">&quot;$&quot;</span></code>
 character only the end (or before the
newline at the end), and Perl does certain optimizations with the
assumption that the string contains only one line.  Embedded newlines
will not be matched by <code class="inline"><span class="q">&quot;^&quot;</span></code>
 or <code class="inline"><span class="q">&quot;$&quot;</span></code>
.  You may, however, wish to treat a
string as a multi-line buffer, such that the <code class="inline"><span class="q">&quot;^&quot;</span></code>
 will match after any
newline within the string (except if the newline is the last character in
the string), and <code class="inline"><span class="q">&quot;$&quot;</span></code>
 will match before any newline.  At the
cost of a little more overhead, you can do this by using the
<a href="#%2fm">/m</a> modifier on the pattern match operator.  (Older programs
did this by setting <code class="inline"><span class="i">$*</span></code>
, but this option was removed in perl 5.10.)
  </p>
<p>To simplify multi-line substitutions, the <code class="inline"><span class="q">&quot;.&quot;</span></code>
 character never matches a
newline unless you use the <a href="#s">/s</a> modifier, which in effect tells
Perl to pretend the string is a single line--even if it isn't.
 </p>
<a name="Modifiers"></a><h2>Modifiers</h2>
<a name="Overview"></a><h3>Overview</h3>
<p>The default behavior for matching can be changed, using various
modifiers.  Modifiers that relate to the interpretation of the pattern
are listed just below.  Modifiers that alter the way a pattern is used
by Perl are detailed in <a href="perlop.html#Regexp-Quote-Like-Operators">Regexp Quote-Like Operators in perlop</a> and
<a href="perlop.html#Gory-details-of-parsing-quoted-constructs">Gory details of parsing quoted constructs in perlop</a>.</p>
<ul>
<li><a name="*m*"></a><b><b><code class="inline"><a class="l_k" href="functions/m.html">m</a></code></b>
   </b>
<p>Treat the string being matched against as multiple lines.  That is, change <code class="inline"><span class="q">&quot;^&quot;</span></code>
 and <code class="inline"><span class="q">&quot;$&quot;</span></code>
 from matching
the start of the string's first line and the end of its last line to
matching the start and end of each line within the string.</p>
</li>
<li><a name="*s*"></a><b><b><code class="inline"><a class="l_k" href="functions/s.html">s</a></code></b>
  
</b>
<p>Treat the string as single line.  That is, change <code class="inline"><span class="q">&quot;.&quot;</span></code>
 to match any character
whatsoever, even a newline, which normally it would not match.</p>
<p>Used together, as <code class="inline">/ms</code>, they let the <code class="inline"><span class="q">&quot;.&quot;</span></code>
 match any character whatsoever,
while still allowing <code class="inline"><span class="q">&quot;^&quot;</span></code>
 and <code class="inline"><span class="q">&quot;$&quot;</span></code>
 to match, respectively, just after
and just before newlines within the string.</p>
</li>
<li><a name="*i*"></a><b><b><code class="inline"><span class="w">i</span></code>
</b>
  
</b>
<p>Do case-insensitive pattern matching.  For example, "A" will match "a"
under <code class="inline">/i</code>.</p>
<p>If locale matching rules are in effect, the case map is taken from the
current
locale for code points less than 255, and from Unicode rules for larger
code points.  However, matches that would cross the Unicode
rules/non-Unicode rules boundary (ords 255/256) will not succeed, unless
the locale is a UTF-8 one.  See <a href="perllocale.html">perllocale</a>.</p>
<p>There are a number of Unicode characters that match a sequence of
multiple characters under <code class="inline">/i</code>.  For example,
<code class="inline"><span class="w">LATIN</span> <span class="w">SMALL</span> <span class="w">LIGATURE</span> <span class="w">FI</span></code>
 should match the sequence <code class="inline"><span class="w">fi</span></code>
.  Perl is not
currently able to do this when the multiple characters are in the pattern and
are split between groupings, or when one or more are quantified.  Thus</p>
<pre class="verbatim"><ol><li> <span class="q">&quot;\N{LATIN SMALL LIGATURE FI}&quot;</span> =~ <span class="q">/fi/i</span><span class="sc">;</span>          <span class="c"># Matches</span></li><li> <span class="q">&quot;\N{LATIN SMALL LIGATURE FI}&quot;</span> =~ <span class="q">/[fi][fi]/i</span><span class="sc">;</span>    <span class="c"># Doesn&#39;t match!</span></li><li> <span class="q">&quot;\N{LATIN SMALL LIGATURE FI}&quot;</span> =~ <span class="q">/fi*/i</span><span class="sc">;</span>         <span class="c"># Doesn&#39;t match!</span></li><li></li><li> <span class="c"># The below doesn&#39;t match, and it isn&#39;t clear what $1 and $2 would</span></li><li> <span class="c"># be even if it did!!</span></li><li> <span class="q">&quot;\N{LATIN SMALL LIGATURE FI}&quot;</span> =~ <span class="q">/(f)(i)/i</span><span class="sc">;</span>      <span class="c"># Doesn&#39;t match!</span></li></ol></pre><p>Perl doesn't match multiple characters in a bracketed
character class unless the character that maps to them is explicitly
mentioned, and it doesn't match them at all if the character class is
inverted, which otherwise could be highly confusing.  See
<a href="perlrecharclass.html#Bracketed-Character-Classes">Bracketed Character Classes in perlrecharclass</a>, and
<a href="perlrecharclass.html#Negation">Negation in perlrecharclass</a>.</p>
</li>
<li><a name="*x*-and-*xx*"></a><b><b><code class="inline"><span class="w">x</span></code>
</b> and <b><code class="inline"><span class="w">xx</span></code>
</b>
</b>
<p>Extend your pattern's legibility by permitting whitespace and comments.
Details in <a href="#%2fx-and-%2fxx">/x and /xx</a></p>
</li>
<li><a name="*p*"></a><b><b><code class="inline"><span class="w">p</span></code>
</b>
  </b>
<p>Preserve the string matched such that <code class="inline"><span class="i">$</span>{<span class="w">^PREMATCH</span>}</code>
, <code class="inline"><span class="i">$</span>{<span class="w">^MATCH</span>}</code>
, and
<code class="inline"><span class="i">$</span>{<span class="w">^POSTMATCH</span>}</code>
 are available for use after matching.</p>
<p>In Perl 5.20 and higher this is ignored. Due to a new copy-on-write
mechanism, <code class="inline"><span class="i">$</span>{<span class="w">^PREMATCH</span>}</code>
, <code class="inline"><span class="i">$</span>{<span class="w">^MATCH</span>}</code>
, and <code class="inline"><span class="i">$</span>{<span class="w">^POSTMATCH</span>}</code>
 will be available
after the match regardless of the modifier.</p>
</li>
<li><a name="*a*%2c-*d*%2c-*l*%2c-and-*u*"></a><b><b><code class="inline"><span class="w">a</span></code>
</b>, <b><code class="inline"><span class="w">d</span></code>
</b>, <b><code class="inline"><span class="w">l</span></code>
</b>, and <b><code class="inline"><span class="w">u</span></code>
</b>
   </b>
<p>These modifiers, all new in 5.14, affect which character-set rules
(Unicode, <i>etc</i>.) are used, as described below in
<a href="#Character-set-modifiers">Character set modifiers</a>.</p>
</li>
<li><a name="*n*"></a><b><b><code class="inline"><span class="w">n</span></code>
</b>
  
</b>
<p>Prevent the grouping metacharacters <code class="inline"><span class="s">(</span><span class="s">)</span></code>
 from capturing. This modifier,
new in 5.22, will stop <code class="inline"><span class="i">$1</span></code>
, <code class="inline"><span class="i">$2</span></code>
, <i>etc</i>... from being filled in.</p>
<pre class="verbatim"><ol><li>  <span class="q">&quot;hello&quot;</span> =~ <span class="q">/(hi|hello)/</span><span class="sc">;</span>   <span class="c"># $1 is &quot;hello&quot;</span></li><li>  <span class="q">&quot;hello&quot;</span> =~ <span class="q">/(hi|hello)/</span><span class="w">n</span><span class="sc">;</span>  <span class="c"># $1 is undef</span></li></ol></pre><p>This is equivalent to putting <code class="inline">?:</code> at the beginning of every capturing group:</p>
<pre class="verbatim"><ol><li>  <span class="q">&quot;hello&quot;</span> =~ <span class="q">/(?:hi|hello)/</span><span class="sc">;</span> <span class="c"># $1 is undef</span></li></ol></pre><p><code class="inline"><span class="q">/n</span></code>
 can be negated on a per-group basis. Alternatively, named captures
may still be used.</p>
<pre class="verbatim"><ol><li>  <span class="q">&quot;hello&quot;</span> =~ <span class="q">/(?-n:(hi|hello))/</span><span class="w">n</span><span class="sc">;</span>   <span class="c"># $1 is &quot;hello&quot;</span></li><li>  <span class="q">&quot;hello&quot;</span> =~ <span class="q">/(?&lt;greet&gt;hi|hello)/</span><span class="w">n</span><span class="sc">;</span> <span class="c"># $1 is &quot;hello&quot;, $+{greet} is</span></li><li>                                    <span class="c"># &quot;hello&quot;</span></li></ol></pre></li>
<li><a name="Other-Modifiers"></a><b>Other Modifiers</b>
<p>There are a number of flags that can be found at the end of regular
expression constructs that are <i>not</i> generic regular expression flags, but
apply to the operation being performed, like matching or substitution (<code class="inline"><a class="l_k" href="functions/m.html">m//</a></code>
or <code class="inline"><a class="l_k" href="functions/s.html">s///</a></code> respectively).</p>
<p>Flags described further in
<a href="perlretut.html#Using-regular-expressions-in-Perl">Using regular expressions in Perl in perlretut</a> are:</p>
<pre class="verbatim"><ol><li>  <span class="w">c</span>  - <span class="w">keep</span> <span class="w">the</span> <span class="w">current</span> <span class="w">position</span> <span class="w">during</span> <span class="w">repeated</span> <span class="w">matching</span></li><li>  <span class="w">g</span>  - <span class="w">globally</span> <span class="w">match</span> <span class="w">the</span> <span class="w">pattern</span> <span class="w">repeatedly</span> <span class="w">in</span> <span class="w">the</span> <span class="w">string</span></li></ol></pre><p>Substitution-specific modifiers described in
<a href="perlop.html#s%2fPATTERN%2fREPLACEMENT%2fmsixpodualngcer">s/PATTERN/REPLACEMENT/msixpodualngcer in perlop</a> are:</p>
<pre class="verbatim"><ol><li>  <span class="w">e</span>  - <span class="w">evaluate</span> <span class="w">the</span> <span class="w">right</span>-<span class="w">hand</span> <span class="w">side</span> <span class="w">as</span> <span class="w">an</span> <span class="w">expression</span></li><li>  <span class="w">ee</span> - <span class="w">evaluate</span> <span class="w">the</span> <span class="w">right</span> <span class="w">side</span> <span class="w">as</span> <span class="w">a</span> <span class="w">string</span> <span class="w">then</span> <a class="l_k" href="functions/eval.html">eval</a> <span class="w">the</span> <span class="w">result</span></li><li>  <span class="w">o</span>  - <span class="w">pretend</span> <span class="w">to</span> <span class="w">optimize</span> <span class="w">your</span> <span class="w">code</span><span class="cm">,</span> <span class="w">but</span> <span class="w">actually</span> <span class="w">introduce</span> <span class="w">bugs</span></li><li>  <span class="w">r</span>  - <span class="w">perform</span> <span class="w">non</span>-<span class="w">destructive</span> <span class="w">substitution</span> <a class="l_k" href="functions/and.html">and</a> <a class="l_k" href="functions/return.html">return</a> <span class="w">the</span> <span class="w">new</span> <span class="w">value</span></li></ol></pre></li>
</ul>
<p>Regular expression modifiers are usually written in documentation
as <i>e.g.</i>, "the <code class="inline">/x</code> modifier", even though the delimiter
in question might not really be a slash.  The modifiers <code class="inline"><span class="q">/imnsxadlup</span></code>

may also be embedded within the regular expression itself using
the <code class="inline">(?...)</code> construct, see <a href="#Extended-Patterns">Extended Patterns</a> below.</p>
<a name="Details-on-some-modifiers"></a><h3>Details on some modifiers</h3>
<p>Some of the modifiers require more explanation than given in the
<a href="#Overview">Overview</a> above.</p>
<h4><code class="inline">/x</code> and  <code class="inline"><span class="q">/xx</span></code>
</h4>
<p>A single <code class="inline">/x</code> tells
the regular expression parser to ignore most whitespace that is neither
backslashed nor within a bracketed character class.  You can use this to
break up your regular expression into more readable parts.
Also, the <code class="inline"><span class="q">&quot;#&quot;</span></code>
 character is treated as a metacharacter introducing a
comment that runs up to the pattern's closing delimiter, or to the end
of the current line if the pattern extends onto the next line.  Hence,
this is very much like an ordinary Perl code comment.  (You can include
the closing delimiter within the comment only if you precede it with a
backslash, so be careful!)</p>
<p>Use of <code class="inline">/x</code> means that if you want real
whitespace or <code class="inline"><span class="q">&quot;#&quot;</span></code>
 characters in the pattern (outside a bracketed character
class, which is unaffected by <code class="inline">/x</code>), then you'll either have to
escape them (using backslashes or <code class="inline">\<span class="w">Q</span>...\<span class="w">E</span></code>
) or encode them using octal,
hex, or <code class="inline">\<span class="w">N</span><span class="s">{</span><span class="s">}</span></code>
 escapes.
It is ineffective to try to continue a comment onto the next line by
escaping the <code class="inline">\<span class="w">n</span></code>
 with a backslash or <code class="inline">\<span class="w">Q</span></code>
.</p>
<p>You can use <a href="#(%3f%23text)">(?#text)</a> to create a comment that ends earlier than the
end of the current line, but <code class="inline"><span class="w">text</span></code>
 also can't contain the closing
delimiter unless escaped with a backslash.</p>
<p>A common pitfall is to forget that <code class="inline"><span class="q">&quot;#&quot;</span></code>
 characters begin a comment under
<code class="inline">/x</code> and are not matched literally.  Just keep that in mind when trying
to puzzle out why a particular <code class="inline">/x</code> pattern isn't working as expected.</p>
<p>Starting in Perl v5.26, if the modifier has a second <code class="inline"><span class="q">&quot;x&quot;</span></code>
 within it,
it does everything that a single <code class="inline">/x</code> does, but additionally
non-backslashed SPACE and TAB characters within bracketed character
classes are also generally ignored, and hence can be added to make the
classes more readable.</p>
<pre class="verbatim"><ol><li>    <span class="q">/ [d-e g-i 3-7]/xx</span></li><li>    /<span class="s">[</span> ! <span class="i">@ &quot;</span> <span class="c"># $ % ^ &amp; * () = ? &lt;&gt; &#39; ]/xx</span></li></ol></pre><p>may be easier to grasp than the squashed equivalents</p>
<pre class="verbatim"><ol><li>    <span class="q">/[d-eg-i3-7]/</span></li><li>    /<span class="s">[</span>!<span class="i">@&quot;</span><span class="c">#$%^&amp;*()=?&lt;&gt;&#39;]/</span></li></ol></pre><p>Taken together, these features go a long way towards
making Perl's regular expressions more readable.  Here's an example:</p>
<pre class="verbatim"><ol><li>    <span class="c"># Delete (most) C comments.</span></li><li>    <span class="i">$program</span> =~ <span class="q">s {</span></li><li>	<span class="q">	/\*	# Match the opening delimiter.</span></li><li>	<span class="q">	.*?	# Match a minimal number of characters.</span></li><li>	<span class="q">	\*/	# Match the closing delimiter.</span></li><li>    <span class="q">    } []gsx</span><span class="sc">;</span></li></ol></pre><p>Note that anything inside
a <code class="inline">\<span class="w">Q</span>...\<span class="w">E</span></code>
 stays unaffected by <code class="inline">/x</code>.  And note that <code class="inline">/x</code> doesn't affect
space interpretation within a single multi-character construct.  For
example in <code class="inline">\<span class="i">x</span><span class="s">{</span>...<span class="s">}</span></code>
, regardless of the <code class="inline">/x</code> modifier, there can be no
spaces.  Same for a <a href="#Quantifiers">quantifier</a> such as <code class="inline"><span class="s">{</span><span class="n">3</span><span class="s">}</span></code>
 or
<code class="inline"><span class="s">{</span><span class="n">5</span><span class="cm">,</span><span class="s">}</span></code>
.  Similarly, <code class="inline">(?:...)</code> can't have a space between the <code class="inline"><span class="q">&quot;{&quot;</span></code>
,
<code class="inline"><span class="q">&quot;?&quot;</span></code>
, and <code class="inline"><span class="q">&quot;:&quot;</span></code>
.  Within any delimiters for such a
construct, allowed spaces are not affected by <code class="inline">/x</code>, and depend on the
construct.  For example, <code class="inline">\<span class="i">x</span><span class="s">{</span>...<span class="s">}</span></code>
 can't have spaces because hexadecimal
numbers don't have spaces in them.  But, Unicode properties can have spaces, so
in <code class="inline">\<span class="i">p</span><span class="s">{</span>...<span class="s">}</span></code>
 there can be spaces that follow the Unicode rules, for which see
<a href="perluniprops.html#Properties-accessible-through-%5cp%7b%7d-and-%5cP%7b%7d">Properties accessible through \p{} and \P{} in perluniprops</a>.
</p>
<p>The set of characters that are deemed whitespace are those that Unicode
calls "Pattern White Space", namely:</p>
<pre class="verbatim"><ol><li> <span class="w">U</span>+<span class="n">0009</span> <span class="w">CHARACTER</span> <span class="w">TABULATION</span></li><li> <span class="w">U</span>+<span class="n">000</span><span class="w">A</span> <span class="w">LINE</span> <span class="w">FEED</span></li><li> <span class="w">U</span>+<span class="n">000</span><span class="w">B</span> <span class="w">LINE</span> <span class="w">TABULATION</span></li><li> <span class="w">U</span>+<span class="n">000</span><span class="w">C</span> <span class="w">FORM</span> <span class="w">FEED</span></li><li> <span class="w">U</span>+<span class="n">000</span><span class="w">D</span> <span class="w">CARRIAGE</span> <span class="w">RETURN</span></li><li> <span class="w">U</span>+<span class="n">0020</span> <span class="w">SPACE</span></li><li> <span class="w">U</span>+<span class="n">0085</span> <span class="w">NEXT</span> <span class="w">LINE</span></li><li> <span class="w">U</span>+<span class="n">200</span><span class="w">E</span> <span class="w">LEFT</span>-<span class="w">TO</span>-<span class="w">RIGHT</span> <span class="w">MARK</span></li><li> <span class="w">U</span>+<span class="n">200</span><span class="w">F</span> <span class="w">RIGHT</span>-<span class="w">TO</span>-<span class="w">LEFT</span> <span class="w">MARK</span></li><li> <span class="w">U</span>+<span class="n">2028</span> <span class="w">LINE</span> <span class="w">SEPARATOR</span></li><li> <span class="w">U</span>+<span class="n">2029</span> <span class="w">PARAGRAPH</span> <span class="w">SEPARATOR</span></li></ol></pre><h4>Character set modifiers</h4>
<p><code class="inline">/d</code>, <code class="inline"><span class="q">/u</span></code>
, <code class="inline"><span class="q">/a</span></code>
, and <code class="inline"><span class="q">/l</span></code>
, available starting in 5.14, are called
the character set modifiers; they affect the character set rules
used for the regular expression.</p>
<p>The <code class="inline">/d</code>, <code class="inline"><span class="q">/u</span></code>
, and <code class="inline"><span class="q">/l</span></code>
 modifiers are not likely to be of much use
to you, and so you need not worry about them very much.  They exist for
Perl's internal use, so that complex regular expression data structures
can be automatically serialized and later exactly reconstituted,
including all their nuances.  But, since Perl can't keep a secret, and
there may be rare instances where they are useful, they are documented
here.</p>
<p>The <code class="inline"><span class="q">/a</span></code>
 modifier, on the other hand, may be useful.  Its purpose is to
allow code that is to work mostly on ASCII data to not have to concern
itself with Unicode.</p>
<p>Briefly, <code class="inline"><span class="q">/l</span></code>
 sets the character set to that of whatever <b>L</b>ocale is in
effect at the time of the execution of the pattern match.</p>
<p><code class="inline"><span class="q">/u</span></code>
 sets the character set to <b>U</b>nicode.</p>
<p><code class="inline"><span class="q">/a</span></code>
 also sets the character set to Unicode, BUT adds several
restrictions for <b>A</b>SCII-safe matching.</p>
<p><code class="inline">/d</code> is the old, problematic, pre-5.14 <b>D</b>efault character set
behavior.  Its only use is to force that old behavior.</p>
<p>At any given time, exactly one of these modifiers is in effect.  Their
existence allows Perl to keep the originally compiled behavior of a
regular expression, regardless of what rules are in effect when it is
actually executed.  And if it is interpolated into a larger regex, the
original's rules continue to apply to it, and only it.</p>
<p>The <code class="inline"><span class="q">/l</span></code>
 and <code class="inline"><span class="q">/u</span></code>
 modifiers are automatically selected for
regular expressions compiled within the scope of various pragmas,
and we recommend that in general, you use those pragmas instead of
specifying these modifiers explicitly.  For one thing, the modifiers
affect only pattern matching, and do not extend to even any replacement
done, whereas using the pragmas gives consistent results for all
appropriate operations within their scopes.  For example,</p>
<pre class="verbatim"><ol><li> <span class="q">s/foo/\Ubar/il</span></li></ol></pre><p>will match "foo" using the locale's rules for case-insensitive matching,
but the <code class="inline"><span class="q">/l</span></code>
 does not affect how the <code class="inline">\<span class="w">U</span></code>
 operates.  Most likely you
want both of them to use locale rules.  To do this, instead compile the
regular expression within the scope of <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">locale</span></code>
.  This both
implicitly adds the <code class="inline"><span class="q">/l</span></code>
, and applies locale rules to the <code class="inline">\<span class="w">U</span></code>
.   The
lesson is to <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">locale</span></code>
, and not <code class="inline"><span class="q">/l</span></code>
 explicitly.</p>
<p>Similarly, it would be better to use <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">feature</span> <span class="q">&#39;unicode_strings&#39;</span></code>

instead of,</p>
<pre class="verbatim"><ol><li> <span class="q">s/foo/\Lbar/iu</span></li></ol></pre><p>to get Unicode rules, as the <code class="inline">\<span class="w">L</span></code>
 in the former (but not necessarily
the latter) would also use Unicode rules.</p>
<p>More detail on each of the modifiers follows.  Most likely you don't
need to know this detail for <code class="inline"><span class="q">/l</span></code>
, <code class="inline"><span class="q">/u</span></code>
, and <code class="inline">/d</code>, and can skip ahead
to <a href="#%2fa-(and-%2faa)">/a</a>.</p>
<h4>/l</h4>
<p>means to use the current locale's rules (see <a href="perllocale.html">perllocale</a>) when pattern
matching.  For example, <code class="inline">\<span class="w">w</span></code>
 will match the "word" characters of that
locale, and <code class="inline"><span class="q">&quot;/i&quot;</span></code>
 case-insensitive matching will match according to
the locale's case folding rules.  The locale used will be the one in
effect at the time of execution of the pattern match.  This may not be
the same as the compilation-time locale, and can differ from one match
to another if there is an intervening call of the
<a href="perllocale.html#The-setlocale-function">setlocale() function</a>.</p>
<p>Prior to v5.20, Perl did not support multi-byte locales.  Starting then,
UTF-8 locales are supported.  No other multi byte locales are ever
likely to be supported.  However, in all locales, one can have code
points above 255 and these will always be treated as Unicode no matter
what locale is in effect.</p>
<p>Under Unicode rules, there are a few case-insensitive matches that cross
the 255/256 boundary.  Except for UTF-8 locales in Perls v5.20 and
later, these are disallowed under <code class="inline"><span class="q">/l</span></code>
.  For example, 0xFF (on ASCII
platforms) does not caselessly match the character at 0x178, <code class="inline"><span class="w">LATIN</span>
<span class="w">CAPITAL</span> <span class="w">LETTER</span> <span class="w">Y</span> <span class="w">WITH</span> <span class="w">DIAERESIS</span></code>
, because 0xFF may not be <code class="inline"><span class="w">LATIN</span> <span class="w">SMALL</span>
<span class="w">LETTER</span> <span class="w">Y</span> <span class="w">WITH</span> <span class="w">DIAERESIS</span></code>
 in the current locale, and Perl has no way of
knowing if that character even exists in the locale, much less what code
point it is.</p>
<p>In a UTF-8 locale in v5.20 and later, the only visible difference
between locale and non-locale in regular expressions should be tainting
(see <a href="perlsec.html">perlsec</a>).</p>
<p>This modifier may be specified to be the default by <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">locale</span></code>
, but
see <a href="#Which-character-set-modifier-is-in-effect%3f">Which character set modifier is in effect?</a>.
</p>
<h4>/u</h4>
<p>means to use Unicode rules when pattern matching.  On ASCII platforms,
this means that the code points between 128 and 255 take on their
Latin-1 (ISO-8859-1) meanings (which are the same as Unicode's).
(Otherwise Perl considers their meanings to be undefined.)  Thus,
under this modifier, the ASCII platform effectively becomes a Unicode
platform; and hence, for example, <code class="inline">\<span class="w">w</span></code>
 will match any of the more than
100_000 word characters in Unicode.</p>
<p>Unlike most locales, which are specific to a language and country pair,
Unicode classifies all the characters that are letters <i>somewhere</i> in
the world as
<code class="inline">\<span class="w">w</span></code>
.  For example, your locale might not think that <code class="inline"><span class="w">LATIN</span> <span class="w">SMALL</span>
<span class="w">LETTER</span> <span class="w">ETH</span></code>
 is a letter (unless you happen to speak Icelandic), but
Unicode does.  Similarly, all the characters that are decimal digits
somewhere in the world will match <code class="inline">\<span class="w">d</span></code>
; this is hundreds, not 10,
possible matches.  And some of those digits look like some of the 10
ASCII digits, but mean a different number, so a human could easily think
a number is a different quantity than it really is.  For example,
<code class="inline"><span class="w">BENGALI</span> <span class="w">DIGIT</span> <span class="w">FOUR</span></code>
 (U+09EA) looks very much like an
<code class="inline"><span class="w">ASCII</span> <span class="w">DIGIT</span> <span class="w">EIGHT</span></code>
 (U+0038).  And, <code class="inline">\<span class="w">d</span>+</code>
, may match strings of digits
that are a mixture from different writing systems, creating a security
issue.  <a href="Unicode/UCD.html#num()">num() in Unicode::UCD</a> can be used to sort
this out.  Or the <code class="inline"><span class="q">/a</span></code>
 modifier can be used to force <code class="inline">\<span class="w">d</span></code>
 to match
just the ASCII 0 through 9.</p>
<p>Also, under this modifier, case-insensitive matching works on the full
set of Unicode
characters.  The <code class="inline"><span class="w">KELVIN</span> <span class="w">SIGN</span></code>
, for example matches the letters "k" and
"K"; and <code class="inline"><span class="w">LATIN</span> <span class="w">SMALL</span> <span class="w">LIGATURE</span> <span class="w">FF</span></code>
 matches the sequence "ff", which,
if you're not prepared, might make it look like a hexadecimal constant,
presenting another potential security issue.  See
<a href="http://unicode.org/reports/tr36">http://unicode.org/reports/tr36</a> for a detailed discussion of Unicode
security issues.</p>
<p>This modifier may be specified to be the default by <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">feature</span>
<span class="q">&#39;unicode_strings</span></code>
, <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">locale</span> <span class="q">&#39;:not_characters&#39;</span></code>
, or
<code class="inline"><a href="functions/use.html">use VERSION</a></code> (or higher),
but see <a href="#Which-character-set-modifier-is-in-effect%3f">Which character set modifier is in effect?</a>.
</p>
<h4>/d</h4>
<p>This modifier means to use the "Default" native rules of the platform
except when there is cause to use Unicode rules instead, as follows:</p>
<dl>
<dt>1</dt><dd>
<p>the target string is encoded in UTF-8; or</p>
</dd>
<dt>2</dt><dd>
<p>the pattern is encoded in UTF-8; or</p>
</dd>
<dt>3</dt><dd>
<p>the pattern explicitly mentions a code point that is above 255 (say by
<code class="inline">\<span class="i">x</span><span class="s">{</span><span class="n">100</span><span class="s">}</span></code>
); or</p>
</dd>
<dt>4</dt><dd>
<p>the pattern uses a Unicode name (<code class="inline">\<span class="i">N</span><span class="s">{</span>...<span class="s">}</span></code>
);  or</p>
</dd>
<dt>5</dt><dd>
<p>the pattern uses a Unicode property (<code class="inline">\<span class="i">p</span><span class="s">{</span>...<span class="s">}</span></code>
 or <code class="inline">\<span class="i">P</span><span class="s">{</span>...<span class="s">}</span></code>
); or</p>
</dd>
<dt>6</dt><dd>
<p>the pattern uses a Unicode break (<code class="inline">\<span class="i">b</span><span class="s">{</span>...<span class="s">}</span></code>
 or <code class="inline">\<span class="i">B</span><span class="s">{</span>...<span class="s">}</span></code>
); or</p>
</dd>
<dt>7</dt><dd>
<p>the pattern uses <a href="#(%3f%5b-%5d)">(?[ ])</a></p>
</dd>
</dl>
<p>Another mnemonic for this modifier is "Depends", as the rules actually
used depend on various things, and as a result you can get unexpected
results.  See <a href="perlunicode.html#The-%22Unicode-Bug%22">The Unicode Bug in perlunicode</a>.  The Unicode Bug has
become rather infamous, leading to yet another (printable) name for this
modifier, "Dodgy".</p>
<p>Unless the pattern or string are encoded in UTF-8, only ASCII characters
can match positively.</p>
<p>Here are some examples of how that works on an ASCII platform:</p>
<pre class="verbatim"><ol><li> <span class="i">$str</span> =  <span class="q">&quot;\xDF&quot;</span><span class="sc">;</span>      <span class="c"># $str is not in UTF-8 format.</span></li><li> <span class="i">$str</span> =~ <span class="q">/^\w/</span><span class="sc">;</span>       <span class="c"># No match, as $str isn&#39;t in UTF-8 format.</span></li><li> <span class="i">$str</span> .= <span class="q">&quot;\x{0e0b}&quot;</span><span class="sc">;</span>  <span class="c"># Now $str is in UTF-8 format.</span></li><li> <span class="i">$str</span> =~ <span class="q">/^\w/</span><span class="sc">;</span>       <span class="c"># Match! $str is now in UTF-8 format.</span></li><li> <a class="l_k" href="functions/chop.html">chop</a> <span class="i">$str</span><span class="sc">;</span></li><li> <span class="i">$str</span> =~ <span class="q">/^\w/</span><span class="sc">;</span>       <span class="c"># Still a match! $str remains in UTF-8 format.</span></li></ol></pre><p>This modifier is automatically selected by default when none of the
others are, so yet another name for it is "Default".</p>
<p>Because of the unexpected behaviors associated with this modifier, you
probably should only explicitly use it to maintain weird backward
compatibilities.</p>
<h4>/a (and /aa)</h4>
<p>This modifier stands for ASCII-restrict (or ASCII-safe).  This modifier
may be doubled-up to increase its effect.</p>
<p>When it appears singly, it causes the sequences <code class="inline">\<span class="w">d</span></code>
, <code class="inline">\s</code>, <code class="inline">\<span class="w">w</span></code>
, and
the Posix character classes to match only in the ASCII range.  They thus
revert to their pre-5.6, pre-Unicode meanings.  Under <code class="inline"><span class="q">/a</span></code>
,  <code class="inline">\<span class="w">d</span></code>

always means precisely the digits <code class="inline"><span class="q">&quot;0&quot;</span></code>
 to <code class="inline"><span class="q">&quot;9&quot;</span></code>
; <code class="inline">\s</code> means the five
characters <code class="inline"><span class="s">[</span> \<span class="w">f</span>\<span class="w">n</span>\<span class="w">r</span>\<span class="w">t</span><span class="s">]</span></code>
, and starting in Perl v5.18, the vertical tab;
<code class="inline">\<span class="w">w</span></code>
 means the 63 characters
<code class="inline"><span class="s">[</span><span class="w">A</span>-<span class="w">Za</span>-<span class="w">z0</span>-<span class="n">9_</span><span class="s">]</span></code>
; and likewise, all the Posix classes such as
<code class="inline">[[:print:]]</code> match only the appropriate ASCII-range characters.</p>
<p>This modifier is useful for people who only incidentally use Unicode,
and who do not wish to be burdened with its complexities and security
concerns.</p>
<p>With <code class="inline"><span class="q">/a</span></code>
, one can write <code class="inline">\<span class="w">d</span></code>
 with confidence that it will only match
ASCII characters, and should the need arise to match beyond ASCII, you
can instead use <code class="inline">\<span class="i">p</span><span class="s">{</span><span class="w">Digit</span><span class="s">}</span></code>
 (or <code class="inline">\<span class="i">p</span><span class="s">{</span><span class="w">Word</span><span class="s">}</span></code>
 for <code class="inline">\<span class="w">w</span></code>
).  There are
similar <code class="inline">\<span class="i">p</span><span class="s">{</span>...<span class="s">}</span></code>
 constructs that can match beyond ASCII both white
space (see <a href="perlrecharclass.html#Whitespace">Whitespace in perlrecharclass</a>), and Posix classes (see
<a href="perlrecharclass.html#POSIX-Character-Classes">POSIX Character Classes in perlrecharclass</a>).  Thus, this modifier
doesn't mean you can't use Unicode, it means that to get Unicode
matching you must explicitly use a construct (<code class="inline">\<span class="w">p</span><span class="s">{</span><span class="s">}</span></code>
, <code class="inline">\<span class="w">P</span><span class="s">{</span><span class="s">}</span></code>
) that
signals Unicode.</p>
<p>As you would expect, this modifier causes, for example, <code class="inline">\<span class="w">D</span></code>
 to mean
the same thing as <code class="inline"><span class="s">[</span>^<span class="n">0</span>-<span class="n">9</span><span class="s">]</span></code>
; in fact, all non-ASCII characters match
<code class="inline">\<span class="w">D</span></code>
, <code class="inline">\<span class="w">S</span></code>
, and <code class="inline">\<span class="w">W</span></code>
.  <code class="inline">\<span class="w">b</span></code>
 still means to match at the boundary
between <code class="inline">\<span class="w">w</span></code>
 and <code class="inline">\<span class="w">W</span></code>
, using the <code class="inline"><span class="q">/a</span></code>
 definitions of them (similarly
for <code class="inline">\<span class="w">B</span></code>
).</p>
<p>Otherwise, <code class="inline"><span class="q">/a</span></code>
 behaves like the <code class="inline"><span class="q">/u</span></code>
 modifier, in that
case-insensitive matching uses Unicode rules; for example, "k" will
match the Unicode <code class="inline">\<span class="i">N</span><span class="s">{</span><span class="w">KELVIN</span> <span class="w">SIGN</span><span class="s">}</span></code>
 under <code class="inline">/i</code> matching, and code
points in the Latin1 range, above ASCII will have Unicode rules when it
comes to case-insensitive matching.</p>
<p>To forbid ASCII/non-ASCII matches (like "k" with <code class="inline">\<span class="i">N</span><span class="s">{</span><span class="w">KELVIN</span> <span class="w">SIGN</span><span class="s">}</span></code>
),
specify the <code class="inline"><span class="q">&quot;a&quot;</span></code>
 twice, for example <code class="inline"><span class="q">/aai</span></code>
 or <code class="inline"><span class="q">/aia</span></code>
.  (The first
occurrence of <code class="inline"><span class="q">&quot;a&quot;</span></code>
 restricts the <code class="inline">\<span class="w">d</span></code>
, <i>etc</i>., and the second occurrence
adds the <code class="inline">/i</code> restrictions.)  But, note that code points outside the
ASCII range will use Unicode rules for <code class="inline">/i</code> matching, so the modifier
doesn't really restrict things to just ASCII; it just forbids the
intermixing of ASCII and non-ASCII.</p>
<p>To summarize, this modifier provides protection for applications that
don't wish to be exposed to all of Unicode.  Specifying it twice
gives added protection.</p>
<p>This modifier may be specified to be the default by <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">re</span> <span class="q">&#39;/a&#39;</span></code>

or <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">re</span> <span class="q">&#39;/aa&#39;</span></code>
.  If you do so, you may actually have occasion to use
the <code class="inline"><span class="q">/u</span></code>
 modifier explicitly if there are a few regular expressions
where you do want full Unicode rules (but even here, it's best if
everything were under feature <code class="inline"><span class="q">&quot;unicode_strings&quot;</span></code>
, along with the
<code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">re</span> <span class="q">&#39;/aa&#39;</span></code>
).  Also see <a href="#Which-character-set-modifier-is-in-effect%3f">Which character set modifier is in effect?</a>.

</p>
<h4>Which character set modifier is in effect?</h4>
<p>Which of these modifiers is in effect at any given point in a regular
expression depends on a fairly complex set of interactions.  These have
been designed so that in general you don't have to worry about it, but
this section gives the gory details.  As
explained below in <a href="#Extended-Patterns">Extended Patterns</a> it is possible to explicitly
specify modifiers that apply only to portions of a regular expression.
The innermost always has priority over any outer ones, and one applying
to the whole expression has priority over any of the default settings that are
described in the remainder of this section.</p>
<p>The <code class="inline"><a href="re.html#'%2fflags'-mode">use re '/foo'</a></code> pragma can be used to set
default modifiers (including these) for regular expressions compiled
within its scope.  This pragma has precedence over the other pragmas
listed below that also change the defaults.</p>
<p>Otherwise, <code class="inline"><a href="perllocale.html">use locale</a></code> sets the default modifier to <code class="inline"><span class="q">/l</span></code>
;
and <code class="inline"><a href="feature.html">use feature 'unicode_strings</a></code>, or
<code class="inline"><a href="functions/use.html">use VERSION</a></code> (or higher) set the default to
<code class="inline"><span class="q">/u</span></code>
 when not in the same scope as either <code class="inline"><a href="perllocale.html">use locale</a></code>
or <code class="inline"><a href="bytes.html">use bytes</a></code>.
(<code class="inline"><a href="perllocale.html#Unicode-and-UTF-8">use locale ':not_characters'</a></code> also
sets the default to <code class="inline"><span class="q">/u</span></code>
, overriding any plain <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">locale</span></code>
.)
Unlike the mechanisms mentioned above, these
affect operations besides regular expressions pattern matching, and so
give more consistent results with other operators, including using
<code class="inline">\<span class="w">U</span></code>
, <code class="inline">\<span class="w">l</span></code>
, <i>etc</i>. in substitution replacements.</p>
<p>If none of the above apply, for backwards compatibility reasons, the
<code class="inline">/d</code> modifier is the one in effect by default.  As this can lead to
unexpected results, it is best to specify which other rule set should be
used.</p>
<h4>Character set modifier behavior prior to Perl 5.14</h4>
<p>Prior to 5.14, there were no explicit modifiers, but <code class="inline"><span class="q">/l</span></code>
 was implied
for regexes compiled within the scope of <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">locale</span></code>
, and <code class="inline">/d</code> was
implied otherwise.  However, interpolating a regex into a larger regex
would ignore the original compilation in favor of whatever was in effect
at the time of the second compilation.  There were a number of
inconsistencies (bugs) with the <code class="inline">/d</code> modifier, where Unicode rules
would be used when inappropriate, and vice versa.  <code class="inline">\<span class="w">p</span><span class="s">{</span><span class="s">}</span></code>
 did not imply
Unicode rules, and neither did all occurrences of <code class="inline">\<span class="w">N</span><span class="s">{</span><span class="s">}</span></code>
, until 5.12.</p>
<a name="Regular-Expressions"></a><h2>Regular Expressions</h2>
<a name="Quantifiers"></a><h3>Quantifiers</h3>
<p>Quantifiers are used when a particular portion of a pattern needs to
match a certain number (or numbers) of times.  If there isn't a
quantifier the number of times to match is exactly one.  The following
standard quantifiers are recognized:
       </p>
<pre class="verbatim"><ol><li>    <span class="i">*           Match</span> <span class="n">0</span> or <span class="w">more</span> <a class="l_k" href="functions/times.html">times</a></li><li>    +           <span class="w">Match</span> <span class="n">1</span> or <span class="w">more</span> <a class="l_k" href="functions/times.html">times</a></li><li>    ?           <span class="w">Match</span> <span class="n">1</span> or <span class="n">0</span> <a class="l_k" href="functions/times.html">times</a></li><li>    <span class="s">{</span><span class="w">n</span><span class="s">}</span>         <span class="w">Match</span> <span class="w">exactly</span> <span class="w">n</span> <a class="l_k" href="functions/times.html">times</a></li><li>    <span class="s">{</span><span class="w">n</span><span class="cm">,</span><span class="s">}</span>        <span class="w">Match</span> <span class="w">at</span> <span class="w">least</span> <span class="w">n</span> <a class="l_k" href="functions/times.html">times</a></li><li>    <span class="s">{</span><span class="w">n</span><span class="cm">,</span><span class="q">m}       Match at least n but not more than m times</span></li></ol></pre><p>(If a non-escaped curly bracket occurs in a context other than one of
the quantifiers listed above, where it does not form part of a
backslashed sequence like <code class="inline">\<span class="i">x</span><span class="s">{</span>...<span class="s">}</span></code>
, it is either a fatal syntax error,
or treated as a regular character, generally with a deprecation warning
raised.  To escape it, you can precede it with a backslash (<code class="inline"><span class="q">&quot;\{&quot;</span></code>
) or
enclose it within square brackets  (<code class="inline"><span class="q">&quot;[{]&quot;</span></code>
).
This change will allow for future syntax extensions (like making the
lower bound of a quantifier optional), and better error checking of
quantifiers).</p>
<p>The <code class="inline"><span class="q">&quot;*&quot;</span></code>
 quantifier is equivalent to <code class="inline"><span class="s">{</span><span class="n">0</span><span class="cm">,</span><span class="s">}</span></code>
, the <code class="inline"><span class="q">&quot;+&quot;</span></code>

quantifier to <code class="inline"><span class="s">{</span><span class="n">1</span><span class="cm">,</span><span class="s">}</span></code>
, and the <code class="inline"><span class="q">&quot;?&quot;</span></code>
 quantifier to <code class="inline"><span class="s">{</span><span class="n">0</span><span class="cm">,</span><span class="n">1</span><span class="s">}</span></code>
.  <i>n</i> and <i>m</i> are limited
to non-negative integral values less than a preset limit defined when perl is built.
This is usually 32766 on the most common platforms.  The actual limit can
be seen in the error message generated by code such as this:</p>
<pre class="verbatim"><ol><li>    <span class="i">$_</span> **= <span class="i">$_</span> <span class="cm">,</span> <span class="q">/ {$_} /</span> for <span class="n">2</span> .. <span class="n">42</span><span class="sc">;</span></li></ol></pre><p>By default, a quantified subpattern is "greedy", that is, it will match as
many times as possible (given a particular starting location) while still
allowing the rest of the pattern to match.  If you want it to match the
minimum number of times possible, follow the quantifier with a <code class="inline"><span class="q">&quot;?&quot;</span></code>
.  Note
that the meanings don't change, just the "greediness":
  
      </p>
<pre class="verbatim"><ol><li>    <span class="i">*?</span>        <span class="w">Match</span> <span class="n">0</span> or <span class="w">more</span> <a class="l_k" href="functions/times.html">times</a><span class="cm">,</span> not <span class="w">greedily</span></li><li>    +<span class="q">?        Match 1 or more times, not greedily</span></li><li>    <span class="q">    ?</span>?        <span class="w">Match</span> <span class="n">0</span> or <span class="n">1</span> <a class="l_k" href="functions/time.html">time</a><span class="cm">,</span> not <span class="w">greedily</span></li><li>    <span class="s">{</span><span class="w">n</span><span class="s">}</span><span class="q">?      Match exactly n times, not greedily (redundant)</span></li><li>    <span class="q">    {n,}?</span>     <span class="w">Match</span> <span class="w">at</span> <span class="w">least</span> <span class="w">n</span> <a class="l_k" href="functions/times.html">times</a><span class="cm">,</span> not <span class="w">greedily</span></li><li>    <span class="s">{</span><span class="w">n</span><span class="cm">,</span><span class="q">m}?    Match at least n but not more than m times, not greedily</span></li></ol></pre><p>Normally when a quantified subpattern does not allow the rest of the
overall pattern to match, Perl will backtrack. However, this behaviour is
sometimes undesirable. Thus Perl provides the "possessive" quantifier form
as well.</p>
<pre class="verbatim"><ol><li> <span class="i">*+</span>     <span class="w">Match</span> <span class="n">0</span> or <span class="w">more</span> <a class="l_k" href="functions/times.html">times</a> and <span class="w">give</span> <span class="w">nothing</span> <span class="w">back</span></li><li> ++     <span class="w">Match</span> <span class="n">1</span> or <span class="w">more</span> <a class="l_k" href="functions/times.html">times</a> and <span class="w">give</span> <span class="w">nothing</span> <span class="w">back</span></li><li> ?+     <span class="w">Match</span> <span class="n">0</span> or <span class="n">1</span> <a class="l_k" href="functions/time.html">time</a> and <span class="w">give</span> <span class="w">nothing</span> <span class="w">back</span></li><li> <span class="s">{</span><span class="w">n</span><span class="s">}</span>+   <span class="w">Match</span> <span class="w">exactly</span> <span class="w">n</span> <a class="l_k" href="functions/times.html">times</a> and <span class="w">give</span> <span class="w">nothing</span> <span class="w">back</span> <span class="s">(</span><span class="w">redundant</span><span class="s">)</span></li><li> <span class="s">{</span><span class="w">n</span><span class="cm">,</span><span class="s">}</span>+  <span class="w">Match</span> <span class="w">at</span> <span class="w">least</span> <span class="w">n</span> <a class="l_k" href="functions/times.html">times</a> and <span class="w">give</span> <span class="w">nothing</span> <span class="w">back</span></li><li> <span class="s">{</span><span class="w">n</span><span class="cm">,</span><span class="q">m}+ Match at least n but not more than m times and give nothing back</span></li></ol></pre><p>For instance,</p>
<pre class="verbatim"><ol><li>   <span class="q">'aaaa'</span> =~ <span class="q">/a++a/</span></li></ol></pre><p>will never match, as the <code class="inline"><span class="w">a</span>++</code>
 will gobble up all the <code class="inline"><span class="q">&quot;a&quot;</span></code>
's in the
string and won't leave any for the remaining part of the pattern. This
feature can be extremely useful to give perl hints about where it
shouldn't backtrack. For instance, the typical "match a double-quoted
string" problem can be most efficiently performed when written as:</p>
<pre class="verbatim"><ol><li>   <span class="q">/&quot;(?:[^&quot;\\]++|\\.)*+&quot;/</span></li></ol></pre><p>as we know that if the final quote does not match, backtracking will not
help. See the independent subexpression
<a href="#(%3f%3epattern)">(?&gt;pattern)</a> for more details;
possessive quantifiers are just syntactic sugar for that construct. For
instance the above example could also be written as follows:</p>
<pre class="verbatim"><ol><li>   <span class="q">/&quot;(?&gt;(?:(?&gt;[^&quot;\\]+)|\\.)*)&quot;/</span></li></ol></pre><p>Note that the possessive quantifier modifier can not be be combined
with the non-greedy modifier. This is because it would make no sense.
Consider the follow equivalency table:</p>
<pre class="verbatim"><ol><li>    <span class="w">Illegal</span>         <span class="w">Legal</span></li><li>    ------------    ------</li><li>    <span class="w">X</span>?<span class="q">?+            X{0}</span></li><li>    <span class="q">    X+?</span>+            <span class="i">X</span><span class="s">{</span><span class="n">1</span><span class="s">}</span></li><li>    <span class="w">X</span><span class="s">{</span><span class="w">min</span><span class="cm">,</span><span class="w">max</span><span class="s">}</span><span class="q">?+    X{min}</span></li></ol></pre><a name="Escape-sequences"></a><h3>Escape sequences</h3>
<p>Because patterns are processed as double-quoted strings, the following
also work:</p>
<pre class="verbatim"><ol><li> \<span class="w">t</span>          <span class="w">tab</span>                   <span class="s">(</span><span class="w">HT</span><span class="cm">,</span> <span class="w">TAB</span><span class="s">)</span></li><li> \<span class="w">n</span>          <span class="w">newline</span>               <span class="s">(</span><span class="w">LF</span><span class="cm">,</span> <span class="w">NL</span><span class="s">)</span></li><li> \<span class="w">r</span>          <a class="l_k" href="functions/return.html">return</a>                <span class="s">(</span><span class="w">CR</span><span class="s">)</span></li><li> \<span class="w">f</span>          <span class="w">form</span> <span class="w">feed</span>             <span class="s">(</span><span class="w">FF</span><span class="s">)</span></li><li> \<span class="w">a</span>          <a class="l_k" href="functions/alarm.html">alarm</a> <span class="s">(</span><span class="w">bell</span><span class="s">)</span>          <span class="s">(</span><span class="w">BEL</span><span class="s">)</span></li><li> \<span class="w">e</span>          <span class="w">escape</span> <span class="s">(</span><span class="w">think</span> <span class="w">troff</span><span class="s">)</span>  <span class="s">(</span><span class="w">ESC</span><span class="s">)</span></li><li> \<span class="w">cK</span>         <span class="w">control</span> <span class="w">char</span>          <span class="s">(</span><span class="w">example</span><span class="co">:</span> <span class="w">VT</span><span class="s">)</span></li><li> \<span class="w">x</span><span class="s">{</span><span class="s">}</span><span class="cm">,</span> \<span class="w">x00</span>  <span class="w">character</span> <span class="w">whose</span> <span class="w">ordinal</span> <span class="w">is</span> <span class="w">the</span> <a class="l_k" href="functions/given.html">given</a> <span class="w">hexadecimal</span> <span class="w">number</span></li><li> \<span class="i">N</span><span class="s">{</span><span class="w">name</span><span class="s">}</span>    <span class="w">named</span> <span class="w">Unicode</span> <span class="w">character</span> <a class="l_k" href="functions/or.html">or</a> <span class="w">character</span> <span class="w">sequence</span></li><li> \<span class="i">N</span><span class="s">{</span><span class="w">U</span>+<span class="n">263</span><span class="w">D</span><span class="s">}</span>  <span class="w">Unicode</span> <span class="w">character</span>     <span class="s">(</span><span class="w">example</span><span class="co">:</span> <span class="w">FIRST</span> <span class="w">QUARTER</span> <span class="w">MOON</span><span class="s">)</span></li><li> \<span class="w">o</span><span class="s">{</span><span class="s">}</span><span class="cm">,</span> \<span class="n">000</span>  <span class="w">character</span> <span class="w">whose</span> <span class="w">ordinal</span> <span class="w">is</span> <span class="w">the</span> <a class="l_k" href="functions/given.html">given</a> <span class="w">octal</span> <span class="w">number</span></li><li> \<span class="w">l</span>          <span class="w">lowercase</span> <a class="l_k" href="functions/next.html">next</a> <span class="j">char</span> <span class="s">(</span><span class="w">think</span> <span class="w">vi</span><span class="s">)</span></li><li> \<span class="w">u</span>          <span class="w">uppercase</span> <a class="l_k" href="functions/next.html">next</a> <span class="j">char</span> <span class="s">(</span><span class="w">think</span> <span class="w">vi</span><span class="s">)</span></li><li> \<span class="w">L</span>          <span class="w">lowercase</span> <a class="l_k" href="functions/until.html">until</a> \<span class="w">E</span> <span class="s">(</span><span class="w">think</span> <span class="w">vi</span><span class="s">)</span></li><li> \<span class="w">U</span>          <span class="w">uppercase</span> <a class="l_k" href="functions/until.html">until</a> \<span class="w">E</span> <span class="s">(</span><span class="w">think</span> <span class="w">vi</span><span class="s">)</span></li><li> \<span class="w">Q</span>          <span class="w">quote</span> <span class="s">(</span><span class="w">disable</span><span class="s">)</span> <span class="w">pattern</span> <span class="w">metacharacters</span> <a class="l_k" href="functions/until.html">until</a> \<span class="w">E</span></li><li> \<span class="w">E</span>          <span class="w">end</span> <span class="w">either</span> case <span class="w">modification</span> <a class="l_k" href="functions/or.html">or</a> <span class="w">quoted</span> <span class="w">section</span><span class="cm">,</span> <span class="w">think</span> <span class="w">vi</span></li></ol></pre><p>Details are in <a href="perlop.html#Quote-and-Quote-like-Operators">Quote and Quote-like Operators in perlop</a>.</p>
<a name="Character-Classes-and-other-Special-Escapes"></a><h3>Character Classes and other Special Escapes</h3>
<p>In addition, Perl defines the following:
   </p>
<pre class="verbatim"><ol><li> <span class="w">Sequence</span>   <span class="w">Note</span>    <span class="w">Description</span></li><li>  <span class="s">[</span>...<span class="s">]</span>     <span class="s">[</span><span class="n">1</span><span class="s">]</span>  <span class="w">Match</span> <span class="w">a</span> <span class="w">character</span> <span class="w">according</span> <span class="w">to</span> <span class="w">the</span> <span class="w">rules</span> <span class="w">of</span> <span class="w">the</span></li><li>                   <span class="w">bracketed</span> <span class="w">character</span> <span class="w">class</span> <a class="l_k" href="functions/defined.html">defined</a> <span class="w">by</span> <span class="w">the</span> <span class="q">&quot;...&quot;</span>.</li><li>                   <span class="w">Example</span><span class="co">:</span> <span class="s">[</span><span class="w">a</span>-z<span class="s">]</span> <span class="w">matches</span> <span class="q">&quot;a&quot;</span> <a class="l_k" href="functions/or.html">or</a> <span class="q">&quot;b&quot;</span> <a class="l_k" href="functions/or.html">or</a> <span class="q">&quot;c&quot;</span> ... <a class="l_k" href="functions/or.html">or</a> <span class="q">&quot;z&quot;</span></li><li>  <span class="s">[</span><span class="s">[</span><span class="co">:</span>...<span class="co">:</span><span class="s">]</span><span class="s">]</span> <span class="s">[</span><span class="n">2</span><span class="s">]</span>  <span class="w">Match</span> <span class="w">a</span> <span class="w">character</span> <span class="w">according</span> <span class="w">to</span> <span class="w">the</span> <span class="w">rules</span> <span class="w">of</span> <span class="w">the</span> <span class="w">POSIX</span></li><li>                   <span class="w">character</span> <span class="w">class</span> <span class="q">&quot;...&quot;</span> <span class="w">within</span> <span class="w">the</span> <span class="w">outer</span> <span class="w">bracketed</span></li><li>                   <span class="w">character</span> <span class="w">class</span>.  <span class="w">Example</span><span class="co">:</span> <span class="s">[</span><span class="s">[</span><span class="co">:</span><span class="w">upper</span><span class="co">:</span><span class="s">]</span><span class="s">]</span> <span class="w">matches</span> <span class="w">any</span></li><li>                   <span class="w">uppercase</span> <span class="w">character</span>.</li><li>  <span class="s">(</span><span class="q">?[...])  [8]  Extended bracketed character class</span></li><li>  <span class="q">  \w        [3]  Match a &quot;word&quot; character (alphanumeric plus &quot;_&quot;, plus</span></li><li>                   <span class="q">                   other connector punctuation chars plus Unicode</span></li><li>                   <span class="q">                   marks)</span></li><li>  <span class="q">  \W        [3]  Match a non-&quot;word&quot; character</span></li><li>  <span class="q">  \s        [3]  Match a whitespace character</span></li><li>  <span class="q">  \S        [3]  Match a non-whitespace character</span></li><li>  <span class="q">  \d        [3]  Match a decimal digit character</span></li><li>  <span class="q">  \D        [3]  Match a non-digit character</span></li><li>  <span class="q">  \pP       [3]  Match P, named property.  Use \p{Prop} for longer names</span></li><li>  <span class="q">  \PP       [3]  Match non-P</span></li><li>  <span class="q">  \X        [4]  Match Unicode &quot;eXtended grapheme cluster&quot;</span></li><li>  <span class="q">  \1        [5]  Backreference to a specific capture group or buffer.</span></li><li>                   <span class="q">                   &#39;1&#39; may actually be any positive integer.</span></li><li>  <span class="q">  \g1       [5]  Backreference to a specific or previous group,</span></li><li>  <span class="q">  \g{-1}    [5]  The number may be negative indicating a relative</span></li><li>                   <span class="q">                   previous group and may optionally be wrapped in</span></li><li>                   <span class="q">                   curly brackets for safer parsing.</span></li><li>  <span class="q">  \g{name}  [5]  Named backreference</span></li><li>  <span class="q">  \k&lt;name&gt;  [5]  Named backreference</span></li><li>  <span class="q">  \K        [6]  Keep the stuff left of the \K, don&#39;t include it in $&amp;</span></li><li>  <span class="q">  \N        [7]  Any character but \n.  Not affected by /s modifier</span></li><li>  <span class="q">  \v        [3]  Vertical whitespace</span></li><li>  <span class="q">  \V        [3]  Not vertical whitespace</span></li><li>  <span class="q">  \h        [3]  Horizontal whitespace</span></li><li>  <span class="q">  \H        [3]  Not horizontal whitespace</span></li><li>  <span class="q">  \R        [4]  Linebreak</span></li></ol></pre><ul>
<li><a name="%5b1%5d"></a><b>[1]</b>
<p>See <a href="perlrecharclass.html#Bracketed-Character-Classes">Bracketed Character Classes in perlrecharclass</a> for details.</p>
</li>
<li><a name="%5b2%5d"></a><b>[2]</b>
<p>See <a href="perlrecharclass.html#POSIX-Character-Classes">POSIX Character Classes in perlrecharclass</a> for details.</p>
</li>
<li><a name="%5b3%5d"></a><b>[3]</b>
<p>See <a href="perlrecharclass.html#Backslash-sequences">Backslash sequences in perlrecharclass</a> for details.</p>
</li>
<li><a name="%5b4%5d"></a><b>[4]</b>
<p>See <a href="perlrebackslash.html#Misc">Misc in perlrebackslash</a> for details.</p>
</li>
<li><a name="%5b5%5d"></a><b>[5]</b>
<p>See <a href="#Capture-groups">Capture groups</a> below for details.</p>
</li>
<li><a name="%5b6%5d"></a><b>[6]</b>
<p>See <a href="#Extended-Patterns">Extended Patterns</a> below for details.</p>
</li>
<li><a name="%5b7%5d"></a><b>[7]</b>
<p>Note that <code class="inline">\<span class="w">N</span></code>
 has two meanings.  When of the form <code class="inline">\<span class="i">N</span><span class="s">{</span><span class="w">NAME</span><span class="s">}</span></code>
, it matches the
character or character sequence whose name is <code class="inline"><span class="w">NAME</span></code>
; and similarly
when of the form <code class="inline">\N{U+<i>hex</i>}</code>, it matches the character whose Unicode
code point is <i>hex</i>.  Otherwise it matches any character but <code class="inline">\<span class="w">n</span></code>
.</p>
</li>
<li><a name="%5b8%5d"></a><b>[8]</b>
<p>See <a href="perlrecharclass.html#Extended-Bracketed-Character-Classes">Extended Bracketed Character Classes in perlrecharclass</a> for details.</p>
</li>
</ul>
<a name="Assertions"></a><h3>Assertions</h3>
<p>Besides <a href="#Metacharacters">^ and &quot;$&quot; </a>, Perl defines the following
zero-width assertions:
  


     </p>
<pre class="verbatim"><ol><li> \<span class="w">b</span><span class="s">{</span><span class="s">}</span>   <span class="w">Match</span> <span class="w">at</span> <span class="w">Unicode</span> <span class="w">boundary</span> <span class="w">of</span> <span class="w">specified</span> <span class="w">type</span></li><li> \<span class="w">B</span><span class="s">{</span><span class="s">}</span>   <span class="w">Match</span> <span class="w">where</span> <span class="w">corresponding</span> \<span class="w">b</span><span class="s">{</span><span class="s">}</span> <span class="w">doesn&#39;t</span> <span class="w">match</span></li><li> \<span class="w">b</span>     <span class="w">Match</span> <span class="w">a</span> \<span class="w">w</span>\<span class="w">W</span> <a class="l_k" href="functions/or.html">or</a> \<span class="w">W</span>\<span class="w">w</span> <span class="w">boundary</span></li><li> \<span class="w">B</span>     <span class="w">Match</span> <span class="w">except</span> <span class="w">at</span> <span class="w">a</span> \<span class="w">w</span>\<span class="w">W</span> <a class="l_k" href="functions/or.html">or</a> \<span class="w">W</span>\<span class="w">w</span> <span class="w">boundary</span></li><li> \<span class="w">A</span>     <span class="w">Match</span> <span class="w">only</span> <span class="w">at</span> <span class="w">beginning</span> <span class="w">of</span> <span class="w">string</span></li><li> \<span class="w">Z</span>     <span class="w">Match</span> <span class="w">only</span> <span class="w">at</span> <span class="w">end</span> <span class="w">of</span> <span class="w">string</span><span class="cm">,</span> <a class="l_k" href="functions/or.html">or</a> <span class="w">before</span> <span class="w">newline</span> <span class="w">at</span> <span class="w">the</span> <span class="w">end</span></li><li> \<span class="w">z</span>     <span class="w">Match</span> <span class="w">only</span> <span class="w">at</span> <span class="w">end</span> <span class="w">of</span> <span class="w">string</span></li><li> \<span class="w">G</span>     <span class="w">Match</span> <span class="w">only</span> <span class="w">at</span> <a class="l_k" href="functions/pos.html">pos</a><span class="s">(</span><span class="s">)</span> <span class="s">(</span><span class="w">e</span>.<span class="w">g</span>. <span class="w">at</span> <span class="w">the</span> <span class="w">end</span>-<span class="w">of</span>-<span class="w">match</span> <span class="w">position</span></li><li>        <span class="w">of</span> <span class="w">prior</span> <span class="q">m//g</span><span class="s">)</span></li></ol></pre><p>A Unicode boundary (<code class="inline">\<span class="w">b</span><span class="s">{</span><span class="s">}</span></code>
), available starting in v5.22, is a spot
between two characters, or before the first character in the string, or
after the final character in the string where certain criteria defined
by Unicode are met.  See <a href="perlrebackslash.html#%5cb%7b%7d%2c-%5cb%2c-%5cB%7b%7d%2c-%5cB">\b{}, \b, \B{}, \B in perlrebackslash</a> for
details.</p>
<p>A word boundary (<code class="inline">\<span class="w">b</span></code>
) is a spot between two characters
that has a <code class="inline">\<span class="w">w</span></code>
 on one side of it and a <code class="inline">\<span class="w">W</span></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 class="inline">\<span class="w">W</span></code>
.  (Within
character classes <code class="inline">\<span class="w">b</span></code>
 represents backspace rather than a word
boundary, just as it normally does in any double-quoted string.)
The <code class="inline">\<span class="w">A</span></code>
 and <code class="inline">\<span class="w">Z</span></code>
 are just like <code class="inline"><span class="q">&quot;^&quot;</span></code>
 and <code class="inline"><span class="q">&quot;$&quot;</span></code>
, except that they
won't match multiple times when the <code class="inline">/m</code> modifier is used, while
<code class="inline"><span class="q">&quot;^&quot;</span></code>
 and <code class="inline"><span class="q">&quot;$&quot;</span></code>
 will match at every internal line boundary.  To match
the actual end of the string and not ignore an optional trailing
newline, use <code class="inline">\<span class="w">z</span></code>
.
    </p>
<p>The <code class="inline">\<span class="w">G</span></code>
 assertion can be used to chain global matches (using
<code class="inline"><a class="l_k" href="functions/m.html">m//g</a></code>), as described in <a href="perlop.html#Regexp-Quote-Like-Operators">Regexp Quote-Like Operators in perlop</a>.
It is also useful when writing <code class="inline"><span class="w">lex</span></code>
-like scanners, when you have
several patterns that you want to match against consequent substrings
of your string; see the previous reference.  The actual location
where <code class="inline">\<span class="w">G</span></code>
 will match can also be influenced by using <code class="inline"><a class="l_k" href="functions/pos.html">pos()</a></code> as
an lvalue: see <a href="functions/pos.html">pos</a>. Note that the rule for zero-length
matches (see <a href="#Repeated-Patterns-Matching-a-Zero-length-Substring">Repeated Patterns Matching a Zero-length Substring</a>)
is modified somewhat, in that contents to the left of <code class="inline">\<span class="w">G</span></code>
 are
not counted when determining the length of the match. Thus the following
will not match forever:
</p>
<pre class="verbatim"><ol><li>     <a class="l_k" href="functions/my.html">my</a> <span class="i">$string</span> = <span class="q">&#39;ABC&#39;</span><span class="sc">;</span></li><li>     <a class="l_k" href="functions/pos.html">pos</a><span class="s">(</span><span class="i">$string</span><span class="s">)</span> = <span class="n">1</span><span class="sc">;</span></li><li>     while <span class="s">(</span><span class="i">$string</span> =~ <span class="q">/(.\G)/g</span><span class="s">)</span> <span class="s">{</span></li><li>         <a class="l_k" href="functions/print.html">print</a> <span class="i">$1</span><span class="sc">;</span></li><li>     <span class="s">}</span></li></ol></pre><p>It will print 'A' and then terminate, as it considers the match to
be zero-width, and thus will not match at the same position twice in a
row.</p>
<p>It is worth noting that <code class="inline">\<span class="w">G</span></code>
 improperly used can result in an infinite
loop. Take care when using patterns that include <code class="inline">\<span class="w">G</span></code>
 in an alternation.</p>
<p>Note also that <code class="inline"><a class="l_k" href="functions/s.html">s///</a></code> will refuse to overwrite part of a substitution
that has already been replaced; so for example this will stop after the
first iteration, rather than iterating its way backwards through the
string:</p>
<pre class="verbatim"><ol><li>    <span class="i">$_</span> = <span class="q">&quot;123456789&quot;</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/pos.html">pos</a> = <span class="n">6</span><span class="sc">;</span></li><li>    <span class="q">s/.(?=.\G)/X/g</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/print.html">print</a><span class="sc">;</span> 	<span class="c"># prints 1234X6789, not XXXXX6789</span></li></ol></pre><a name="Capture-groups"></a><h3>Capture groups</h3>
<p>The grouping construct <code class="inline"><span class="s">(</span> ... <span class="s">)</span></code>
 creates capture groups (also referred to as
capture buffers). To refer to the current contents of a group later on, within
the same pattern, use <code class="inline">\<span class="w">g1</span></code>
 (or <code class="inline">\<span class="i">g</span><span class="s">{</span><span class="n">1</span><span class="s">}</span></code>
) for the first, <code class="inline">\<span class="w">g2</span></code>
 (or <code class="inline">\<span class="i">g</span><span class="s">{</span><span class="n">2</span><span class="s">}</span></code>
)
for the second, and so on.
This is called a <i>backreference</i>.
 
 
 
 
    
 
 
  
There is no limit to the number of captured substrings that you may use.
Groups are numbered with the leftmost open parenthesis being number 1, <i>etc</i>.  If
a group did not match, the associated backreference won't match either. (This
can happen if the group is optional, or in a different branch of an
alternation.)
You can omit the <code class="inline"><span class="q">&quot;g&quot;</span></code>
, and write <code class="inline"><span class="q">&quot;\1&quot;</span></code>
, <i>etc</i>, but there are some issues with
this form, described below.</p>
<p>You can also refer to capture groups relatively, by using a negative number, so
that <code class="inline">\<span class="w">g</span>-<span class="n">1</span></code>
 and <code class="inline">\<span class="i">g</span><span class="s">{</span><span class="n">-1</span><span class="s">}</span></code>
 both refer to the immediately preceding capture
group, and <code class="inline">\<span class="w">g</span>-<span class="n">2</span></code>
 and <code class="inline">\<span class="i">g</span><span class="s">{</span><span class="n">-2</span><span class="s">}</span></code>
 both refer to the group before it.  For
example:</p>
<pre class="verbatim"><ol><li>        <span class="q">/</span></li><li>         <span class="q">         (Y)            # group 1</span></li><li>         <span class="q">         (              # group 2</span></li><li>            <span class="q">            (X)         # group 3</span></li><li>            <span class="q">            \g{-1}      # backref to group 3</span></li><li>            <span class="q">            \g{-3}      # backref to group 1</span></li><li>         <span class="q">         )</span></li><li>        <span class="q">        /x</span></li></ol></pre><p>would match the same as <code class="inline"><span class="q">/(Y) ( (X) \g3 \g1 )/x</span></code>
.  This allows you to
interpolate regexes into larger regexes and not have to worry about the
capture groups being renumbered.</p>
<p>You can dispense with numbers altogether and create named capture groups.
The notation is <code class="inline">(?&lt;<i>name</i>&gt;...)</code> to declare and <code class="inline">\g{<i>name</i>}</code> to
reference.  (To be compatible with .Net regular expressions, <code class="inline">\g{<i>name</i>}</code> may
also be written as <code class="inline">\k{<i>name</i>}</code>, <code class="inline">\k&lt;<i>name</i>&gt;</code> or <code class="inline">\k'<i>name</i>'</code>.)
<i>name</i> must not begin with a number, nor contain hyphens.
When different groups within the same pattern have the same name, any reference
to that name assumes the leftmost defined group.  Named groups count in
absolute and relative numbering, and so can also be referred to by those
numbers.
(It's possible to do things with named capture groups that would otherwise
require <code class="inline"><span class="s">(</span><span class="q">??</span><span class="s">{</span><span class="s">}</span><span class="s">)</span></code>
.)</p>
<p>Capture group contents are dynamically scoped and available to you outside the
pattern until the end of the enclosing block or until the next successful
match, whichever comes first.  (See <a href="perlsyn.html#Compound-Statements">Compound Statements in perlsyn</a>.)
You can refer to them by absolute number (using <code class="inline"><span class="q">&quot;$1&quot;</span></code>
 instead of <code class="inline"><span class="q">&quot;\g1&quot;</span></code>
,
<i>etc</i>); or by name via the <code class="inline"><span class="i">%+</span></code>
 hash, using <code class="inline">"$+{<i>name</i>}"</code>.</p>
<p>Braces are required in referring to named capture groups, but are optional for
absolute or relative numbered ones.  Braces are safer when creating a regex by
concatenating smaller strings.  For example if you have <code class="inline"><a class="l_k" href="functions/qr.html">qr/$a$b/</a></code>, and <code class="inline"><span class="i">$a</span></code>

contained <code class="inline"><span class="q">&quot;\g1&quot;</span></code>
, and <code class="inline"><span class="i">$b</span></code>
 contained <code class="inline"><span class="q">&quot;37&quot;</span></code>
, you would get <code class="inline"><span class="q">/\g137/</span></code>
 which
is probably not what you intended.</p>
<p>The <code class="inline">\<span class="w">g</span></code>
 and <code class="inline">\<span class="w">k</span></code>
 notations were introduced in Perl 5.10.0.  Prior to that
there were no named nor relative numbered capture groups.  Absolute numbered
groups were referred to using <code class="inline">\<span class="n">1</span></code>
,
<code class="inline">\<span class="n">2</span></code>
, <i>etc</i>., and this notation is still
accepted (and likely always will be).  But it leads to some ambiguities if
there are more than 9 capture groups, as <code class="inline">\<span class="n">10</span></code>
 could mean either the tenth
capture group, or the character whose ordinal in octal is 010 (a backspace in
ASCII).  Perl resolves this ambiguity by interpreting <code class="inline">\<span class="n">10</span></code>
 as a backreference
only if at least 10 left parentheses have opened before it.  Likewise <code class="inline">\<span class="n">11</span></code>
 is
a backreference only if at least 11 left parentheses have opened before it.
And so on.  <code class="inline">\<span class="n">1</span></code>
 through <code class="inline">\<span class="n">9</span></code>
 are always interpreted as backreferences.
There are several examples below that illustrate these perils.  You can avoid
the ambiguity by always using <code class="inline">\<span class="w">g</span><span class="s">{</span><span class="s">}</span></code>
 or <code class="inline">\<span class="w">g</span></code>
 if you mean capturing groups;
and for octal constants always using <code class="inline">\<span class="w">o</span><span class="s">{</span><span class="s">}</span></code>
, or for <code class="inline">\<span class="n">077</span></code>
 and below, using 3
digits padded with leading zeros, since a leading zero implies an octal
constant.</p>
<p>The <code class="inline">\<i>digit</i></code> notation also works in certain circumstances outside
the pattern.  See <a href="#Warning-on-%5c1-Instead-of-%241">Warning on \1 Instead of $1</a> below for details.</p>
<p>Examples:</p>
<pre class="verbatim"><ol><li>    <span class="q">s/^([^ ]*) *([^ ]*)/$2 $1/</span><span class="sc">;</span>     <span class="c"># swap first two words</span></li><li></li><li>    <span class="q">/(.)\g1/</span>                        <span class="c"># find first doubled char</span></li><li>         and <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;&#39;$1&#39; is the first doubled character\n&quot;</span><span class="sc">;</span></li><li></li><li>    <span class="q">/(?&lt;char&gt;.)\k&lt;char&gt;/</span>            <span class="c"># ... a different way</span></li><li>         and <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;&#39;$+{char}&#39; is the first doubled character\n&quot;</span><span class="sc">;</span></li><li></li><li>    <span class="q">/(?&#39;char&#39;.)\g1/</span>                 <span class="c"># ... mix and match</span></li><li>         and <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;&#39;$1&#39; is the first doubled character\n&quot;</span><span class="sc">;</span></li><li></li><li>    if <span class="s">(</span><span class="q">/Time: (..):(..):(..)/</span><span class="s">)</span> <span class="s">{</span>   <span class="c"># parse out values</span></li><li>        <span class="i">$hours</span> = <span class="i">$1</span><span class="sc">;</span></li><li>        <span class="i">$minutes</span> = <span class="i">$2</span><span class="sc">;</span></li><li>        <span class="i">$seconds</span> = <span class="i">$3</span><span class="sc">;</span></li><li>    <span class="s">}</span></li><li></li><li>    <span class="q">/(.)(.)(.)(.)(.)(.)(.)(.)(.)\g10/</span>   <span class="c"># \g10 is a backreference</span></li><li>    /<span class="s">(</span>.<span class="s">)</span><span class="s">(</span>.<span class="s">)</span><span class="s">(</span>.<span class="s">)</span><span class="s">(</span>.<span class="s">)</span><span class="s">(</span>.<span class="s">)</span><span class="s">(</span>.<span class="s">)</span><span class="s">(</span>.<span class="s">)</span><span class="s">(</span>.<span class="s">)</span><span class="s">(</span>.<span class="s">)</span>\<span class="n">10</span>/    <span class="c"># \10 is octal</span></li><li>    <span class="q">/((.)(.)(.)(.)(.)(.)(.)(.)(.))\10/</span>  <span class="c"># \10 is a backreference</span></li><li>    /<span class="s">(</span><span class="s">(</span>.<span class="s">)</span><span class="s">(</span>.<span class="s">)</span><span class="s">(</span>.<span class="s">)</span><span class="s">(</span>.<span class="s">)</span><span class="s">(</span>.<span class="s">)</span><span class="s">(</span>.<span class="s">)</span><span class="s">(</span>.<span class="s">)</span><span class="s">(</span>.<span class="s">)</span><span class="s">(</span>.<span class="s">)</span><span class="s">)</span>\<span class="n">010</span>/ <span class="c"># \010 is octal</span></li><li></li><li>    <span class="i">$a</span> = <span class="q">&#39;(.)\1&#39;</span><span class="sc">;</span>        <span class="c"># Creates problems when concatenated.</span></li><li>    <span class="i">$b</span> = <span class="q">&#39;(.)\g{1}&#39;</span><span class="sc">;</span>     <span class="c"># Avoids the problems.</span></li><li>    <span class="q">&quot;aa&quot;</span> =~ <span class="q">/${a}/</span><span class="sc">;</span>      <span class="c"># True</span></li><li>    <span class="q">&quot;aa&quot;</span> =~ <span class="q">/${b}/</span><span class="sc">;</span>      <span class="c"># True</span></li><li>    <span class="q">&quot;aa0&quot;</span> =~ <span class="q">/${a}0/</span><span class="sc">;</span>    <span class="c"># False!</span></li><li>    <span class="q">&quot;aa0&quot;</span> =~ <span class="q">/${b}0/</span><span class="sc">;</span>    <span class="c"># True</span></li><li>    <span class="q">&quot;aa\x08&quot;</span> =~ <span class="q">/${a}0/</span><span class="sc">;</span>  <span class="c"># True!</span></li><li>    <span class="q">&quot;aa\x08&quot;</span> =~ <span class="q">/${b}0/</span><span class="sc">;</span>  <span class="c"># False</span></li></ol></pre><p>Several special variables also refer back to portions of the previous
match.  <code class="inline"><span class="i">$+</span></code>
 returns whatever the last bracket match matched.
<code class="inline"><span class="i">$&amp;</span></code>
 returns the entire matched string.  (At one point <code class="inline"><span class="i">$0</span></code>
 did
also, but now it returns the name of the program.)  <code class="inline"><span class="i">$`</span></code>
 returns
everything before the matched string.  <code class="inline"><span class="i">$&#39;</span></code>
 returns everything
after the matched string. And <code class="inline"><span class="i">$^N</span></code>
 contains whatever was matched by
the most-recently closed group (submatch). <code class="inline"><span class="i">$^N</span></code>
 can be used in
extended patterns (see below), for example to assign a submatch to a
variable.
    </p>
<p>These special variables, like the <code class="inline"><span class="i">%+</span></code>
 hash and the numbered match variables
(<code class="inline"><span class="i">$1</span></code>
, <code class="inline"><span class="i">$2</span></code>
, <code class="inline"><span class="i">$3</span></code>
, <i>etc</i>.) are dynamically scoped
until the end of the enclosing block or until the next successful
match, whichever comes first.  (See <a href="perlsyn.html#Compound-Statements">Compound Statements in perlsyn</a>.)
    
        </p>
<p><b>NOTE</b>: Failed matches in Perl do not reset the match variables,
which makes it easier to write code that tests for a series of more
specific cases and remembers the best match.</p>
<p><b>WARNING</b>: If your code is to run on Perl 5.16 or earlier,
beware that once Perl sees that you need one of <code class="inline"><span class="i">$&amp;</span></code>
, <code class="inline"><span class="i">$`</span></code>
, or
<code class="inline"><span class="i">$&#39;</span></code>
 anywhere in the program, it has to provide them for every
pattern match.  This may substantially slow your program.</p>
<p>Perl uses the same mechanism to produce <code class="inline"><span class="i">$1</span></code>
, <code class="inline"><span class="i">$2</span></code>
, <i>etc</i>, so you also
pay a price for each pattern that contains capturing parentheses.
(To avoid this cost while retaining the grouping behaviour, use the
extended regular expression <code class="inline">(?: ... )</code> instead.)  But if you never
use <code class="inline"><span class="i">$&amp;</span></code>
, <code class="inline"><span class="i">$`</span></code>
 or <code class="inline"><span class="i">$&#39;</span></code>
, then patterns <i>without</i> capturing
parentheses will not be penalized.  So avoid <code class="inline"><span class="i">$&amp;</span></code>
, <code class="inline"><span class="i">$&#39;</span></code>
, and <code class="inline"><span class="i">$`</span></code>

if you can, but if you can't (and some algorithms really appreciate
them), once you've used them once, use them at will, because you've
already paid the price.
  </p>
<p>Perl 5.16 introduced a slightly more efficient mechanism that notes
separately whether each of <code class="inline"><span class="i">$`</span></code>
, <code class="inline"><span class="i">$&amp;</span></code>
, and <code class="inline"><span class="i">$&#39;</span></code>
 have been seen, and
thus may only need to copy part of the string.  Perl 5.20 introduced a
much more efficient copy-on-write mechanism which eliminates any slowdown.</p>
<p>As another workaround for this problem, Perl 5.10.0 introduced <code class="inline"><span class="i">$</span>{<span class="w">^PREMATCH</span>}</code>
,
<code class="inline"><span class="i">$</span>{<span class="w">^MATCH</span>}</code>
 and <code class="inline"><span class="i">$</span>{<span class="w">^POSTMATCH</span>}</code>
, which are equivalent to <code class="inline"><span class="i">$`</span></code>
, <code class="inline"><span class="i">$&amp;</span></code>

and <code class="inline"><span class="i">$&#39;</span></code>
, <b>except</b> that they are only guaranteed to be defined after a
successful match that was executed with the <code class="inline">/p</code> (preserve) modifier.
The use of these variables incurs no global performance penalty, unlike
their punctuation character equivalents, however at the trade-off that you
have to tell perl when you want to use them.  As of Perl 5.20, these three
variables are equivalent to <code class="inline"><span class="i">$`</span></code>
, <code class="inline"><span class="i">$&amp;</span></code>
 and <code class="inline"><span class="i">$&#39;</span></code>
, and <code class="inline">/p</code> is ignored.
 </p>
<a name="Quoting-metacharacters"></a><h2>Quoting metacharacters</h2>
<p>Backslashed metacharacters in Perl are alphanumeric, such as <code class="inline">\<span class="w">b</span></code>
,
<code class="inline">\<span class="w">w</span></code>
, <code class="inline">\<span class="w">n</span></code>
.  Unlike some other regular expression languages, there
are no backslashed symbols that aren't alphanumeric.  So anything
that looks like <code class="inline">\\</code>
, <code class="inline">\<span class="s">(</span></code>
, <code class="inline">\<span class="p">)</span></code>
, <code class="inline">\[</code>, <code class="inline">\]</code>, <code class="inline">\{</code>, or <code class="inline">\<span class="s">}</span></code>
 is
always
interpreted as a literal character, not a metacharacter.  This was
once used in a common idiom to disable or quote the special meanings
of regular expression metacharacters in a string that you want to
use for a pattern. Simply quote all non-"word" characters:</p>
<pre class="verbatim"><ol><li>    <span class="i">$pattern</span> =~ <span class="q">s/(\W)/\\$1/g</span><span class="sc">;</span></li></ol></pre><p>(If <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">locale</span></code>
 is set, then this depends on the current locale.)
Today it is more common to use the <code class="inline"><a href="functions/quotemeta.html">quotemeta</a></code>
function or the <code class="inline">\<span class="w">Q</span></code>
 metaquoting escape sequence to disable all
metacharacters' special meanings like this:</p>
<pre class="verbatim"><ol><li>    <span class="q">/$unquoted\Q$quoted\E$unquoted/</span></li></ol></pre><p>Beware that if you put literal backslashes (those not inside
interpolated variables) between <code class="inline">\<span class="w">Q</span></code>
 and <code class="inline">\<span class="w">E</span></code>
, double-quotish
backslash interpolation may lead to confusing results.  If you
<i>need</i> to use literal backslashes within <code class="inline">\<span class="w">Q</span>...\<span class="w">E</span></code>
,
consult <a href="perlop.html#Gory-details-of-parsing-quoted-constructs">Gory details of parsing quoted constructs in perlop</a>.</p>
<p><code class="inline"><a class="l_k" href="functions/quotemeta.html">quotemeta()</a></code> and <code class="inline">\<span class="w">Q</span></code>
 are fully described in <a href="functions/quotemeta.html">quotemeta</a>.</p>
<a name="Extended-Patterns"></a><h2>Extended Patterns</h2>
<p>Perl also defines a consistent extension syntax for features not
found in standard tools like <b>awk</b> and
<b>lex</b>.  The syntax for most of these is a
pair of parentheses with a question mark as the first thing within
the parentheses.  The character after the question mark indicates
the extension.</p>
<p>A question mark was chosen for this and for the minimal-matching
construct because 1) question marks are rare in older regular
expressions, and 2) whenever you see one, you should stop and
"question" exactly what is going on.  That's psychology....</p>
<ul>
<li><a name="(%3f%23text)"></a><b><code class="inline">(?#text)</code>
</b>
<p>A comment.  The text is ignored.
Note that Perl closes
the comment as soon as it sees a <code class="inline"><span class="q">&quot;)&quot;</span></code>
, so there is no way to put a literal
<code class="inline"><span class="q">&quot;)&quot;</span></code>
 in the comment.  The pattern's closing delimiter must be escaped by
a backslash if it appears in the comment.</p>
<p>See <a href="#%2fx">/x</a> for another way to have comments in patterns.</p>
<p>Note that a comment can go just about anywhere, except in the middle of
an escape sequence.   Examples:</p>
<pre class="verbatim"><ol><li> <span class="q">qr/foo(?#comment)bar/</span><span class="q">&#39;  # Matches &#39;</span><span class="w">foobar</span><span class="q">&#39;</span></li><li></li><li> <span class="q"> # The pattern below matches &#39;</span><span class="w">abcd</span><span class="q">&#39;, &#39;</span><span class="w">abccd</span><span class="q">&#39;, or &#39;</span><span class="w">abcccd</span><span class="q">&#39;</span></li><li> <span class="q"> qr/abc(?#comment between literal and its quantifier){1,3}d/</span></li><li></li><li> <span class="q"> # The pattern below generates a syntax error, because the &#39;</span>\<span class="w">p</span><span class="q">&#39; must</span></li><li> <span class="q"> # be followed immediately by a &#39;</span><span class="s">{</span><span class="q">&#39;.</span></li><li> <span class="q"> qr/\p(?#comment between \p and its property name){Any}/</span></li><li></li><li> <span class="q"> # The pattern below generates a syntax error, because the initial</span></li><li> <span class="q"> # &#39;</span>\<span class="s">(</span><span class="q">&#39; is a literal opening parenthesis, and so there is nothing</span></li><li> <span class="q"> # for the  closing &#39;</span><span class="s">)</span><span class="q">&#39; to match</span></li><li> <span class="q"> qr/\(?#the backslash means this isn&#39;</span><span class="w">t</span> <span class="w">a</span> <span class="w">comment</span><span class="p">)</span><span class="i">p</span><span class="s">{</span><span class="w">Any</span><span class="s">}</span><span class="q">/</span></li></ol></pre></li>
<li><a name="(%3fadlupimnsx-imnsx)"></a><b><code class="inline"><span class="s">(</span><span class="q">?adlupimnsx-imnsx)</span></code>
</b>
</li>
<li><a name="(%3f%5ealupimnsx)"></a><b><code class="inline"><span class="s">(</span><span class="q">?^alupimnsx)</span></code>

 </b>
<p>One or more embedded pattern-match modifiers, to be turned on (or
turned off if preceded by <code class="inline"><span class="q">&quot;-&quot;</span></code>
) for the remainder of the pattern or
the remainder of the enclosing pattern group (if any).</p>
<p>This is particularly useful for dynamically-generated patterns,
such as those read in from a
configuration file, taken from an argument, or specified in a table
somewhere.  Consider the case where some patterns want to be
case-sensitive and some do not:  The case-insensitive ones merely need to
include <code class="inline">(?i)</code> at the front of the pattern.  For example:</p>
<pre class="verbatim"><ol><li>    <span class="i">$pattern</span> = <span class="q">&quot;foobar&quot;</span><span class="sc">;</span></li><li>    if <span class="s">(</span> <span class="q">/$pattern/i</span> <span class="s">)</span> <span class="s">{</span> <span class="s">}</span></li><li></li><li>    <span class="c"># more flexible:</span></li><li></li><li>    <span class="i">$pattern</span> = <span class="q">&quot;(?i)foobar&quot;</span><span class="sc">;</span></li><li>    if <span class="s">(</span> <span class="q">/$pattern/</span> <span class="s">)</span> <span class="s">{</span> <span class="s">}</span></li></ol></pre><p>These modifiers are restored at the end of the enclosing group. For example,</p>
<pre class="verbatim"><ol><li>    <span class="s">(</span> <span class="s">(</span><span class="q">?i) blah ) \s+ \g1</span></li></ol></pre><p>will match <code class="inline"><span class="w">blah</span></code>
 in any case, some spaces, and an exact (<i>including the case</i>!)
repetition of the previous word, assuming the <code class="inline">/x</code> modifier, and no <code class="inline">/i</code>
modifier outside this group.</p>
<p>These modifiers do not carry over into named subpatterns called in the
enclosing group. In other words, a pattern such as <code class="inline"><span class="s">(</span><span class="s">(</span><span class="q">?i)(?</span>&amp;<span class="w">NAME</span><span class="s">)</span><span class="s">)</span></code>
 does not
change the case-sensitivity of the <code class="inline"><span class="q">&quot;NAME&quot;</span></code>
 pattern.</p>
<p>A modifier is overridden by later occurrences of this construct in the
same scope containing the same modifier, so that</p>
<pre class="verbatim"><ol><li>    <span class="q">/((?im)foo(?-m)bar)/</span></li></ol></pre><p>matches all of <code class="inline"><span class="w">foobar</span></code>
 case insensitively, but uses <code class="inline">/m</code> rules for
only the <code class="inline"><span class="w">foo</span></code>
 portion.  The <code class="inline"><span class="q">&quot;a&quot;</span></code>
 flag overrides <code class="inline"><span class="w">aa</span></code>
 as well;
likewise <code class="inline"><span class="w">aa</span></code>
 overrides <code class="inline"><span class="q">&quot;a&quot;</span></code>
.  The same goes for <code class="inline"><span class="q">&quot;x&quot;</span></code>
 and <code class="inline"><span class="w">xx</span></code>
.
Hence, in</p>
<pre class="verbatim"><ol><li>    <span class="q">/(?-x)foo/xx</span></li></ol></pre><p>both <code class="inline">/x</code> and <code class="inline"><span class="q">/xx</span></code>
 are turned off during matching <code class="inline"><span class="w">foo</span></code>
.  And in</p>
<pre class="verbatim"><ol><li>    <span class="q">/(?x)foo/x</span></li></ol></pre><p><code class="inline">/x</code> but NOT <code class="inline"><span class="q">/xx</span></code>
 is turned on for matching <code class="inline"><span class="w">foo</span></code>
.  (One might
mistakenly think that since the inner <code class="inline">(?x)</code> is already in the scope of
<code class="inline">/x</code>, that the result would effectively be the sum of them, yielding
<code class="inline"><span class="q">/xx</span></code>
.  It doesn't work that way.)  Similarly, doing something like
<code class="inline"><span class="s">(</span><span class="q">?xx-x)foo</span></code>
 turns off all <code class="inline"><span class="q">&quot;x&quot;</span></code>
 behavior for matching <code class="inline"><span class="w">foo</span></code>
, it is not
that you subtract 1 <code class="inline"><span class="q">&quot;x&quot;</span></code>
 from 2 to get 1 <code class="inline"><span class="q">&quot;x&quot;</span></code>
 remaining.</p>
<p>Any of these modifiers can be set to apply globally to all regular
expressions compiled within the scope of a <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">re</span></code>
.  See
<a href="re.html#'%2fflags'-mode">'/flags' mode in re</a>.</p>
<p>Starting in Perl 5.14, a <code class="inline"><span class="q">&quot;^&quot;</span></code>
 (caret or circumflex accent) immediately
after the <code class="inline"><span class="q">&quot;?&quot;</span></code>
 is a shorthand equivalent to <code class="inline"><span class="w">d</span>-<span class="w">imnsx</span></code>
.  Flags (except
<code class="inline"><span class="q">&quot;d&quot;</span></code>
) may follow the caret to override it.
But a minus sign is not legal with it.</p>
<p>Note that the <code class="inline"><span class="q">&quot;a&quot;</span></code>
, <code class="inline"><span class="q">&quot;d&quot;</span></code>
, <code class="inline"><span class="q">&quot;l&quot;</span></code>
, <code class="inline"><span class="q">&quot;p&quot;</span></code>
, and <code class="inline"><span class="q">&quot;u&quot;</span></code>
 modifiers are special in
that they can only be enabled, not disabled, and the <code class="inline"><span class="q">&quot;a&quot;</span></code>
, <code class="inline"><span class="q">&quot;d&quot;</span></code>
, <code class="inline"><span class="q">&quot;l&quot;</span></code>
, and
<code class="inline"><span class="q">&quot;u&quot;</span></code>
 modifiers are mutually exclusive: specifying one de-specifies the
others, and a maximum of one (or two <code class="inline"><span class="q">&quot;a&quot;</span></code>
's) may appear in the
construct.  Thus, for
example, <code class="inline">(?-p)</code> will warn when compiled under <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">warnings</span></code>
;
<code class="inline"><span class="s">(</span><span class="q">?-d:...)</span></code>
 and <code class="inline"><span class="s">(</span><span class="q">?dl:...)</span></code>
 are fatal errors.</p>
<p>Note also that the <code class="inline"><span class="q">&quot;p&quot;</span></code>
 modifier is special in that its presence
anywhere in a pattern has a global effect.</p>
</li>
<li><a name="(%3f%3apattern)"></a><b><code class="inline">(?:pattern)</code>
</b>
</li>
<li><a name="(%3fadluimnsx-imnsx%3apattern)"></a><b><code class="inline"><span class="s">(</span><span class="q">?adluimnsx-imnsx:pattern)</span></code>
</b>
</li>
<li><a name="(%3f%5ealuimnsx%3apattern)"></a><b><code class="inline"><span class="s">(</span><span class="q">?^aluimnsx:pattern)</span></code>

</b>
<p>This is for clustering, not capturing; it groups subexpressions like
<code class="inline"><span class="q">&quot;()&quot;</span></code>
, but doesn't make backreferences as <code class="inline"><span class="q">&quot;()&quot;</span></code>
 does.  So</p>
<pre class="verbatim"><ol><li>    <span class="i">@fields</span> = <a class="l_k" href="functions/split.html">split</a><span class="s">(</span><span class="q">/\b(?:a|b|c)\b/</span><span class="s">)</span></li></ol></pre><p>matches the same field delimiters as</p>
<pre class="verbatim"><ol><li>    <span class="i">@fields</span> = <a class="l_k" href="functions/split.html">split</a><span class="s">(</span><span class="q">/\b(a|b|c)\b/</span><span class="s">)</span></li></ol></pre><p>but doesn't spit out the delimiters themselves as extra fields (even though
that's the behaviour of <a href="functions/split.html">split</a> when its pattern contains capturing
groups).  It's also cheaper not to capture
characters if you don't need to.</p>
<p>Any letters between <code class="inline"><span class="q">&quot;?&quot;</span></code>
 and <code class="inline"><span class="q">&quot;:&quot;</span></code>
 act as flags modifiers as with
<code class="inline"><span class="s">(</span><span class="q">?adluimnsx-imnsx)</span></code>
.  For example,</p>
<pre class="verbatim"><ol><li>    <span class="q">/(?s-i:more.*than).*million/i</span></li></ol></pre><p>is equivalent to the more verbose</p>
<pre class="verbatim"><ol><li>    <span class="q">/(?:(?s-i)more.*than).*million/i</span></li></ol></pre><p>Note that any <code class="inline"><span class="s">(</span><span class="s">)</span></code>
 constructs enclosed within this one will still
capture unless the <code class="inline"><span class="q">/n</span></code>
 modifier is in effect.</p>
<p>Like the <a href="#(%3fadlupimnsx-imnsx)">(?adlupimnsx-imnsx)</a> construct, <code class="inline"><span class="w">aa</span></code>
 and <code class="inline"><span class="q">&quot;a&quot;</span></code>
 override each
other, as do <code class="inline"><span class="w">xx</span></code>
 and <code class="inline"><span class="q">&quot;x&quot;</span></code>
.  They are not additive.  So, doing
something like <code class="inline"><span class="s">(</span><span class="q">?xx-x:foo)</span></code>
 turns off all <code class="inline"><span class="q">&quot;x&quot;</span></code>
 behavior for matching
<code class="inline"><span class="w">foo</span></code>
.</p>
<p>Starting in Perl 5.14, a <code class="inline"><span class="q">&quot;^&quot;</span></code>
 (caret or circumflex accent) immediately
after the <code class="inline"><span class="q">&quot;?&quot;</span></code>
 is a shorthand equivalent to <code class="inline"><span class="w">d</span>-<span class="w">imnsx</span></code>
.  Any positive
flags (except <code class="inline"><span class="q">&quot;d&quot;</span></code>
) may follow the caret, so</p>
<pre class="verbatim"><ol><li>    <span class="s">(</span><span class="q">?^x:foo)</span></li></ol></pre><p>is equivalent to</p>
<pre class="verbatim"><ol><li>    <span class="s">(</span><span class="q">?x-imns:foo)</span></li></ol></pre><p>The caret tells Perl that this cluster doesn't inherit the flags of any
surrounding pattern, but uses the system defaults (<code class="inline"><span class="w">d</span>-<span class="w">imnsx</span></code>
),
modified by any flags specified.</p>
<p>The caret allows for simpler stringification of compiled regular
expressions.  These look like</p>
<pre class="verbatim"><ol><li>    <span class="s">(</span><span class="q">?^:pattern)</span></li></ol></pre><p>with any non-default flags appearing between the caret and the colon.
A test that looks at such stringification thus doesn't need to have the
system default flags hard-coded in it, just the caret.  If new flags are
added to Perl, the meaning of the caret's expansion will change to include
the default for those flags, so the test will still work, unchanged.</p>
<p>Specifying a negative flag after the caret is an error, as the flag is
redundant.</p>
<p>Mnemonic for <code class="inline"><span class="s">(</span><span class="q">?^...)</span></code>
:  A fresh beginning since the usual use of a caret is
to match at the beginning.</p>
</li>
<li><a name="(%3f%7cpattern)"></a><b><code class="inline">(?|pattern)</code>
 </b>
<p>This is the "branch reset" pattern, which has the special property
that the capture groups are numbered from the same starting point
in each alternation branch. It is available starting from perl 5.10.0.</p>
<p>Capture groups are numbered from left to right, but inside this
construct the numbering is restarted for each branch.</p>
<p>The numbering within each branch will be as normal, and any groups
following this construct will be numbered as though the construct
contained only one branch, that being the one with the most capture
groups in it.</p>
<p>This construct is useful when you want to capture one of a
number of alternative matches.</p>
<p>Consider the following pattern.  The numbers underneath show in
which group the captured content will be stored.</p>
<pre class="verbatim"><ol><li>    <span class="c"># before  ---------------branch-reset----------- after</span></li><li>    <span class="q">/ ( a )  (?| x ( y ) z | (p (q) r) | (t) u (v) ) ( z ) /x</span></li><li>    <span class="c"># 1            2         2  3        2     3     4</span></li></ol></pre><p>Be careful when using the branch reset pattern in combination with
named captures. Named captures are implemented as being aliases to
numbered groups holding the captures, and that interferes with the
implementation of the branch reset pattern. If you are using named
captures in a branch reset pattern, it's best to use the same names,
in the same order, in each of the alternations:</p>
<pre class="verbatim"><ol><li>   <span class="q">/(?|  (?&lt;a&gt; x ) (?&lt;b&gt; y )</span></li><li>      <span class="q">      |  (?&lt;a&gt; z ) (?&lt;b&gt; w )) /x</span></li></ol></pre><p>Not doing so may lead to surprises:</p>
<pre class="verbatim"><ol><li>  <span class="q">&quot;12&quot;</span> =~ <span class="q">/(?| (?&lt;a&gt; \d+ ) | (?&lt;b&gt; \D+))/x</span><span class="sc">;</span></li><li>  <a class="l_k" href="functions/say.html">say</a> <span class="i">$+</span>{<span class="w">a</span>}<span class="sc">;</span>    <span class="c"># Prints &#39;12&#39;</span></li><li>  <a class="l_k" href="functions/say.html">say</a> <span class="i">$+</span>{<span class="w">b</span>}<span class="sc">;</span>    <span class="c"># *Also* prints &#39;12&#39;.</span></li></ol></pre><p>The problem here is that both the group named <code class="inline"><span class="w">a</span></code>
 and the group
named <code class="inline"><span class="w">b</span></code>
 are aliases for the group belonging to <code class="inline"><span class="i">$1</span></code>
.</p>
</li>
<li><a name="Lookaround-Assertions"></a><b>Lookaround Assertions
   </b>
<p>Lookaround assertions are zero-width patterns which match a specific
pattern without including it in <code class="inline"><span class="i">$&amp;</span></code>
. Positive assertions match when
their subpattern matches, negative assertions match when their subpattern
fails. Lookbehind matches text up to the current match position,
lookahead matches text following the current match position.</p>
<ul>
<li><a name="(%3f%3dpattern)"></a><b><code class="inline">(?=pattern)</code>
  </b>
<p>A zero-width positive lookahead assertion.  For example, <code class="inline"><span class="q">/\w+(?=\t)/</span></code>

matches a word followed by a tab, without including the tab in <code class="inline"><span class="i">$&amp;</span></code>
.</p>
</li>
<li><a name="(%3f!pattern)"></a><b><code class="inline">(?!pattern)</code>
  </b>
<p>A zero-width negative lookahead assertion.  For example <code class="inline"><span class="q">/foo(?!bar)/</span></code>

matches any occurrence of "foo" that isn't followed by "bar".  Note
however that lookahead and lookbehind are NOT the same thing.  You cannot
use this for lookbehind.</p>
<p>If you are looking for a "bar" that isn't preceded by a "foo", <code class="inline"><span class="q">/(?!foo)bar/</span></code>

will not do what you want.  That's because the <code class="inline">(?!foo)</code> is just saying that
the next thing cannot be "foo"--and it's not, it's a "bar", so "foobar" will
match.  Use lookbehind instead (see below).</p>
</li>
<li><a name="(%3f%3c%3dpattern)"></a><b><code class="inline">(?&lt;=pattern)</code></b>
</li>
<li><a name="%5cK"></a><b><code class="inline">\<span class="w">K</span></code>

   </b>
<p>A zero-width positive lookbehind assertion.  For example, <code class="inline"><span class="q">/(?&lt;=\t)\w+/</span></code>

matches a word that follows a tab, without including the tab in <code class="inline"><span class="i">$&amp;</span></code>
.
Works only for fixed-width lookbehind.</p>
<p>There is a special form of this construct, called <code class="inline">\<span class="w">K</span></code>
 (available since
Perl 5.10.0), which causes the
regex engine to "keep" everything it had matched prior to the <code class="inline">\<span class="w">K</span></code>
 and
not include it in <code class="inline"><span class="i">$&amp;</span></code>
. This effectively provides variable-length
lookbehind. The use of <code class="inline">\<span class="w">K</span></code>
 inside of another lookaround assertion
is allowed, but the behaviour is currently not well defined.</p>
<p>For various reasons <code class="inline">\<span class="w">K</span></code>
 may be significantly more efficient than the
equivalent <code class="inline">(?&lt;=...)</code> construct, and it is especially useful in
situations where you want to efficiently remove something following
something else in a string. For instance</p>
<pre class="verbatim"><ol><li>  <span class="q">s/(foo)bar/$1/g</span><span class="sc">;</span></li></ol></pre><p>can be rewritten as the much more efficient</p>
<pre class="verbatim"><ol><li>  <span class="q">s/foo\Kbar//g</span><span class="sc">;</span></li></ol></pre></li>
<li><a name="(%3f%3c!pattern)"></a><b><code class="inline">(?&lt;!pattern)</code>
  </b>
<p>A zero-width negative lookbehind assertion.  For example <code class="inline"><span class="q">/(?&lt;!bar)foo/</span></code>

matches any occurrence of "foo" that does not follow "bar".  Works
only for fixed-width lookbehind.</p>
</li>
</ul>
</li>
<li><a name="(%3f%3cNAME%3epattern)"></a><b><code class="inline">(?&lt;NAME&gt;pattern)</code></b>
</li>
<li><a name="(%3f'NAME'pattern)"></a><b><code class="inline">(?'NAME'pattern)</code>
   </b>
<p>A named capture group. Identical in every respect to normal capturing
parentheses <code class="inline"><span class="s">(</span><span class="s">)</span></code>
 but for the additional fact that the group
can be referred to by name in various regular expression
constructs (like <code class="inline">\<span class="i">g</span><span class="s">{</span><span class="w">NAME</span><span class="s">}</span></code>
) and can be accessed by name
after a successful match via <code class="inline"><span class="i">%+</span></code>
 or <code class="inline"><span class="i">%-</span></code>
. See <a href="perlvar.html">perlvar</a>
for more details on the <code class="inline"><span class="i">%+</span></code>
 and <code class="inline"><span class="i">%-</span></code>
 hashes.</p>
<p>If multiple distinct capture groups have the same name then the
<code class="inline"><span class="i">$+</span>{<span class="w">NAME</span>}</code>
 will refer to the leftmost defined group in the match.</p>
<p>The forms <code class="inline">(?'NAME'pattern)</code> and <code class="inline">(?&lt;NAME&gt;pattern)</code> are equivalent.</p>
<p><b>NOTE:</b> While the notation of this construct is the same as the similar
function in .NET regexes, the behavior is not. In Perl the groups are
numbered sequentially regardless of being named or not. Thus in the
pattern</p>
<pre class="verbatim"><ol><li>  <span class="q">/(x)(?&lt;foo&gt;y)(z)/</span></li></ol></pre><p><code class="inline">$+{<i>foo</i>}</code> will be the same as <code class="inline"><span class="i">$2</span></code>
, and <code class="inline"><span class="i">$3</span></code>
 will contain 'z' instead of
the opposite which is what a .NET regex hacker might expect.</p>
<p>Currently <i>NAME</i> is restricted to simple identifiers only.
In other words, it must match <code class="inline"><span class="q">/^[_A-Za-z][_A-Za-z0-9]*\z/</span></code>
 or
its Unicode extension (see <a href="utf8.html">utf8</a>),
though it isn't extended by the locale (see <a href="perllocale.html">perllocale</a>).</p>
<p><b>NOTE:</b> In order to make things easier for programmers with experience
with the Python or PCRE regex engines, the pattern <code class="inline">(?P&lt;NAME&gt;pattern)</code>
may be used instead of <code class="inline">(?&lt;NAME&gt;pattern)</code>; however this form does not
support the use of single quotes as a delimiter for the name.</p>
</li>
<li><a name="%5ck%3cNAME%3e"></a><b><code class="inline">\<span class="w">k</span><span class="q">&lt;NAME&gt;</span></code>
</b>
</li>
<li><a name="%5ck'NAME'"></a><b><code class="inline">\k'NAME'</code></b>
<p>Named backreference. Similar to numeric backreferences, except that
the group is designated by name and not number. If multiple groups
have the same name then it refers to the leftmost defined group in
the current match.</p>
<p>It is an error to refer to a name not defined by a <code class="inline">(?&lt;NAME&gt;)</code>
earlier in the pattern.</p>
<p>Both forms are equivalent.</p>
<p><b>NOTE:</b> In order to make things easier for programmers with experience
with the Python or PCRE regex engines, the pattern <code class="inline">(?P=NAME)</code>
may be used instead of <code class="inline">\<span class="w">k</span><span class="q">&lt;NAME&gt;</span></code>
.</p>
</li>
<li><a name="(%3f%7b-code-%7d)"></a><b><code class="inline">(?{ code })</code>
   </b>
<p><b>WARNING</b>: Using this feature safely requires that you understand its
limitations.  Code executed that has side effects may not perform identically
from version to version due to the effect of future optimisations in the regex
engine.  For more information on this, see <a href="#Embedded-Code-Execution-Frequency">Embedded Code Execution Frequency</a>.</p>
<p>This zero-width assertion executes any embedded Perl code.  It always
succeeds, and its return value is set as <code class="inline"><span class="i">$^R</span></code>
.</p>
<p>In literal patterns, the code is parsed at the same time as the
surrounding code. While within the pattern, control is passed temporarily
back to the perl parser, until the logically-balancing closing brace is
encountered. This is similar to the way that an array index expression in
a literal string is handled, for example</p>
<pre class="verbatim"><ol><li>    <span class="q">&quot;abc$array[ 1 + f(&#39;[&#39;) + g()]def&quot;</span></li></ol></pre><p>In particular, braces do not need to be balanced:</p>
<pre class="verbatim"><ol><li>    <span class="q">s/abc(?{ f(&#39;{&#39;); })/def/</span></li></ol></pre><p>Even in a pattern that is interpolated and compiled at run-time, literal
code blocks will be compiled once, at perl compile time; the following
prints "ABCD":</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;D&quot;</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$qr</span> = <span class="q">qr/(?{ BEGIN { print &quot;A&quot; } })/</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$foo</span> = <span class="q">&quot;foo&quot;</span><span class="sc">;</span></li><li>    <span class="q">/$foo$qr(?{ BEGIN { print &quot;B&quot; } })/</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/BEGIN.html">BEGIN</a> <span class="s">{</span> <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;C&quot;</span> <span class="s">}</span></li></ol></pre><p>In patterns where the text of the code is derived from run-time
information rather than appearing literally in a source code /pattern/,
the code is compiled at the same time that the pattern is compiled, and
for reasons of security, <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">re</span> <span class="q">&#39;eval&#39;</span></code>
 must be in scope. This is to
stop user-supplied patterns containing code snippets from being
executable.</p>
<p>In situations where you need to enable this with <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">re</span> <span class="q">&#39;eval&#39;</span></code>
, you should
also have taint checking enabled.  Better yet, use the carefully
constrained evaluation within a Safe compartment.  See <a href="perlsec.html">perlsec</a> for
details about both these mechanisms.</p>
<p>From the viewpoint of parsing, lexical variable scope and closures,</p>
<pre class="verbatim"><ol><li>    <span class="q">/AAA(?{ BBB })CCC/</span></li></ol></pre><p>behaves approximately like</p>
<pre class="verbatim"><ol><li>    <span class="q">/AAA/</span> &amp;&amp; <a class="l_k" href="functions/do.html">do</a> <span class="s">{</span> <span class="w">BBB</span> <span class="s">}</span> &amp;&amp; <span class="q">/CCC/</span></li></ol></pre><p>Similarly,</p>
<pre class="verbatim"><ol><li>    <span class="q">qr/AAA(?{ BBB })CCC/</span></li></ol></pre><p>behaves approximately like</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/sub.html">sub</a> <span class="s">{</span> <span class="q">/AAA/</span> &amp;&amp; <a class="l_k" href="functions/do.html">do</a> <span class="s">{</span> <span class="w">BBB</span> <span class="s">}</span> &amp;&amp; <span class="q">/CCC/</span> <span class="s">}</span></li></ol></pre><p>In particular:</p>
<pre class="verbatim"><ol><li>    <span class="s">{</span> <a class="l_k" href="functions/my.html">my</a> <span class="i">$i</span> = <span class="n">1</span><span class="sc">;</span> <span class="i">$r</span> = <span class="q">qr/(?{ print $i })/</span> <span class="s">}</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$i</span> = <span class="n">2</span><span class="sc">;</span></li><li>    <span class="q">/$r/</span><span class="sc">;</span> <span class="c"># prints &quot;1&quot;</span></li></ol></pre><p>Inside a <code class="inline">(?{...})</code> block, <code class="inline"><span class="i">$_</span></code>
 refers to the string the regular
expression is matching against. You can also use <code class="inline"><a class="l_k" href="functions/pos.html">pos()</a></code> to know what is
the current position of matching within this string.</p>
<p>The code block introduces a new scope from the perspective of lexical
variable declarations, but <b>not</b> from the perspective of <code class="inline"><a class="l_k" href="functions/local.html">local</a></code> and
similar localizing behaviours. So later code blocks within the same
pattern will still see the values which were localized in earlier blocks.
These accumulated localizations are undone either at the end of a
successful match, or if the assertion is backtracked (compare
<a href="#Backtracking">Backtracking</a>). For example,</p>
<pre class="verbatim"><ol><li>  <span class="i">$_</span> = <span class="q">&#39;a&#39;</span> x <span class="n">8</span><span class="sc">;</span></li><li>  <span class="q">m&lt;</span></li><li>     <span class="q">     (?{ $cnt = 0 })               # Initialize $cnt.</span></li><li>     <span class="q">     (</span></li><li>       <span class="q">       a</span></li><li>       <span class="q">       (?{</span></li><li>           <span class="q">           local $cnt = $cnt + 1;  # Update $cnt,</span></li><li>                                   <span class="q">                                   # backtracking-safe.</span></li><li>       <span class="q">       })</span></li><li>     <span class="q">     )*</span></li><li>     <span class="q">     aaaa</span></li><li>     <span class="q">     (?{ $res = $cnt })            # On success copy to</span></li><li>                                   <span class="q">                                   # non-localized location.</span></li><li>   <span class="q">   &gt;x</span><span class="sc">;</span></li></ol></pre><p>will initially increment <code class="inline"><span class="i">$cnt</span></code>
 up to 8; then during backtracking, its
value will be unwound back to 4, which is the value assigned to <code class="inline"><span class="i">$res</span></code>
.
At the end of the regex execution, <code class="inline"><span class="i">$cnt</span></code>
 will be wound back to its initial
value of 0.</p>
<p>This assertion may be used as the condition in a</p>
<pre class="verbatim"><ol><li>    <span class="s">(</span><span class="q">?(condition)yes-pattern|no-pattern)</span></li></ol></pre><p>switch.  If <i>not</i> used in this way, the result of evaluation of <code class="inline"><span class="w">code</span></code>

is put into the special variable <code class="inline"><span class="i">$^R</span></code>
.  This happens immediately, so
<code class="inline"><span class="i">$^R</span></code>
 can be used from other <code class="inline">(?{ code })</code> assertions inside the same
regular expression.</p>
<p>The assignment to <code class="inline"><span class="i">$^R</span></code>
 above is properly localized, so the old
value of <code class="inline"><span class="i">$^R</span></code>
 is restored if the assertion is backtracked; compare
<a href="#Backtracking">Backtracking</a>.</p>
<p>Note that the special variable <code class="inline"><span class="i">$^N</span></code>
  is particularly useful with code
blocks to capture the results of submatches in variables without having to
keep track of the number of nested parentheses. For example:</p>
<pre class="verbatim"><ol><li>  <span class="i">$_</span> = <span class="q">&quot;The brown fox jumps over the lazy dog&quot;</span><span class="sc">;</span></li><li>  <span class="q">/the (\S+)(?{ $color = $^N }) (\S+)(?{ $animal = $^N })/i</span><span class="sc">;</span></li><li>  <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;color = $color, animal = $animal\n&quot;</span><span class="sc">;</span></li></ol></pre></li>
<li><a name="(%3f%3f%7b-code-%7d)"></a><b><code class="inline"><span class="s">(</span><span class="q">??</span><span class="s">{</span> <span class="w">code</span> <span class="s">}</span><span class="s">)</span></code>


  </b>
<p><b>WARNING</b>: Using this feature safely requires that you understand its
limitations.  Code executed that has side effects may not perform
identically from version to version due to the effect of future
optimisations in the regex engine.  For more information on this, see
<a href="#Embedded-Code-Execution-Frequency">Embedded Code Execution Frequency</a>.</p>
<p>This is a "postponed" regular subexpression.  It behaves in <i>exactly</i> the
same way as a <code class="inline">(?{ code })</code> code block as described above, except that
its return value, rather than being assigned to <code class="inline"><span class="i">$^R</span></code>
, is treated as a
pattern, compiled if it's a string (or used as-is if its a qr// object),
then matched as if it were inserted instead of this construct.</p>
<p>During the matching of this sub-pattern, it has its own set of
captures which are valid during the sub-match, but are discarded once
control returns to the main pattern. For example, the following matches,
with the inner pattern capturing "B" and matching "BB", while the outer
pattern captures "A";</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$inner</span> = <span class="q">&#39;(.)\1&#39;</span><span class="sc">;</span></li><li>    <span class="q">&quot;ABBA&quot;</span> =~ <span class="q">/^(.)(??{ $inner })\1/</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/print.html">print</a> <span class="i">$1</span><span class="sc">;</span> <span class="c"># prints &quot;A&quot;;</span></li></ol></pre><p>Note that this means that  there is no way for the inner pattern to refer
to a capture group defined outside.  (The code block itself can use <code class="inline"><span class="i">$1</span></code>
,
<i>etc</i>., to refer to the enclosing pattern's capture groups.)  Thus, although</p>
<pre class="verbatim"><ol><li>    <span class="s">(</span><span class="q">'a'</span> x <span class="n">100</span><span class="s">)</span>=~<span class="q">/(??{'(.)' x 100})/</span></li></ol></pre><p><i>will</i> match, it will <i>not</i> set <code class="inline"><span class="i">$1</span></code>
 on exit.</p>
<p>The following pattern matches a parenthesized group:</p>
<pre class="verbatim"><ol><li> <span class="i">$re</span> = <span class="q">qr{</span></li><li>            <span class="q">            \(</span></li><li>            <span class="q">            (?:</span></li><li>               <span class="q">               (?&gt; [^()]+ )  # Non-parens without backtracking</span></li><li>             <span class="q">             |</span></li><li>               <span class="q">               (??{ $re })   # Group with matching parens</span></li><li>            <span class="q">            )*</span></li><li>            <span class="q">            \)</span></li><li>         <span class="q">         }x</span><span class="sc">;</span></li></ol></pre><p>See also
<a href="#(%3fPARNO)-(%3f-PARNO)-(%3f%2bPARNO)-(%3fR)-(%3f0)">(?PARNO)</a>
for a different, more efficient way to accomplish
the same task.</p>
<p>Executing a postponed regular expression too many times without
consuming any input string will also result in a fatal error.  The depth
at which that happens is compiled into perl, so it can be changed with a
custom build.</p>
</li>
<li><a name="(%3f_PARNO_)-(%3f-_PARNO_)-(%3f%2b_PARNO_)-(%3fR)-(%3f0)"></a><b><code class="inline">(?<i>PARNO</i>)</code> <code class="inline">(?-<i>PARNO</i>)</code> <code class="inline">(?+<i>PARNO</i>)</code> <code class="inline">(?R)</code> <code class="inline">(?0)</code>
       
  
  </b>
<p>Recursive subpattern. Treat the contents of a given capture buffer in the
current pattern as an independent subpattern and attempt to match it at
the current position in the string. Information about capture state from
the caller for things like backreferences is available to the subpattern,
but capture buffers set by the subpattern are not visible to the caller.</p>
<p>Similar to <code class="inline"><span class="s">(</span><span class="q">??</span><span class="s">{</span> <span class="w">code</span> <span class="s">}</span><span class="s">)</span></code>
 except that it does not involve executing any
code or potentially compiling a returned pattern string; instead it treats
the part of the current pattern contained within a specified capture group
as an independent pattern that must match at the current position. Also
different is the treatment of capture buffers, unlike <code class="inline"><span class="s">(</span><span class="q">??</span><span class="s">{</span> <span class="w">code</span> <span class="s">}</span><span class="s">)</span></code>

recursive patterns have access to their caller's match state, so one can
use backreferences safely.</p>
<p><i>PARNO</i> is a sequence of digits (not starting with 0) whose value reflects
the paren-number of the capture group to recurse to. <code class="inline">(?R)</code> recurses to
the beginning of the whole pattern. <code class="inline">(?0)</code> is an alternate syntax for
<code class="inline">(?R)</code>. If <i>PARNO</i> is preceded by a plus or minus sign then it is assumed
to be relative, with negative numbers indicating preceding capture groups
and positive ones following. Thus <code class="inline">(?-1)</code> refers to the most recently
declared group, and <code class="inline">(?+1)</code> indicates the next group to be declared.
Note that the counting for relative recursion differs from that of
relative backreferences, in that with recursion unclosed groups <b>are</b>
included.</p>
<p>The following pattern matches a function <code class="inline"><span class="i">foo</span><span class="s">(</span><span class="s">)</span></code>
 which may contain
balanced parentheses as the argument.</p>
<pre class="verbatim"><ol><li>  <span class="i">$re</span> = <span class="q">qr{ (                   # paren group 1 (full function)</span></li><li>              <span class="q">              foo</span></li><li>              <span class="q">              (                 # paren group 2 (parens)</span></li><li>                <span class="q">                \(</span></li><li>                  <span class="q">                  (             # paren group 3 (contents of parens)</span></li><li>                  <span class="q">                  (?:</span></li><li>                   <span class="q">                   (?&gt; [^()]+ ) # Non-parens without backtracking</span></li><li>                  <span class="q">                  |</span></li><li>                   <span class="q">                   (?2)         # Recurse to start of paren group 2</span></li><li>                  <span class="q">                  )*</span></li><li>                  <span class="q">                  )</span></li><li>                <span class="q">                \)</span></li><li>              <span class="q">              )</span></li><li>            <span class="q">            )</span></li><li>          <span class="q">          }x</span><span class="sc">;</span></li></ol></pre><p>If the pattern was used as follows</p>
<pre class="verbatim"><ol><li>    <span class="q">'foo(bar(baz)+baz(bop))'</span>=~<span class="q">/$re/</span></li><li>        and <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;\$1 = $1\n&quot;</span><span class="cm">,</span></li><li>                  <span class="q">&quot;\$2 = $2\n&quot;</span><span class="cm">,</span></li><li>                  <span class="q">&quot;\$3 = $3\n&quot;</span><span class="sc">;</span></li></ol></pre><p>the output produced should be the following:</p>
<pre class="verbatim"><ol><li>    $1 = foo(bar(baz)+baz(bop))</li><li>    $2 = (bar(baz)+baz(bop))</li><li>    $3 = bar(baz)+baz(bop)</li></ol></pre><p>If there is no corresponding capture group defined, then it is a
fatal error.  Recursing deeply without consuming any input string will
also result in a fatal error.  The depth at which that happens is
compiled into perl, so it can be changed with a custom build.</p>
<p>The following shows how using negative indexing can make it
easier to embed recursive patterns inside of a <code class="inline"><a class="l_k" href="functions/qr.html">qr//</a></code> construct
for later use:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$parens</span> = <span class="q">qr/(\((?:[^()]++|(?-1))*+\))/</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/if.html">if</a> <span class="s">(</span><span class="q">/foo $parens \s+ \+ \s+ bar $parens/x</span><span class="s">)</span> <span class="s">{</span></li><li>       <span class="c"># do something here...</span></li><li>    <span class="s">}</span></li></ol></pre><p><b>Note</b> that this pattern does not behave the same way as the equivalent
PCRE or Python construct of the same form. In Perl you can backtrack into
a recursed group, in PCRE and Python the recursed into group is treated
as atomic. Also, modifiers are resolved at compile time, so constructs
like <code class="inline"><span class="s">(</span><span class="q">?i:(?</span><span class="n">1</span><span class="s">)</span><span class="p">)</span></code>
 or <code class="inline"><span class="s">(</span><span class="q">?:(?i</span><span class="s">)</span><span class="s">(</span><span class="q">?1))</span></code>
 do not affect how the sub-pattern will
be processed.</p>
</li>
<li><a name="(%3f%26NAME)"></a><b><code class="inline">(?&amp;NAME)</code>
</b>
<p>Recurse to a named subpattern. Identical to <code class="inline">(?<i>PARNO</i>)</code> except that the
parenthesis to recurse to is determined by name. If multiple parentheses have
the same name, then it recurses to the leftmost.</p>
<p>It is an error to refer to a name that is not declared somewhere in the
pattern.</p>
<p><b>NOTE:</b> In order to make things easier for programmers with experience
with the Python or PCRE regex engines the pattern <code class="inline">(?P&gt;NAME)</code>
may be used instead of <code class="inline">(?&amp;NAME)</code>.</p>
</li>
<li><a name="(%3f(condition)yes-pattern%7cno-pattern)"></a><b><code class="inline">(?(condition)yes-pattern|no-pattern)</code>
</b>
</li>
<li><a name="(%3f(condition)yes-pattern)"></a><b><code class="inline">(?(condition)yes-pattern)</code></b>
<p>Conditional expression. Matches <code class="inline"><span class="w">yes</span>-<span class="w">pattern</span></code>
 if <code class="inline"><span class="w">condition</span></code>
 yields
a true value, matches <code class="inline"><a class="l_k" href="functions/no.html">no-pattern</a></code> otherwise. A missing pattern always
matches.</p>
<p><code class="inline"><span class="s">(</span><span class="w">condition</span><span class="s">)</span></code>
 should be one of:</p>
<ul>
<li><a name="an-integer-in-parentheses"></a><b>an integer in parentheses</b>
<p>(which is valid if the corresponding pair of parentheses
matched);</p>
</li>
<li><a name="a-lookahead%2flookbehind%2fevaluate-zero-width-assertion%3b"></a><b>a lookahead/lookbehind/evaluate zero-width assertion;</b>
</li>
<li><a name="a-name-in-angle-brackets-or-single-quotes"></a><b>a name in angle brackets or single quotes</b>
<p>(which is valid if a group with the given name matched);</p>
</li>
<li><a name="the-special-symbol-(R)"></a><b>the special symbol <code class="inline"><span class="s">(</span><span class="w">R</span><span class="s">)</span></code>
</b>
<p>(true when evaluated inside of recursion or eval).  Additionally the
<code class="inline"><span class="q">&quot;R&quot;</span></code>
 may be
followed by a number, (which will be true when evaluated when recursing
inside of the appropriate group), or by <code class="inline"><span class="i">&amp;NAME</span></code>
, in which case it will
be true only when evaluated during recursion in the named group.</p>
</li>
</ul>
<p>Here's a summary of the possible predicates:</p>
<ul>
<li><a name="(1)-(2)-..."></a><b><code class="inline"><span class="s">(</span><span class="n">1</span><span class="s">)</span></code>
 <code class="inline"><span class="s">(</span><span class="n">2</span><span class="s">)</span></code>
 ...</b>
<p>Checks if the numbered capturing group has matched something.
Full syntax: <code class="inline"><span class="s">(</span><span class="q">?(1)then|else)</span></code>
</p>
</li>
<li><a name="(%3c_NAME_%3e)-('_NAME_')"></a><b><code class="inline">(&lt;<i>NAME</i>&gt;)</code> <code class="inline">('<i>NAME</i>')</code></b>
<p>Checks if a group with the given name has matched something.
Full syntax: <code class="inline"><span class="s">(</span><span class="q">?(&lt;name&gt;)then|else)</span></code>
</p>
</li>
<li><a name="(%3f%3d...)-(%3f!...)-(%3f%3c%3d...)-(%3f%3c!...)"></a><b><code class="inline"><span class="s">(</span><span class="q">?=...)</span></code>
 <code class="inline"><span class="s">(</span><span class="q">?!...)</span></code>
 <code class="inline">(?&lt;=...)</code> <code class="inline"><span class="s">(</span><span class="q">?&lt;!...)</span></code>
</b>
<p>Checks whether the pattern matches (or does not match, for the <code class="inline"><span class="q">&quot;!&quot;</span></code>

variants).
Full syntax: <code class="inline"><span class="s">(</span><span class="q">?(?</span>=<span class="w">lookahead</span><span class="s">)</span><span class="w">then</span>|<a class="l_k" href="functions/else.html">else</a><span class="p">)</span></code>
</p>
</li>
<li><a name="(%3f%7b-_CODE_-%7d)"></a><b><code class="inline">(?{ <i>CODE</i> })</code></b>
<p>Treats the return value of the code block as the condition.
Full syntax: <code class="inline"><span class="s">(</span><span class="q">?(?</span><span class="s">{</span> <span class="w">code</span> <span class="s">}</span><span class="s">)</span><span class="w">then</span>|<a class="l_k" href="functions/else.html">else</a><span class="p">)</span></code>
</p>
</li>
<li><a name="(R)"></a><b><code class="inline"><span class="s">(</span><span class="w">R</span><span class="s">)</span></code>
</b>
<p>Checks if the expression has been evaluated inside of recursion.
Full syntax: <code class="inline"><span class="s">(</span><span class="q">?(R)then|else)</span></code>
</p>
</li>
<li><a name="(R1)-(R2)-..."></a><b><code class="inline"><span class="s">(</span><span class="w">R1</span><span class="s">)</span></code>
 <code class="inline"><span class="s">(</span><span class="w">R2</span><span class="s">)</span></code>
 ...</b>
<p>Checks if the expression has been evaluated while executing directly
inside of the n-th capture group. This check is the regex equivalent of</p>
<pre class="verbatim"><ol><li>  if <span class="s">(</span><span class="s">(</span><a class="l_k" href="functions/caller.html">caller</a><span class="s">(</span><span class="n">0</span><span class="s">)</span><span class="s">)</span>[<span class="n">3</span>] eq <span class="q">'subname'</span><span class="s">)</span> <span class="s">{</span> ... <span class="s">}</span></li></ol></pre><p>In other words, it does not check the full recursion stack.</p>
<p>Full syntax: <code class="inline"><span class="s">(</span><span class="q">?(R1)then|else)</span></code>
</p>
</li>
<li><a name="(R%26_NAME_)"></a><b><code class="inline">(R&amp;<i>NAME</i>)</code></b>
<p>Similar to <code class="inline"><span class="s">(</span><span class="w">R1</span><span class="s">)</span></code>
, this predicate checks to see if we're executing
directly inside of the leftmost group with a given name (this is the same
logic used by <code class="inline">(?&amp;<i>NAME</i>)</code> to disambiguate). It does not check the full
stack, but only the name of the innermost active recursion.
Full syntax: <code class="inline"><span class="s">(</span><span class="q">?(R&amp;name)then|else)</span></code>
</p>
</li>
<li><a name="(DEFINE)"></a><b><code class="inline"><span class="s">(</span><span class="w">DEFINE</span><span class="s">)</span></code>
</b>
<p>In this case, the yes-pattern is never directly executed, and no
no-pattern is allowed. Similar in spirit to <code class="inline">(?{0})</code> but more efficient.
See below for details.
Full syntax: <code class="inline"><span class="s">(</span><span class="q">?(DEFINE)definitions...)</span></code>
</p>
</li>
</ul>
<p>For example:</p>
<pre class="verbatim"><ol><li>    <span class="q">m{ ( \( )?</span></li><li>       <span class="q">       [^()]+</span></li><li>       <span class="q">       (?(1) \) )</span></li><li>     <span class="q">     }x</span></li></ol></pre><p>matches a chunk of non-parentheses, possibly included in parentheses
themselves.</p>
<p>A special form is the <code class="inline"><span class="s">(</span><span class="w">DEFINE</span><span class="s">)</span></code>
 predicate, which never executes its
yes-pattern directly, and does not allow a no-pattern. This allows one to
define subpatterns which will be executed only by the recursion mechanism.
This way, you can define a set of regular expression rules that can be
bundled into any pattern you choose.</p>
<p>It is recommended that for this usage you put the DEFINE block at the
end of the pattern, and that you name any subpatterns defined within it.</p>
<p>Also, it's worth noting that patterns defined this way probably will
not be as efficient, as the optimizer is not very clever about
handling them.</p>
<p>An example of how this might be used is as follows:</p>
<pre class="verbatim"><ol><li>  <span class="q">/(?&lt;NAME&gt;(?&amp;NAME_PAT))(?&lt;ADDR&gt;(?&amp;ADDRESS_PAT))</span></li><li>   <span class="q">   (?(DEFINE)</span></li><li>     <span class="q">     (?&lt;NAME_PAT&gt;....)</span></li><li>     <span class="q">     (?&lt;ADDRESS_PAT&gt;....)</span></li><li>   <span class="q">   )/x</span></li></ol></pre><p>Note that capture groups matched inside of recursion are not accessible
after the recursion returns, so the extra layer of capturing groups is
necessary. Thus <code class="inline"><span class="i">$+</span>{<span class="w">NAME_PAT</span>}</code>
 would not be defined even though
<code class="inline"><span class="i">$+</span>{<span class="w">NAME</span>}</code>
 would be.</p>
<p>Finally, keep in mind that subpatterns created inside a DEFINE block
count towards the absolute and relative number of captures, so this:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">@captures</span> = <span class="q">&quot;a&quot;</span> =~ <span class="q">/(.)                  # First capture</span></li><li>                           <span class="q">                           (?(DEFINE)</span></li><li>                               <span class="q">                               (?&lt;EXAMPLE&gt; 1 )  # Second capture</span></li><li>                           <span class="q">                           )/x</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/say.html">say</a> <a class="l_k" href="functions/scalar.html">scalar</a> <span class="i">@captures</span><span class="sc">;</span></li></ol></pre><p>Will output 2, not 1. This is particularly important if you intend to
compile the definitions with the <code class="inline"><a class="l_k" href="functions/qr.html">qr//</a></code> operator, and later
interpolate them in another pattern.</p>
</li>
<li><a name="(%3f%3epattern)"></a><b><code class="inline">(?&gt;pattern)</code>
   </b>
<p>An "independent" subexpression, one which matches the substring
that a <i>standalone</i> <code class="inline"><span class="w">pattern</span></code>
 would match if anchored at the given
position, and it matches <i>nothing other than this substring</i>.  This
construct is useful for optimizations of what would otherwise be
"eternal" matches, because it will not backtrack (see <a href="#Backtracking">Backtracking</a>).
It may also be useful in places where the "grab all you can, and do not
give anything back" semantic is desirable.</p>
<p>For example: <code class="inline">^(?&gt;a*)ab</code> will never match, since <code class="inline">(?&gt;a*)</code>
(anchored at the beginning of string, as above) will match <i>all</i>
characters <code class="inline"><span class="q">&quot;a&quot;</span></code>
 at the beginning of string, leaving no <code class="inline"><span class="q">&quot;a&quot;</span></code>
 for
<code class="inline"><span class="w">ab</span></code>
 to match.  In contrast, <code class="inline"><span class="w">a</span>*<span class="w">ab</span></code>
 will match the same as <code class="inline"><span class="w">a</span>+<span class="w">b</span></code>
,
since the match of the subgroup <code class="inline"><span class="w">a</span>*</code>
 is influenced by the following
group <code class="inline"><span class="w">ab</span></code>
 (see <a href="#Backtracking">Backtracking</a>).  In particular, <code class="inline"><span class="w">a</span>*</code>
 inside
<code class="inline"><span class="w">a</span>*<span class="w">ab</span></code>
 will match fewer characters than a standalone <code class="inline"><span class="w">a</span>*</code>
, since
this makes the tail match.</p>
<p><code class="inline">(?&gt;pattern)</code> does not disable backtracking altogether once it has
matched. It is still possible to backtrack past the construct, but not
into it. So <code class="inline"><span class="s">(</span><span class="s">(</span><span class="q">?&gt;a*)|(?</span>&gt;<span class="w">b</span>*<span class="s">)</span><span class="s">)</span><span class="w">ar</span></code>
 will still match "bar".</p>
<p>An effect similar to <code class="inline">(?&gt;pattern)</code> may be achieved by writing
<code class="inline"><span class="s">(</span><span class="q">?=(pattern))\g{-1}</span></code>
.  This matches the same substring as a standalone
<code class="inline"><span class="w">a</span>+</code>
, and the following <code class="inline">\<span class="i">g</span><span class="s">{</span><span class="n">-1</span><span class="s">}</span></code>
 eats the matched string; it therefore
makes a zero-length assertion into an analogue of <code class="inline">(?&gt;...)</code>.
(The difference between these two constructs is that the second one
uses a capturing group, thus shifting ordinals of backreferences
in the rest of a regular expression.)</p>
<p>Consider this pattern:</p>
<pre class="verbatim"><ol><li>    <span class="q">m{ \(</span></li><li>          <span class="q">          (</span></li><li>            <span class="q">            [^()]+           # x+</span></li><li>          <span class="q">          |</span></li><li>            <span class="q">            \( [^()]* \)</span></li><li>          <span class="q">          )+</span></li><li>       <span class="q">       \)</span></li><li>     <span class="q">     }x</span></li></ol></pre><p>That will efficiently match a nonempty group with matching parentheses
two levels deep or less.  However, if there is no such group, it
will take virtually forever on a long string.  That's because there
are so many different ways to split a long string into several
substrings.  This is what <code class="inline">(.+)+</code> is doing, and <code class="inline">(.+)+</code> is similar
to a subpattern of the above pattern.  Consider how the pattern
above detects no-match on <code class="inline">((()aaaaaaaaaaaaaaaaaa</code> in several
seconds, but that each extra letter doubles this time.  This
exponential performance will make it appear that your program has
hung.  However, a tiny change to this pattern</p>
<pre class="verbatim"><ol><li>    <span class="q">m{ \(</span></li><li>          <span class="q">          (</span></li><li>            <span class="q">            (?&gt; [^()]+ )        # change x+ above to (?&gt; x+ )</span></li><li>          <span class="q">          |</span></li><li>            <span class="q">            \( [^()]* \)</span></li><li>          <span class="q">          )+</span></li><li>       <span class="q">       \)</span></li><li>     <span class="q">     }x</span></li></ol></pre><p>which uses <code class="inline">(?&gt;...)</code> matches exactly when the one above does (verifying
this yourself would be a productive exercise), but finishes in a fourth
the time when used on a similar string with 1000000 <code class="inline"><span class="q">&quot;a&quot;</span></code>
s.  Be aware,
however, that, when this construct is followed by a
quantifier, it currently triggers a warning message under
the <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">warnings</span></code>
 pragma or <b>-w</b> switch saying it
<code class="inline"><span class="q">&quot;matches null string many times in regex&quot;</span></code>
.</p>
<p>On simple groups, such as the pattern <code class="inline">(?&gt; [^()]+ )</code>, a comparable
effect may be achieved by negative lookahead, as in <code class="inline">[^()]+ (?! [^()] )</code>.
This was only 4 times slower on a string with 1000000 <code class="inline"><span class="q">&quot;a&quot;</span></code>
s.</p>
<p>The "grab all you can, and do not give anything back" semantic is desirable
in many situations where on the first sight a simple <code class="inline"><span class="s">(</span><span class="s">)</span>*</code>
 looks like
the correct solution.  Suppose we parse text with comments being delimited
by <code class="inline"><span class="q">&quot;#&quot;</span></code>
 followed by some optional (horizontal) whitespace.  Contrary to
its appearance, <code class="inline"><span class="c">#[ \t]*</span></code>
 <i>is not</i> the correct subexpression to match
the comment delimiter, because it may "give up" some whitespace if
the remainder of the pattern can be made to match that way.  The correct
answer is either one of these:</p>
<pre class="verbatim"><ol><li>    <span class="s">(</span><span class="q">?&gt;#[ \t]*)</span></li><li>    <span class="q">    #[ \t]*(?</span>!<span class="s">[</span> \<span class="w">t</span><span class="s">]</span><span class="s">)</span></li></ol></pre><p>For example, to grab non-empty comments into <code class="inline"><span class="i">$1</span></code>
, one should use either
one of these:</p>
<pre class="verbatim"><ol><li>    <span class="q">/ (?&gt; \# [ \t]* ) (        .+ ) /x</span><span class="sc">;</span></li><li>    <span class="q">/     \# [ \t]*   ( [^ \t] .* ) /x</span><span class="sc">;</span></li></ol></pre><p>Which one you pick depends on which of these expressions better reflects
the above specification of comments.</p>
<p>In some literature this construct is called "atomic matching" or
"possessive matching".</p>
<p>Possessive quantifiers are equivalent to putting the item they are applied
to inside of one of these constructs. The following equivalences apply:</p>
<pre class="verbatim"><ol><li>    Quantifier Form     Bracketing Form</li><li>    ---------------     ---------------</li><li>    PAT*+               (?&gt;PAT*)</li><li>    PAT++               (?&gt;PAT+)</li><li>    PAT?+               (?&gt;PAT?)</li><li>    PAT{min,max}+       (?&gt;PAT{min,max})</li></ol></pre></li>
<li><a name="(%3f%5b-%5d)"></a><b><code class="inline"><span class="s">(</span><span class="q">?[ ])</span></code>
</b>
<p>See <a href="perlrecharclass.html#Extended-Bracketed-Character-Classes">Extended Bracketed Character Classes in perlrecharclass</a>.</p>
<p>Note that this feature is currently <a href="perlpolicy.html#experimental">experimental</a>;
using it yields a warning in the <code class="inline"><span class="w">experimental::regex_sets</span></code>
 category.</p>
</li>
</ul>
<a name="Backtracking"></a><h2>Backtracking
 </h2>
<p>NOTE: This section presents an abstract approximation of regular
expression behavior.  For a more rigorous (and complicated) view of
the rules involved in selecting a match among possible alternatives,
see <a href="#Combining-RE-Pieces">Combining RE Pieces</a>.</p>
<p>A fundamental feature of regular expression matching involves the
notion called <i>backtracking</i>, which is currently used (when needed)
by all regular non-possessive expression quantifiers, namely <code class="inline"><span class="q">&quot;*&quot;</span></code>
, <code class="inline"><span class="i">*?</span></code>
, <code class="inline"><span class="q">&quot;+&quot;</span></code>
,
<code class="inline">+?</code>, <code class="inline">{n,m}</code>, and <code class="inline">{n,m}?</code>.  Backtracking is often optimized
internally, but the general principle outlined here is valid.</p>
<p>For a regular expression to match, the <i>entire</i> regular expression must
match, not just part of it.  So if the beginning of a pattern containing a
quantifier succeeds in a way that causes later parts in the pattern to
fail, the matching engine backs up and recalculates the beginning
part--that's why it's called backtracking.</p>
<p>Here is an example of backtracking:  Let's say you want to find the
word following "foo" in the string "Food is on the foo table.":</p>
<pre class="verbatim"><ol><li>    <span class="i">$_</span> = <span class="q">&quot;Food is on the foo table.&quot;</span><span class="sc">;</span></li><li>    if <span class="s">(</span> <span class="q">/\b(foo)\s+(\w+)/i</span> <span class="s">)</span> <span class="s">{</span></li><li>        <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;$2 follows $1.\n&quot;</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>When the match runs, the first part of the regular expression (<code class="inline">\<span class="i">b</span><span class="s">(</span><span class="w">foo</span><span class="s">)</span></code>
)
finds a possible match right at the beginning of the string, and loads up
<code class="inline"><span class="i">$1</span></code>
 with "Foo".  However, as soon as the matching engine sees that there's
no whitespace following the "Foo" that it had saved in <code class="inline"><span class="i">$1</span></code>
, it realizes its
mistake and starts over again one character after where it had the
tentative match.  This time it goes all the way until the next occurrence
of "foo". The complete regular expression matches this time, and you get
the expected output of "table follows foo."</p>
<p>Sometimes minimal matching can help a lot.  Imagine you'd like to match
everything between "foo" and "bar".  Initially, you write something
like this:</p>
<pre class="verbatim"><ol><li>    <span class="i">$_</span> =  <span class="q">&quot;The food is under the bar in the barn.&quot;</span><span class="sc">;</span></li><li>    if <span class="s">(</span> <span class="q">/foo(.*)bar/</span> <span class="s">)</span> <span class="s">{</span></li><li>        <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;got &lt;$1&gt;\n&quot;</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>Which perhaps unexpectedly yields:</p>
<pre class="verbatim"><ol><li>  <span class="w">got</span> <span class="q">&lt;d is under the bar in the &gt;</span></li></ol></pre><p>That's because <code class="inline">.*</code> was greedy, so you get everything between the
<i>first</i> "foo" and the <i>last</i> "bar".  Here it's more effective
to use minimal matching to make sure you get the text between a "foo"
and the first "bar" thereafter.</p>
<pre class="verbatim"><ol><li>    if <span class="s">(</span> <span class="q">/foo(.*?)bar/</span> <span class="s">)</span> <span class="s">{</span> <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;got &lt;$1&gt;\n&quot;</span> <span class="s">}</span></li><li>  <span class="w">got</span> <span class="q">&lt;d is under the &gt;</span></li></ol></pre><p>Here's another example. Let's say you'd like to match a number at the end
of a string, and you also want to keep the preceding part of the match.
So you write this:</p>
<pre class="verbatim"><ol><li>    <span class="i">$_</span> = <span class="q">&quot;I have 2 numbers: 53147&quot;</span><span class="sc">;</span></li><li>    if <span class="s">(</span> <span class="q">/(.*)(\d*)/</span> <span class="s">)</span> <span class="s">{</span>                                <span class="c"># Wrong!</span></li><li>        <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;Beginning is &lt;$1&gt;, number is &lt;$2&gt;.\n&quot;</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>That won't work at all, because <code class="inline">.*</code> was greedy and gobbled up the
whole string. As <code class="inline">\<span class="w">d</span>*</code>
 can match on an empty string the complete
regular expression matched successfully.</p>
<pre class="verbatim"><ol><li>    <span class="w">Beginning</span> <span class="w">is</span> <span class="q">&lt;I have 2 numbers: 53147&gt;</span><span class="cm">,</span> <span class="w">number</span> <span class="w">is</span> &lt;&gt;.</li></ol></pre><p>Here are some variants, most of which don't work:</p>
<pre class="verbatim"><ol><li>    <span class="i">$_</span> = <span class="q">&quot;I have 2 numbers: 53147&quot;</span><span class="sc">;</span></li><li>    <span class="i">@pats</span> = <span class="q">qw{</span></li><li>        <span class="q">(.*)(\d*)</span></li><li>        <span class="q">(.*)(\d+)</span></li><li>        <span class="q">(.*?)(\d*)</span></li><li>        <span class="q">(.*?)(\d+)</span></li><li>        <span class="q">(.*)(\d+)$</span></li><li>        <span class="q">(.*?)(\d+)$</span></li><li>        <span class="q">(.*)\b(\d+)$</span></li><li>        <span class="q">(.*\D)(\d+)$</span></li><li>    <span class="q">}</span><span class="sc">;</span></li><li></li><li>    for <span class="i">$pat</span> <span class="s">(</span><span class="i">@pats</span><span class="s">)</span> <span class="s">{</span></li><li>        <a class="l_k" href="functions/printf.html">printf</a> <span class="q">&quot;%-12s &quot;</span><span class="cm">,</span> <span class="i">$pat</span><span class="sc">;</span></li><li>        if <span class="s">(</span> <span class="q">/$pat/</span> <span class="s">)</span> <span class="s">{</span></li><li>            <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;&lt;$1&gt; &lt;$2&gt;\n&quot;</span><span class="sc">;</span></li><li>        <span class="s">}</span> else <span class="s">{</span></li><li>            <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;FAIL\n&quot;</span><span class="sc">;</span></li><li>        <span class="s">}</span></li><li>    <span class="s">}</span></li></ol></pre><p>That will print out:</p>
<pre class="verbatim"><ol><li>    (.*)(\d*)    &lt;I have 2 numbers: 53147&gt; &lt;&gt;</li><li>    (.*)(\d+)    &lt;I have 2 numbers: 5314&gt; &lt;7&gt;</li><li>    (.*?)(\d*)   &lt;&gt; &lt;&gt;</li><li>    (.*?)(\d+)   &lt;I have &gt; &lt;2&gt;</li><li>    (.*)(\d+)$   &lt;I have 2 numbers: 5314&gt; &lt;7&gt;</li><li>    (.*?)(\d+)$  &lt;I have 2 numbers: &gt; &lt;53147&gt;</li><li>    (.*)\b(\d+)$ &lt;I have 2 numbers: &gt; &lt;53147&gt;</li><li>    (.*\D)(\d+)$ &lt;I have 2 numbers: &gt; &lt;53147&gt;</li></ol></pre><p>As you see, this can be a bit tricky.  It's important to realize that a
regular expression is merely a set of assertions that gives a definition
of success.  There may be 0, 1, or several different ways that the
definition might succeed against a particular string.  And if there are
multiple ways it might succeed, you need to understand backtracking to
know which variety of success you will achieve.</p>
<p>When using lookahead assertions and negations, this can all get even
trickier.  Imagine you'd like to find a sequence of non-digits not
followed by "123".  You might try to write that as</p>
<pre class="verbatim"><ol><li>    <span class="i">$_</span> = <span class="q">&quot;ABC123&quot;</span><span class="sc">;</span></li><li>    if <span class="s">(</span> <span class="q">/^\D*(?!123)/</span> <span class="s">)</span> <span class="s">{</span>                <span class="c"># Wrong!</span></li><li>        <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;Yup, no 123 in $_\n&quot;</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>But that isn't going to match; at least, not the way you're hoping.  It
claims that there is no 123 in the string.  Here's a clearer picture of
why that pattern matches, contrary to popular expectations:</p>
<pre class="verbatim"><ol><li>    <span class="i">$x</span> = <span class="q">&#39;ABC123&#39;</span><span class="sc">;</span></li><li>    <span class="i">$y</span> = <span class="q">&#39;ABC445&#39;</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;1: got $1\n&quot;</span> if <span class="i">$x</span> =~ <span class="q">/^(ABC)(?!123)/</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;2: got $1\n&quot;</span> if <span class="i">$y</span> =~ <span class="q">/^(ABC)(?!123)/</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;3: got $1\n&quot;</span> if <span class="i">$x</span> =~ <span class="q">/^(\D*)(?!123)/</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;4: got $1\n&quot;</span> if <span class="i">$y</span> =~ <span class="q">/^(\D*)(?!123)/</span><span class="sc">;</span></li></ol></pre><p>This prints</p>
<pre class="verbatim"><ol><li>    2: got ABC</li><li>    3: got AB</li><li>    4: got ABC</li></ol></pre><p>You might have expected test 3 to fail because it seems to a more
general purpose version of test 1.  The important difference between
them is that test 3 contains a quantifier (<code class="inline">\<span class="w">D</span>*</code>
) and so can use
backtracking, whereas test 1 will not.  What's happening is
that you've asked "Is it true that at the start of <code class="inline"><span class="i">$x</span></code>
, following 0 or more
non-digits, you have something that's not 123?"  If the pattern matcher had
let <code class="inline">\<span class="w">D</span>*</code>
 expand to "ABC", this would have caused the whole pattern to
fail.</p>
<p>The search engine will initially match <code class="inline">\<span class="w">D</span>*</code>
 with "ABC".  Then it will
try to match <code class="inline"><span class="s">(</span><span class="q">?!123)</span></code>
 with "123", which fails.  But because
a quantifier (<code class="inline">\<span class="w">D</span>*</code>
) has been used in the regular expression, the
search engine can backtrack and retry the match differently
in the hope of matching the complete regular expression.</p>
<p>The pattern really, <i>really</i> wants to succeed, so it uses the
standard pattern back-off-and-retry and lets <code class="inline">\<span class="w">D</span>*</code>
 expand to just "AB" this
time.  Now there's indeed something following "AB" that is not
"123".  It's "C123", which suffices.</p>
<p>We can deal with this by using both an assertion and a negation.
We'll say that the first part in <code class="inline"><span class="i">$1</span></code>
 must be followed both by a digit
and by something that's not "123".  Remember that the lookaheads
are zero-width expressions--they only look, but don't consume any
of the string in their match.  So rewriting this way produces what
you'd expect; that is, case 5 will fail, but case 6 succeeds:</p>
<pre class="verbatim"><ol><li>    print "5: got $1\n" if $x =~ /^(\D*)(?=\d)(?!123)/;</li><li>    print "6: got $1\n" if $y =~ /^(\D*)(?=\d)(?!123)/;</li><li></li><li>    6: got ABC</li></ol></pre><p>In other words, the two zero-width assertions next to each other work as though
they're ANDed together, just as you'd use any built-in assertions:  <code class="inline"><span class="q">/^$/</span></code>

matches only if you're at the beginning of the line AND the end of the
line simultaneously.  The deeper underlying truth is that juxtaposition in
regular expressions always means AND, except when you write an explicit OR
using the vertical bar.  <code class="inline"><span class="q">/ab/</span></code>
 means match "a" AND (then) match "b",
although the attempted matches are made at different positions because "a"
is not a zero-width assertion, but a one-width assertion.</p>
<p><b>WARNING</b>: Particularly complicated regular expressions can take
exponential time to solve because of the immense number of possible
ways they can use backtracking to try for a match.  For example, without
internal optimizations done by the regular expression engine, this will
take a painfully long time to run:</p>
<pre class="verbatim"><ol><li>    <span class="q">&#39;aaaaaaaaaaaa&#39;</span> =~ <span class="q">/((a{0,5}){0,5})*[c]/</span></li></ol></pre><p>And if you used <code class="inline"><span class="q">&quot;*&quot;</span></code>
's in the internal groups instead of limiting them
to 0 through 5 matches, then it would take forever--or until you ran
out of stack space.  Moreover, these internal optimizations are not
always applicable.  For example, if you put <code class="inline"><span class="s">{</span><span class="n">0</span><span class="cm">,</span><span class="n">5</span><span class="s">}</span></code>
 instead of <code class="inline"><span class="q">&quot;*&quot;</span></code>

on the external group, no current optimization is applicable, and the
match takes a long time to finish.</p>
<p>A powerful tool for optimizing such beasts is what is known as an
"independent group",
which does not backtrack (see <a href="#(%3f%3epattern)">(?&gt;pattern)</a>).  Note also that
zero-length lookahead/lookbehind assertions will not backtrack to make
the tail match, since they are in "logical" context: only
whether they match is considered relevant.  For an example
where side-effects of lookahead <i>might</i> have influenced the
following match, see <a href="#(%3f%3epattern)">(?&gt;pattern)</a>.</p>
<a name="Special-Backtracking-Control-Verbs"></a><h2>Special Backtracking Control Verbs</h2>
<p>These special patterns are generally of the form <code class="inline">(*<i>VERB</i>:<i>ARG</i>)</code>. Unless
otherwise stated the <i>ARG</i> argument is optional; in some cases, it is
mandatory.</p>
<p>Any pattern containing a special backtracking verb that allows an argument
has the special behaviour that when executed it sets the current package's
<code class="inline"><span class="i">$REGERROR</span></code>
 and <code class="inline"><span class="i">$REGMARK</span></code>
 variables. When doing so the following
rules apply:</p>
<p>On failure, the <code class="inline"><span class="i">$REGERROR</span></code>
 variable will be set to the <i>ARG</i> value of the
verb pattern, if the verb was involved in the failure of the match. If the
<i>ARG</i> part of the pattern was omitted, then <code class="inline"><span class="i">$REGERROR</span></code>
 will be set to the
name of the last <code class="inline">(*MARK:NAME)</code> pattern executed, or to TRUE if there was
none. Also, the <code class="inline"><span class="i">$REGMARK</span></code>
 variable will be set to FALSE.</p>
<p>On a successful match, the <code class="inline"><span class="i">$REGERROR</span></code>
 variable will be set to FALSE, and
the <code class="inline"><span class="i">$REGMARK</span></code>
 variable will be set to the name of the last
<code class="inline">(*MARK:NAME)</code> pattern executed.  See the explanation for the
<code class="inline">(*MARK:NAME)</code> verb below for more details.</p>
<p><b>NOTE:</b> <code class="inline"><span class="i">$REGERROR</span></code>
 and <code class="inline"><span class="i">$REGMARK</span></code>
 are not magic variables like <code class="inline"><span class="i">$1</span></code>

and most other regex-related variables. They are not local to a scope, nor
readonly, but instead are volatile package variables similar to <code class="inline"><span class="i">$AUTOLOAD</span></code>
.
They are set in the package containing the code that <i>executed</i> the regex
(rather than the one that compiled it, where those differ).  If necessary, you
can use <code class="inline"><a class="l_k" href="functions/local.html">local</a></code> to localize changes to these variables to a specific scope
before executing a regex.</p>
<p>If a pattern does not contain a special backtracking verb that allows an
argument, then <code class="inline"><span class="i">$REGERROR</span></code>
 and <code class="inline"><span class="i">$REGMARK</span></code>
 are not touched at all.</p>
<ul>
<li><a name="Verbs"></a><b>Verbs</b>
<ul>
<li><a name="(*PRUNE)-(*PRUNE%3aNAME)"></a><b><code class="inline"><span class="s">(</span><span class="i">*PRUNE</span><span class="s">)</span></code>
 <code class="inline">(*PRUNE:NAME)</code>
 </b>
<p>This zero-width pattern prunes the backtracking tree at the current point
when backtracked into on failure. Consider the pattern <code class="inline">/<i>A</i> (*PRUNE) <i>B</i>/</code>,
where <i>A</i> and <i>B</i> are complex patterns. Until the <code class="inline"><span class="s">(</span><span class="i">*PRUNE</span><span class="s">)</span></code>
 verb is reached,
<i>A</i> may backtrack as necessary to match. Once it is reached, matching
continues in <i>B</i>, which may also backtrack as necessary; however, should B
not match, then no further backtracking will take place, and the pattern
will fail outright at the current starting position.</p>
<p>The following example counts all the possible matching strings in a
pattern (without actually matching any of them).</p>
<pre class="verbatim"><ol><li>    <span class="q">'aaab'</span> =~ <span class="q">/a+b?(?{print &quot;$&amp;\n&quot;; $count++})(*FAIL)/</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;Count=$count\n&quot;</span><span class="sc">;</span></li></ol></pre><p>which produces:</p>
<pre class="verbatim"><ol><li>    <span class="w">aaab</span></li><li>    <span class="w">aaa</span></li><li>    <span class="w">aa</span></li><li>    <span class="w">a</span></li><li>    <span class="w">aab</span></li><li>    <span class="w">aa</span></li><li>    <span class="w">a</span></li><li>    <span class="w">ab</span></li><li>    <span class="w">a</span></li><li>    <span class="w">Count</span>=<span class="n">9</span></li></ol></pre><p>If we add a <code class="inline"><span class="s">(</span><span class="i">*PRUNE</span><span class="s">)</span></code>
 before the count like the following</p>
<pre class="verbatim"><ol><li>    <span class="q">'aaab'</span> =~ <span class="q">/a+b?(*PRUNE)(?{print &quot;$&amp;\n&quot;; $count++})(*FAIL)/</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;Count=$count\n&quot;</span><span class="sc">;</span></li></ol></pre><p>we prevent backtracking and find the count of the longest matching string
at each matching starting point like so:</p>
<pre class="verbatim"><ol><li>    <span class="w">aaab</span></li><li>    <span class="w">aab</span></li><li>    <span class="w">ab</span></li><li>    <span class="w">Count</span>=<span class="n">3</span></li></ol></pre><p>Any number of <code class="inline"><span class="s">(</span><span class="i">*PRUNE</span><span class="s">)</span></code>
 assertions may be used in a pattern.</p>
<p>See also <code class="inline"><a href="#(%3f%3epattern)">(?&gt;pattern)</a></code> and possessive quantifiers for
other ways to
control backtracking. In some cases, the use of <code class="inline"><span class="s">(</span><span class="i">*PRUNE</span><span class="s">)</span></code>
 can be
replaced with a <code class="inline">(?&gt;pattern)</code> with no functional difference; however,
<code class="inline"><span class="s">(</span><span class="i">*PRUNE</span><span class="s">)</span></code>
 can be used to handle cases that cannot be expressed using a
<code class="inline">(?&gt;pattern)</code> alone.</p>
</li>
<li><a name="(*SKIP)-(*SKIP%3aNAME)"></a><b><code class="inline"><span class="s">(</span><span class="i">*SKIP</span><span class="s">)</span></code>
 <code class="inline">(*SKIP:NAME)</code>
</b>
<p>This zero-width pattern is similar to <code class="inline"><span class="s">(</span><span class="i">*PRUNE</span><span class="s">)</span></code>
, except that on
failure it also signifies that whatever text that was matched leading up
to the <code class="inline"><span class="s">(</span><span class="i">*SKIP</span><span class="s">)</span></code>
 pattern being executed cannot be part of <i>any</i> match
of this pattern. This effectively means that the regex engine "skips" forward
to this position on failure and tries to match again, (assuming that
there is sufficient room to match).</p>
<p>The name of the <code class="inline">(*SKIP:NAME)</code> pattern has special significance. If a
<code class="inline">(*MARK:NAME)</code> was encountered while matching, then it is that position
which is used as the "skip point". If no <code class="inline"><span class="s">(</span><span class="i">*MARK</span><span class="s">)</span></code>
 of that name was
encountered, then the <code class="inline"><span class="s">(</span><span class="i">*SKIP</span><span class="s">)</span></code>
 operator has no effect. When used
without a name the "skip point" is where the match point was when
executing the <code class="inline"><span class="s">(</span><span class="i">*SKIP</span><span class="s">)</span></code>
 pattern.</p>
<p>Compare the following to the examples in <code class="inline"><span class="s">(</span><span class="i">*PRUNE</span><span class="s">)</span></code>
; note the string
is twice as long:</p>
<pre class="verbatim"><ol><li> <span class="q">&#39;aaabaaab&#39;</span> =~ <span class="q">/a+b?(*SKIP)(?{print &quot;$&amp;\n&quot;; $count++})(*FAIL)/</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;Count=$count\n&quot;</span><span class="sc">;</span></li></ol></pre><p>outputs</p>
<pre class="verbatim"><ol><li>    <span class="w">aaab</span></li><li>    <span class="w">aaab</span></li><li>    <span class="w">Count</span>=<span class="n">2</span></li></ol></pre><p>Once the 'aaab' at the start of the string has matched, and the <code class="inline"><span class="s">(</span><span class="i">*SKIP</span><span class="s">)</span></code>

executed, the next starting point will be where the cursor was when the
<code class="inline"><span class="s">(</span><span class="i">*SKIP</span><span class="s">)</span></code>
 was executed.</p>
</li>
<li><a name="(*MARK%3aNAME)-(*%3aNAME)"></a><b><code class="inline">(*MARK:NAME)</code> <code class="inline">(*:NAME)</code>
  </b>
<p>This zero-width pattern can be used to mark the point reached in a string
when a certain part of the pattern has been successfully matched. This
mark may be given a name. A later <code class="inline"><span class="s">(</span><span class="i">*SKIP</span><span class="s">)</span></code>
 pattern will then skip
forward to that point if backtracked into on failure. Any number of
<code class="inline"><span class="s">(</span><span class="i">*MARK</span><span class="s">)</span></code>
 patterns are allowed, and the <i>NAME</i> portion may be duplicated.</p>
<p>In addition to interacting with the <code class="inline"><span class="s">(</span><span class="i">*SKIP</span><span class="s">)</span></code>
 pattern, <code class="inline">(*MARK:NAME)</code>
can be used to "label" a pattern branch, so that after matching, the
program can determine which branches of the pattern were involved in the
match.</p>
<p>When a match is successful, the <code class="inline"><span class="i">$REGMARK</span></code>
 variable will be set to the
name of the most recently executed <code class="inline">(*MARK:NAME)</code> that was involved
in the match.</p>
<p>This can be used to determine which branch of a pattern was matched
without using a separate capture group for each branch, which in turn
can result in a performance improvement, as perl cannot optimize
<code class="inline"><span class="q">/(?:(x)|(y)|(z))/</span></code>
 as efficiently as something like
<code class="inline"><span class="q">/(?:x(*MARK:x)|y(*MARK:y)|z(*MARK:z))/</span></code>
.</p>
<p>When a match has failed, and unless another verb has been involved in
failing the match and has provided its own name to use, the <code class="inline"><span class="i">$REGERROR</span></code>

variable will be set to the name of the most recently executed
<code class="inline">(*MARK:NAME)</code>.</p>
<p>See <a href="#(*SKIP)">(*SKIP)</a> for more details.</p>
<p>As a shortcut <code class="inline">(*MARK:NAME)</code> can be written <code class="inline">(*:NAME)</code>.</p>
</li>
<li><a name="(*THEN)-(*THEN%3aNAME)"></a><b><code class="inline"><span class="s">(</span><span class="i">*THEN</span><span class="s">)</span></code>
 <code class="inline">(*THEN:NAME)</code></b>
<p>This is similar to the "cut group" operator <code class="inline"><span class="w">::</span></code>
 from Perl 6.  Like
<code class="inline"><span class="s">(</span><span class="i">*PRUNE</span><span class="s">)</span></code>
, this verb always matches, and when backtracked into on
failure, it causes the regex engine to try the next alternation in the
innermost enclosing group (capturing or otherwise) that has alternations.
The two branches of a <code class="inline">(?(condition)yes-pattern|no-pattern)</code> do not
count as an alternation, as far as <code class="inline"><span class="s">(</span><span class="i">*THEN</span><span class="s">)</span></code>
 is concerned.</p>
<p>Its name comes from the observation that this operation combined with the
alternation operator (<code class="inline"><span class="q">&quot;|&quot;</span></code>
) can be used to create what is essentially a
pattern-based if/then/else block:</p>
<pre class="verbatim"><ol><li>  ( COND (*THEN) FOO | COND2 (*THEN) BAR | COND3 (*THEN) BAZ )</li></ol></pre><p>Note that if this operator is used and NOT inside of an alternation then
it acts exactly like the <code class="inline"><span class="s">(</span><span class="i">*PRUNE</span><span class="s">)</span></code>
 operator.</p>
<pre class="verbatim"><ol><li>  <span class="q">/ A (*PRUNE) B /</span></li></ol></pre><p>is the same as</p>
<pre class="verbatim"><ol><li>  <span class="q">/ A (*THEN) B /</span></li></ol></pre><p>but</p>
<pre class="verbatim"><ol><li>  <span class="q">/ ( A (*THEN) B | C ) /</span></li></ol></pre><p>is not the same as</p>
<pre class="verbatim"><ol><li>  <span class="q">/ ( A (*PRUNE) B | C ) /</span></li></ol></pre><p>as after matching the <i>A</i> but failing on the <i>B</i> the <code class="inline"><span class="s">(</span><span class="i">*THEN</span><span class="s">)</span></code>
 verb will
backtrack and try <i>C</i>; but the <code class="inline"><span class="s">(</span><span class="i">*PRUNE</span><span class="s">)</span></code>
 verb will simply fail.</p>
</li>
<li><a name="(*COMMIT)-(*COMMIT%3aargs)"></a><b><code class="inline"><span class="s">(</span><span class="i">*COMMIT</span><span class="s">)</span></code>
 <code class="inline"><span class="s">(</span><span class="i">*COMMIT</span><span class="co">:</span><span class="w">args</span><span class="s">)</span></code>

</b>
<p>This is the Perl 6 "commit pattern" <code class="inline"><span class="q">&lt;commit&gt;</span></code>
 or <code class="inline">:::</code>. It's a
zero-width pattern similar to <code class="inline"><span class="s">(</span><span class="i">*SKIP</span><span class="s">)</span></code>
, except that when backtracked
into on failure it causes the match to fail outright. No further attempts
to find a valid match by advancing the start pointer will occur again.
For example,</p>
<pre class="verbatim"><ol><li> <span class="q">&#39;aaabaaab&#39;</span> =~ <span class="q">/a+b?(*COMMIT)(?{print &quot;$&amp;\n&quot;; $count++})(*FAIL)/</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;Count=$count\n&quot;</span><span class="sc">;</span></li></ol></pre><p>outputs</p>
<pre class="verbatim"><ol><li>    <span class="w">aaab</span></li><li>    <span class="w">Count</span>=<span class="n">1</span></li></ol></pre><p>In other words, once the <code class="inline"><span class="s">(</span><span class="i">*COMMIT</span><span class="s">)</span></code>
 has been entered, and if the pattern
does not match, the regex engine will not try any further matching on the
rest of the string.</p>
</li>
<li><a name="(*FAIL)-(*F)-(*FAIL%3aarg)"></a><b><code class="inline"><span class="s">(</span><span class="i">*FAIL</span><span class="s">)</span></code>
 <code class="inline"><span class="s">(</span><span class="i">*F</span><span class="s">)</span></code>
 <code class="inline"><span class="s">(</span><span class="i">*FAIL</span><span class="co">:</span><span class="w">arg</span><span class="s">)</span></code>

 </b>
<p>This pattern matches nothing and always fails. It can be used to force the
engine to backtrack. It is equivalent to <code class="inline">(?!)</code>, but easier to read. In
fact, <code class="inline">(?!)</code> gets optimised into <code class="inline"><span class="s">(</span><span class="i">*FAIL</span><span class="s">)</span></code>
 internally. You can provide
an argument so that if the match fails because of this <code class="inline"><span class="w">FAIL</span></code>
 directive
the argument can be obtained from <code class="inline"><span class="i">$REGERROR</span></code>
.</p>
<p>It is probably useful only when combined with <code class="inline">(?{})</code> or <code class="inline"><span class="s">(</span><span class="q">??</span><span class="s">{</span><span class="s">}</span><span class="s">)</span></code>
.</p>
</li>
<li><a name="(*ACCEPT)-(*ACCEPT%3aarg)"></a><b><code class="inline"><span class="s">(</span><span class="i">*ACCEPT</span><span class="s">)</span></code>
 <code class="inline"><span class="s">(</span><span class="i">*ACCEPT</span><span class="co">:</span><span class="w">arg</span><span class="s">)</span></code>

</b>
<p>This pattern matches nothing and causes the end of successful matching at
the point at which the <code class="inline"><span class="s">(</span><span class="i">*ACCEPT</span><span class="s">)</span></code>
 pattern was encountered, regardless of
whether there is actually more to match in the string. When inside of a
nested pattern, such as recursion, or in a subpattern dynamically generated
via <code class="inline"><span class="s">(</span><span class="q">??</span><span class="s">{</span><span class="s">}</span><span class="s">)</span></code>
, only the innermost pattern is ended immediately.</p>
<p>If the <code class="inline"><span class="s">(</span><span class="i">*ACCEPT</span><span class="s">)</span></code>
 is inside of capturing groups then the groups are
marked as ended at the point at which the <code class="inline"><span class="s">(</span><span class="i">*ACCEPT</span><span class="s">)</span></code>
 was encountered.
For instance:</p>
<pre class="verbatim"><ol><li>  <span class="q">'AB'</span> =~ <span class="q">/(A (A|B(*ACCEPT)|C) D)(E)/x</span><span class="sc">;</span></li></ol></pre><p>will match, and <code class="inline"><span class="i">$1</span></code>
 will be <code class="inline"><span class="w">AB</span></code>
 and <code class="inline"><span class="i">$2</span></code>
 will be <code class="inline"><span class="q">&quot;B&quot;</span></code>
, <code class="inline"><span class="i">$3</span></code>
 will not
be set. If another branch in the inner parentheses was matched, such as in the
string 'ACDE', then the <code class="inline"><span class="q">&quot;D&quot;</span></code>
 and <code class="inline"><span class="q">&quot;E&quot;</span></code>
 would have to be matched as well.</p>
<p>You can provide an argument, which will be available in the var
<code class="inline"><span class="i">$REGMARK</span></code>
 after the match completes.</p>
</li>
</ul>
</li>
</ul>
<a name="Warning-on-%5c1-Instead-of-%241"></a><h2>Warning on <code class="inline">\<span class="n">1</span></code>
 Instead of <code class="inline"><span class="i">$1</span></code>
</h2>
<p>Some people get too used to writing things like:</p>
<pre class="verbatim"><ol><li>    <span class="i">$pattern</span> =~ <span class="q">s/(\W)/\\\1/g</span><span class="sc">;</span></li></ol></pre><p>This is grandfathered (for \1 to \9) for the RHS of a substitute to avoid
shocking the
<b>sed</b> addicts, but it's a dirty habit to get into.  That's because in
PerlThink, the righthand side of an <code class="inline"><a class="l_k" href="functions/s.html">s///</a></code> is a double-quoted string.  <code class="inline">\<span class="n">1</span></code>
 in
the usual double-quoted string means a control-A.  The customary Unix
meaning of <code class="inline">\<span class="n">1</span></code>
 is kludged in for <code class="inline"><a class="l_k" href="functions/s.html">s///</a></code>.  However, if you get into the habit
of doing that, you get yourself into trouble if you then add an <code class="inline">/e</code>
modifier.</p>
<pre class="verbatim"><ol><li>    <span class="q">s/(\d+)/ \1 + 1 /eg</span><span class="sc">;</span>            <span class="c"># causes warning under -w</span></li></ol></pre><p>Or if you try to do</p>
<pre class="verbatim"><ol><li>    <span class="q">s/(\d+)/\1000/</span><span class="sc">;</span></li></ol></pre><p>You can't disambiguate that by saying <code class="inline">\<span class="s">{</span><span class="n">1</span><span class="s">}</span><span class="n">000</span></code>
, whereas you can fix it with
<code class="inline">${1}000</code>.  The operation of interpolation should not be confused
with the operation of matching a backreference.  Certainly they mean two
different things on the <i>left</i> side of the <code class="inline"><a class="l_k" href="functions/s.html">s///</a></code>.</p>
<a name="Repeated-Patterns-Matching-a-Zero-length-Substring"></a><h2>Repeated Patterns Matching a Zero-length Substring</h2>
<p><b>WARNING</b>: Difficult material (and prose) ahead.  This section needs a rewrite.</p>
<p>Regular expressions provide a terse and powerful programming language.  As
with most other power tools, power comes together with the ability
to wreak havoc.</p>
<p>A common abuse of this power stems from the ability to make infinite
loops using regular expressions, with something as innocuous as:</p>
<pre class="verbatim"><ol><li>    <span class="q">&#39;foo&#39;</span> =~ <span class="q">m{ ( o? )* }x</span><span class="sc">;</span></li></ol></pre><p>The <code class="inline">o?</code> matches at the beginning of "<code class="inline"><span class="w">foo</span></code>
", and since the position
in the string is not moved by the match, <code class="inline">o?</code> would match again and again
because of the <code class="inline"><span class="q">&quot;*&quot;</span></code>
 quantifier.  Another common way to create a similar cycle
is with the looping modifier <code class="inline">/g</code>:</p>
<pre class="verbatim"><ol><li>    <span class="i">@matches</span> = <span class="s">(</span> <span class="q">&#39;foo&#39;</span> =~ <span class="q">m{ o? }xg</span> <span class="s">)</span><span class="sc">;</span></li></ol></pre><p>or</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;match: &lt;$&amp;&gt;\n&quot;</span> while <span class="q">&#39;foo&#39;</span> =~ <span class="q">m{ o? }xg</span><span class="sc">;</span></li></ol></pre><p>or the loop implied by <code class="inline"><a class="l_k" href="functions/split.html">split()</a></code>.</p>
<p>However, long experience has shown that many programming tasks may
be significantly simplified by using repeated subexpressions that
may match zero-length substrings.  Here's a simple example being:</p>
<pre class="verbatim"><ol><li>    <span class="i">@chars</span> = <a class="l_k" href="functions/split.html">split</a> <span class="q">//</span><span class="cm">,</span> <span class="i">$string</span><span class="sc">;</span>           <span class="c"># // is not magic in split</span></li><li>    <span class="s">(</span><span class="i">$whitewashed</span> = <span class="i">$string</span><span class="s">)</span> =~ <span class="q">s/()/ /g</span><span class="sc">;</span> <span class="c"># parens avoid magic s// /</span></li></ol></pre><p>Thus Perl allows such constructs, by <i>forcefully breaking
the infinite loop</i>.  The rules for this are different for lower-level
loops given by the greedy quantifiers <code class="inline"><span class="i">*+</span>{}</code>
, and for higher-level
ones like the <code class="inline">/g</code> modifier or <code class="inline"><a class="l_k" href="functions/split.html">split()</a></code> operator.</p>
<p>The lower-level loops are <i>interrupted</i> (that is, the loop is
broken) when Perl detects that a repeated expression matched a
zero-length substring.   Thus</p>
<pre class="verbatim"><ol><li>   <span class="q">m{ (?: NON_ZERO_LENGTH | ZERO_LENGTH )* }x</span><span class="sc">;</span></li></ol></pre><p>is made equivalent to</p>
<pre class="verbatim"><ol><li>   <span class="q">m{ (?: NON_ZERO_LENGTH )* (?: ZERO_LENGTH )? }x</span><span class="sc">;</span></li></ol></pre><p>For example, this program</p>
<pre class="verbatim"><ol><li>   <span class="c">#!perl -l</span></li><li>   <span class="q">&quot;aaaaab&quot;</span> =~ <span class="q">/</span></li><li>     <span class="q">     (?:</span></li><li>        <span class="q">        a                 # non-zero</span></li><li>        <span class="q">        |                 # or</span></li><li>       <span class="q">       (?{print &quot;hello&quot;}) # print hello whenever this</span></li><li>                          <span class="q">                          #    branch is tried</span></li><li>       <span class="q">       (?=(b))            # zero-width assertion</span></li><li>     <span class="q">     )*  # any number of times</span></li><li>    <span class="q">    /x</span><span class="sc">;</span></li><li>   <a class="l_k" href="functions/print.html">print</a> <span class="i">$&amp;</span><span class="sc">;</span></li><li>   <a class="l_k" href="functions/print.html">print</a> <span class="i">$1</span><span class="sc">;</span></li></ol></pre><p>prints</p>
<pre class="verbatim"><ol><li>   <span class="w">hello</span></li><li>   <span class="w">aaaaa</span></li><li>   <span class="w">b</span></li></ol></pre><p>Notice that "hello" is only printed once, as when Perl sees that the sixth
iteration of the outermost <code class="inline"><span class="s">(</span><span class="q">?:)*</span></code>
 matches a zero-length string, it stops
the <code class="inline"><span class="q">&quot;*&quot;</span></code>
.</p>
<p>The higher-level loops preserve an additional state between iterations:
whether the last match was zero-length.  To break the loop, the following
match after a zero-length match is prohibited to have a length of zero.
This prohibition interacts with backtracking (see <a href="#Backtracking">Backtracking</a>),
and so the <i>second best</i> match is chosen if the <i>best</i> match is of
zero length.</p>
<p>For example:</p>
<pre class="verbatim"><ol><li>    <span class="i">$_</span> = <span class="q">&#39;bar&#39;</span><span class="sc">;</span></li><li>    <span class="q">s/\w??/&lt;$&amp;&gt;/g</span><span class="sc">;</span></li></ol></pre><p>results in <code class="inline">&lt;&gt;&lt;<span class="w">b</span>&gt;&lt;&gt;&lt;<span class="w">a</span>&gt;&lt;&gt;&lt;<span class="w">r</span>&gt;&lt;&gt;</code>
.  At each position of the string the best
match given by non-greedy <code class="inline"><span class="q">??</span></code>
 is the zero-length match, and the <i>second
best</i> match is what is matched by <code class="inline">\<span class="w">w</span></code>
.  Thus zero-length matches
alternate with one-character-long matches.</p>
<p>Similarly, for repeated <code class="inline"><a class="l_k" href="functions/m.html">m/()/g</a></code> the second-best match is the match at the
position one notch further in the string.</p>
<p>The additional state of being <i>matched with zero-length</i> is associated with
the matched string, and is reset by each assignment to <code class="inline"><a class="l_k" href="functions/pos.html">pos()</a></code>.
Zero-length matches at the end of the previous match are ignored
during <code class="inline"><a class="l_k" href="functions/split.html">split</a></code>.</p>
<a name="Combining-RE-Pieces"></a><h2>Combining RE Pieces</h2>
<p>Each of the elementary pieces of regular expressions which were described
before (such as <code class="inline"><span class="w">ab</span></code>
 or <code class="inline">\<span class="w">Z</span></code>
) could match at most one substring
at the given position of the input string.  However, in a typical regular
expression these elementary pieces are combined into more complicated
patterns using combining operators <code class="inline"><span class="w">ST</span></code>
, <code class="inline"><span class="w">S</span>|<span class="w">T</span></code>
, <code class="inline"><span class="w">S</span>*</code>
 <i>etc</i>.
(in these examples <code class="inline"><span class="q">&quot;S&quot;</span></code>
 and <code class="inline"><span class="q">&quot;T&quot;</span></code>
 are regular subexpressions).</p>
<p>Such combinations can include alternatives, leading to a problem of choice:
if we match a regular expression <code class="inline"><span class="w">a</span>|<span class="w">ab</span></code>
 against <code class="inline"><span class="q">&quot;abc&quot;</span></code>
, will it match
substring <code class="inline"><span class="q">&quot;a&quot;</span></code>
 or <code class="inline"><span class="q">&quot;ab&quot;</span></code>
?  One way to describe which substring is
actually matched is the concept of backtracking (see <a href="#Backtracking">Backtracking</a>).
However, this description is too low-level and makes you think
in terms of a particular implementation.</p>
<p>Another description starts with notions of "better"/"worse".  All the
substrings which may be matched by the given regular expression can be
sorted from the "best" match to the "worst" match, and it is the "best"
match which is chosen.  This substitutes the question of "what is chosen?"
by the question of "which matches are better, and which are worse?".</p>
<p>Again, for elementary pieces there is no such question, since at most
one match at a given position is possible.  This section describes the
notion of better/worse for combining operators.  In the description
below <code class="inline"><span class="q">&quot;S&quot;</span></code>
 and <code class="inline"><span class="q">&quot;T&quot;</span></code>
 are regular subexpressions.</p>
<ul>
<li><a name="ST"></a><b><code class="inline"><span class="w">ST</span></code>
</b>
<p>Consider two possible matches, <code class="inline"><span class="w">AB</span></code>
 and <code class="inline">A'B'</code>, <code class="inline"><span class="q">&quot;A&quot;</span></code>
 and <code class="inline">A'</code> are
substrings which can be matched by <code class="inline"><span class="q">&quot;S&quot;</span></code>
, <code class="inline"><span class="q">&quot;B&quot;</span></code>
 and <code class="inline">B'</code> are substrings
which can be matched by <code class="inline"><span class="q">&quot;T&quot;</span></code>
.</p>
<p>If <code class="inline"><span class="q">&quot;A&quot;</span></code>
 is a better match for <code class="inline"><span class="q">&quot;S&quot;</span></code>
 than <code class="inline">A'</code>, <code class="inline"><span class="w">AB</span></code>
 is a better
match than <code class="inline">A'B'</code>.</p>
<p>If <code class="inline"><span class="q">&quot;A&quot;</span></code>
 and <code class="inline">A'</code> coincide: <code class="inline"><span class="w">AB</span></code>
 is a better match than <code class="inline">AB'</code> if
<code class="inline"><span class="q">&quot;B&quot;</span></code>
 is a better match for <code class="inline"><span class="q">&quot;T&quot;</span></code>
 than <code class="inline">B'</code>.</p>
</li>
<li><a name="S%7cT"></a><b><code class="inline"><span class="w">S</span>|<span class="w">T</span></code>
</b>
<p>When <code class="inline"><span class="q">&quot;S&quot;</span></code>
 can match, it is a better match than when only <code class="inline"><span class="q">&quot;T&quot;</span></code>
 can match.</p>
<p>Ordering of two matches for <code class="inline"><span class="q">&quot;S&quot;</span></code>
 is the same as for <code class="inline"><span class="q">&quot;S&quot;</span></code>
.  Similar for
two matches for <code class="inline"><span class="q">&quot;T&quot;</span></code>
.</p>
</li>
<li><a name="S%7bREPEAT_COUNT%7d"></a><b><code class="inline"><span class="i">S</span><span class="s">{</span><span class="w">REPEAT_COUNT</span><span class="s">}</span></code>
</b>
<p>Matches as <code class="inline"><span class="w">SSS</span>...<span class="w">S</span></code>
 (repeated as many times as necessary).</p>
</li>
<li><a name="S%7bmin%2cmax%7d"></a><b><code class="inline"><span class="w">S</span><span class="s">{</span><span class="w">min</span><span class="cm">,</span><span class="w">max</span><span class="s">}</span></code>
</b>
<p>Matches as <code class="inline">S{max}|S{max-1}|...|S{min+1}|S{min}</code>.</p>
</li>
<li><a name="S%7bmin%2cmax%7d%3f"></a><b><code class="inline">S{min,max}?</code></b>
<p>Matches as <code class="inline">S{min}|S{min+1}|...|S{max-1}|S{max}</code>.</p>
</li>
<li><a name="S%3f%2c-S*%2c-S%2b"></a><b><code class="inline">S?</code>, <code class="inline"><span class="w">S</span>*</code>
, <code class="inline"><span class="w">S</span>+</code>
</b>
<p>Same as <code class="inline"><span class="w">S</span><span class="s">{</span><span class="n">0</span><span class="cm">,</span><span class="n">1</span><span class="s">}</span></code>
, <code class="inline"><span class="w">S</span><span class="s">{</span><span class="n">0</span><span class="cm">,</span><span class="w">BIG_NUMBER</span><span class="s">}</span></code>
, <code class="inline"><span class="w">S</span><span class="s">{</span><span class="n">1</span><span class="cm">,</span><span class="w">BIG_NUMBER</span><span class="s">}</span></code>
 respectively.</p>
</li>
<li><a name="S%3f%3f%2c-S*%3f%2c-S%2b%3f"></a><b><code class="inline"><span class="w">S</span><span class="q">??</span></code>
, <code class="inline">S*?</code>, <code class="inline">S+?</code></b>
<p>Same as <code class="inline">S{0,1}?</code>, <code class="inline">S{0,BIG_NUMBER}?</code>, <code class="inline">S{1,BIG_NUMBER}?</code> respectively.</p>
</li>
<li><a name="(%3f%3eS)"></a><b><code class="inline">(?&gt;S)</code></b>
<p>Matches the best match for <code class="inline"><span class="q">&quot;S&quot;</span></code>
 and only that.</p>
</li>
<li><a name="(%3f%3dS)%2c-(%3f%3c%3dS)"></a><b><code class="inline">(?=S)</code>, <code class="inline">(?&lt;=S)</code></b>
<p>Only the best match for <code class="inline"><span class="q">&quot;S&quot;</span></code>
 is considered.  (This is important only if
<code class="inline"><span class="q">&quot;S&quot;</span></code>
 has capturing parentheses, and backreferences are used somewhere
else in the whole regular expression.)</p>
</li>
<li><a name="(%3f!S)%2c-(%3f%3c!S)"></a><b><code class="inline">(?!S)</code>, <code class="inline">(?&lt;!S)</code></b>
<p>For this grouping operator there is no need to describe the ordering, since
only whether or not <code class="inline"><span class="q">&quot;S&quot;</span></code>
 can match is important.</p>
</li>
<li><a name="(%3f%3f%7b-EXPR-%7d)%2c-(%3f_PARNO_)"></a><b><code class="inline"><span class="s">(</span><span class="q">??</span><span class="s">{</span> <span class="w">EXPR</span> <span class="s">}</span><span class="s">)</span></code>
, <code class="inline">(?<i>PARNO</i>)</code></b>
<p>The ordering is the same as for the regular expression which is
the result of EXPR, or the pattern contained by capture group <i>PARNO</i>.</p>
</li>
<li><a name="(%3f(condition)yes-pattern%7cno-pattern)"></a><b><code class="inline">(?(condition)yes-pattern|no-pattern)</code></b>
<p>Recall that which of <code class="inline"><span class="w">yes</span>-<span class="w">pattern</span></code>
 or <code class="inline"><a class="l_k" href="functions/no.html">no-pattern</a></code> actually matches is
already determined.  The ordering of the matches is the same as for the
chosen subexpression.</p>
</li>
</ul>
<p>The above recipes describe the ordering of matches <i>at a given position</i>.
One more rule is needed to understand how a match is determined for the
whole regular expression: a match at an earlier position is always better
than a match at a later position.</p>
<a name="Creating-Custom-RE-Engines"></a><h2>Creating Custom RE Engines</h2>
<p>As of Perl 5.10.0, one can create custom regular expression engines.  This
is not for the faint of heart, as they have to plug in at the C level.  See
<a href="perlreapi.html">perlreapi</a> for more details.</p>
<p>As an alternative, overloaded constants (see <a href="overload.html">overload</a>) provide a simple
way to extend the functionality of the RE engine, by substituting one
pattern for another.</p>
<p>Suppose that we want to enable a new RE escape-sequence <code class="inline">\<span class="w">Y</span>|</code>
 which
matches at a boundary between whitespace characters and non-whitespace
characters.  Note that <code class="inline"><span class="s">(</span><span class="q">?=\S)(?</span>&lt;!\<span class="w">S</span><span class="s">)</span>|<span class="s">(</span><span class="q">?!\S)(?</span>&lt;=\<span class="w">S</span><span class="s">)</span></code>
 matches exactly
at these positions, so we want to have each <code class="inline">\<span class="w">Y</span>|</code>
 in the place of the
more complicated version.  We can create a module <code class="inline"><span class="w">customre</span></code>
 to do
this:</p>
<pre class="verbatim"><ol><li><a name="package-customre"></a>    package <span class="i">customre</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">overload</span><span class="sc">;</span></li><li></li><li><a name="import"></a>    sub <span class="m">import</span> <span class="s">{</span></li><li>      <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>      <a class="l_k" href="functions/die.html">die</a> <span class="q">&quot;No argument to customre::import allowed&quot;</span> if <span class="i">@_</span><span class="sc">;</span></li><li>      <span class="w">overload::constant</span> <span class="q">&#39;qr&#39;</span> <span class="cm">=&gt;</span> \<span class="i">&amp;convert</span><span class="sc">;</span></li><li>    <span class="s">}</span></li><li></li><li><a name="invalid"></a>    sub <span class="m">invalid</span> <span class="s">{</span> <a class="l_k" href="functions/die.html">die</a> <span class="q">&quot;/$_[0]/: invalid escape &#39;\\$_[1]&#39;&quot;</span><span class="s">}</span></li><li></li><li>    <span class="c"># We must also take care of not escaping the legitimate \\Y|</span></li><li>    <span class="c"># sequence, hence the presence of &#39;\\&#39; in the conversion rules.</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">%rules</span> = <span class="s">(</span> <span class="q">&#39;\\&#39;</span> <span class="cm">=&gt;</span> <span class="q">&#39;\\\\&#39;</span><span class="cm">,</span></li><li>                  <span class="q">&#39;Y|&#39;</span> <span class="cm">=&gt;</span> <span class="q">qr/(?=\S)(?&lt;!\S)|(?!\S)(?&lt;=\S)/</span> <span class="s">)</span><span class="sc">;</span></li><li><a name="convert"></a>    sub <span class="m">convert</span> <span class="s">{</span></li><li>      <a class="l_k" href="functions/my.html">my</a> <span class="i">$re</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>      <span class="i">$re</span> =~ <span class="q">s{</span></li><li>                <span class="q">                \\ ( \\ | Y . )</span></li><li>              <span class="q">              }</span></li><li>              <span class="q">              { $rules{$1} or invalid($re,$1) }sgex</span><span class="sc">;</span></li><li>      <a class="l_k" href="functions/return.html">return</a> <span class="i">$re</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>Now <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">customre</span></code>
 enables the new escape in constant regular
expressions, <i>i.e.</i>, those without any runtime variable interpolations.
As documented in <a href="overload.html">overload</a>, this conversion will work only over
literal parts of regular expressions.  For <code class="inline">\<span class="w">Y</span>|<span class="i">$re</span>\<span class="w">Y</span>|</code>
 the variable
part of this regular expression needs to be converted explicitly
(but only if the special meaning of <code class="inline">\<span class="w">Y</span>|</code>
 should be enabled inside <code class="inline"><span class="i">$re</span></code>
):</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">customre</span><span class="sc">;</span></li><li>    <span class="i">$re</span> = &lt;&gt;<span class="sc">;</span></li><li>    <a class="l_k" href="functions/chomp.html">chomp</a> <span class="i">$re</span><span class="sc">;</span></li><li>    <span class="i">$re</span> = <span class="w">customre::convert</span> <span class="i">$re</span><span class="sc">;</span></li><li>    <span class="q">/\Y|$re\Y|/</span><span class="sc">;</span></li></ol></pre><a name="Embedded-Code-Execution-Frequency"></a><h2>Embedded Code Execution Frequency</h2>
<p>The exact rules for how often <code class="inline"><span class="s">(</span><span class="q">??</span><span class="s">{</span><span class="s">}</span><span class="s">)</span></code>
 and <code class="inline">(?{})</code> are executed in a pattern
are unspecified.  In the case of a successful match you can assume that
they DWIM and will be executed in left to right order the appropriate
number of times in the accepting path of the pattern as would any other
meta-pattern.  How non-accepting pathways and match failures affect the
number of times a pattern is executed is specifically unspecified and
may vary depending on what optimizations can be applied to the pattern
and is likely to change from version to version.</p>
<p>For instance in</p>
<pre class="verbatim"><ol><li>  <span class="q">&quot;aaabcdeeeee&quot;</span>=~<span class="q">/a(?{print &quot;a&quot;})b(?{print &quot;b&quot;})cde/</span><span class="sc">;</span></li></ol></pre><p>the exact number of times "a" or "b" are printed out is unspecified for
failure, but you may assume they will be printed at least once during
a successful match, additionally you may assume that if "b" is printed,
it will be preceded by at least one "a".</p>
<p>In the case of branching constructs like the following:</p>
<pre class="verbatim"><ol><li>  <span class="q">/a(b|(?{ print &quot;a&quot; }))c(?{ print &quot;c&quot; })/</span><span class="sc">;</span></li></ol></pre><p>you can assume that the input "ac" will output "ac", and that "abc"
will output only "c".</p>
<p>When embedded code is quantified, successful matches will call the
code once for each matched iteration of the quantifier.  For
example:</p>
<pre class="verbatim"><ol><li>  <span class="q">&quot;good&quot;</span> =~ <span class="q">/g(?:o(?{print &quot;o&quot;}))*d/</span><span class="sc">;</span></li></ol></pre><p>will output "o" twice.</p>
<a name="PCRE%2fPython-Support"></a><h2>PCRE/Python Support</h2>
<p>As of Perl 5.10.0, Perl supports several Python/PCRE-specific extensions
to the regex syntax. While Perl programmers are encouraged to use the
Perl-specific syntax, the following are also accepted:</p>
<ul>
<li><a name="(%3fP%3cNAME%3epattern)"></a><b><code class="inline">(?P&lt;NAME&gt;pattern)</code></b>
<p>Define a named capture group. Equivalent to <code class="inline">(?&lt;NAME&gt;pattern)</code>.</p>
</li>
<li><a name="(%3fP%3dNAME)"></a><b><code class="inline">(?P=NAME)</code></b>
<p>Backreference to a named capture group. Equivalent to <code class="inline">\<span class="i">g</span><span class="s">{</span><span class="w">NAME</span><span class="s">}</span></code>
.</p>
</li>
<li><a name="(%3fP%3eNAME)"></a><b><code class="inline">(?P&gt;NAME)</code></b>
<p>Subroutine call to a named capture group. Equivalent to <code class="inline">(?&amp;NAME)</code>.</p>
</li>
</ul>
<a name="BUGS"></a><h1>BUGS</h1>
<p>There are a number of issues with regard to case-insensitive matching
in Unicode rules.  See <code class="inline"><span class="q">&quot;i&quot;</span></code>
 under <a href="#Modifiers">Modifiers</a> above.</p>
<p>This document varies from difficult to understand to completely
and utterly opaque.  The wandering prose riddled with jargon is
hard to fathom in several places.</p>
<p>This document needs a rewrite that separates the tutorial content
from the reference content.</p>
<a name="SEE-ALSO"></a><h1>SEE ALSO</h1>
<p>The syntax of patterns used in Perl pattern matching evolved from those
supplied in the Bell Labs Research Unix 8th Edition (Version 8) regex
routines.  (The code is actually derived (distantly) from Henry
Spencer's freely redistributable reimplementation of those V8 routines.)</p>
<p><a href="perlrequick.html">perlrequick</a>.</p>
<p><a href="perlretut.html">perlretut</a>.</p>
<p><a href="perlop.html#Regexp-Quote-Like-Operators">Regexp Quote-Like Operators in perlop</a>.</p>
<p><a href="perlop.html#Gory-details-of-parsing-quoted-constructs">Gory details of parsing quoted constructs in perlop</a>.</p>
<p><a href="perlfaq6.html">perlfaq6</a>.</p>
<p><a href="functions/pos.html">pos</a>.</p>
<p><a href="perllocale.html">perllocale</a>.</p>
<p><a href="perlebcdic.html">perlebcdic</a>.</p>
<p><i>Mastering Regular Expressions</i> by Jeffrey Friedl, published
by O'Reilly and Associates.</p>




  <div id="page_index" class="hud_container">
    <div id="page_index_header" class="hud_header">
      <div id="page_index_close" class="hud_close"><a href="#" onClick="pageIndex.hide();return false;"></a></div>
      <div id="page_index_title" class="hud_title"><span class="hud_span_top">Page index</span></div>
      <div id="page_index_topright" class="hud_topright"></div>
    </div>
    <div id="page_index_content" class="hud_content">
      <ul><li><a href="#NAME">NAME
  </a><li><a href="#DESCRIPTION">DESCRIPTION</a><ul><li><a href="#The-Basics">The Basics
  </a><li><a href="#Modifiers">Modifiers</a><li><a href="#Regular-Expressions">Regular Expressions</a><li><a href="#Quoting-metacharacters">Quoting metacharacters</a><li><a href="#Extended-Patterns">Extended Patterns</a><li><a href="#Backtracking">Backtracking
 </a><li><a href="#Special-Backtracking-Control-Verbs">Special Backtracking Control Verbs</a><li><a href="#Warning-on-%5c1-Instead-of-%241">Warning on \1 Instead of $1</a><li><a href="#Repeated-Patterns-Matching-a-Zero-length-Substring">Repeated Patterns Matching a Zero-length Substring</a><li><a href="#Combining-RE-Pieces">Combining RE Pieces</a><li><a href="#Creating-Custom-RE-Engines">Creating Custom RE Engines</a><li><a href="#Embedded-Code-Execution-Frequency">Embedded Code Execution Frequency</a><li><a href="#PCRE%2fPython-Support">PCRE/Python Support</a></ul><li><a href="#BUGS">BUGS</a><li><a href="#SEE-ALSO">SEE ALSO</a></ul>
    </div>
    <div id="page_index_footer" class="hud_footer">
      <div id="page_index_bottomleft" class="hud_bottomleft"></div>
      <div id="page_index_bottom" class="hud_bottom"><span class="hud_span_bottom"></span></div>
      <div id="page_index_resize" class="hud_resize"></div>
    </div>
  </div>


	    &nbsp;
          </div>
          <div id="content_footer">
          </div>
        </div>
        <div class="clear"></div>
      </div>
      
    <div id="footer">
      <div id="footer_content">
        <div id="footer_strapline">
          perldoc.perl.org - Official documentation for the Perl programming language
        </div>
        <div id="footer_links">
          <div id="address">
            <p class="name">Contact details</p>
            <p class="address">
	      Site maintained by <a href="mailto:jj@jonallen.info">Jon Allen (JJ)</a><br>
	    </p>
            <p class="contact">
              Documentation maintained by the <a href="http://lists.cpan.org/showlist.cgi?name=perl5-porters">Perl 5 Porters</a>
            </p>
          </div>
          <ul class="f1">
            <li>Manual
              <ul class="f2">
                <li><a href="index-overview.html">Overview</a>
                <li><a href="index-tutorials.html">Tutorials</a>
                <li><a href="index-faq.html">FAQs</a>
                <li><a href="index-history.html">Changes</a>
              </ul>
            <li>Reference
              <ul class="f2">
                <li><a href="index-language.html">Language</a>
                <li><a href="index-functions.html">Functions</a>
                <li><a href="perlop.html">Operators</a>
                <li><a href="perlvar.html">Variables</a>
              </ul>
            <li>Modules
              <ul class="f2">
                <li><a href="index-modules-A.html">Modules</a>
                <li><a href="index-pragmas.html">Pragmas</a>
                <li><a href="index-utilities.html">Utilities</a>
              </ul>
            <li>Misc
              <ul class="f2">
                <li><a href="index-licence.html">License</a>
                <li><a href="index-internals.html">Internals</a>
                <li><a href="index-platforms.html">Platforms</a>
              </ul>          </ul>
          <div class="clear"></div>
        </div>
      </div>
      <div id="footer_end">
      </div>
    </div>
      
    </div>
      <script language="JavaScript" type="text/javascript" src="static/exploreperl.js"></script>
      <script language="JavaScript" src="static/combined-20100403.js" type="text/javascript"></script>
<script language="JavaScript" type="text/javascript">
  perldoc.setPath(0);
  perldoc.pageName    = 'perlre';
  perldoc.pageAddress = 'perlre.html';
  perldoc.contentPage = 1;
  explorePerl.render();
  explorePerl.addEvents('explore_anchor');
</script>
    
  </body>
</html>