File: perlrequick.html

package info (click to toggle)
perl-doc-html 5.26.0-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 39,400 kB
  • sloc: xml: 36; makefile: 7
file content (803 lines) | stat: -rw-r--r-- 57,159 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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <title>perlrequick - 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>perlrequick</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-tutorials.html">Tutorials</a> &gt;
      
    
    perlrequick
  

            </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>perlrequick</h1>


  <!--    -->
<ul><li><a href="#NAME">NAME</a><li><a href="#DESCRIPTION">DESCRIPTION</a><li><a href="#The-Guide">The Guide</a><ul><li><a href="#Simple-word-matching">Simple word matching</a><li><a href="#Using-character-classes">Using character classes</a><li><a href="#Matching-this-or-that">Matching this or that</a><li><a href="#Grouping-things-and-hierarchical-matching">Grouping things and hierarchical matching</a><li><a href="#Extracting-matches">Extracting matches</a><li><a href="#Matching-repetitions">Matching repetitions</a><li><a href="#More-matching">More matching</a><li><a href="#Search-and-replace">Search and replace</a><li><a href="#The-split-operator">The split operator</a><li><a href="#use-re-'strict'">use re 'strict'</a></ul><li><a href="#BUGS">BUGS</a><li><a href="#SEE-ALSO">SEE ALSO</a><li><a href="#AUTHOR-AND-COPYRIGHT">AUTHOR AND COPYRIGHT</a><ul><li><a href="#Acknowledgments">Acknowledgments</a></ul></ul><a name="NAME"></a><h1>NAME</h1>
<p>perlrequick - Perl regular expressions quick start</p>
<a name="DESCRIPTION"></a><h1>DESCRIPTION</h1>
<p>This page covers the very basics of understanding, creating and
using regular expressions ('regexes') in Perl.</p>
<a name="The-Guide"></a><h1>The Guide</h1>
<p>This page assumes you already know things, like what a "pattern" is, and
the basic syntax of using them.  If you don't, see <a href="perlretut.html">perlretut</a>.</p>
<a name="Simple-word-matching"></a><h2>Simple word matching</h2>
<p>The simplest regex is simply a word, or more generally, a string of
characters.  A regex consisting of a word matches any string that
contains that word:</p>
<pre class="verbatim"><ol><li>    <span class="q">&quot;Hello World&quot;</span> =~ <span class="q">/World/</span><span class="sc">;</span>  <span class="c"># matches</span></li></ol></pre><p>In this statement, <code class="inline"><span class="w">World</span></code>
 is a regex and the <code class="inline"><span class="q">//</span></code>
 enclosing
<code class="inline"><span class="q">/World/</span></code>
 tells Perl to search a string for a match.  The operator
<code class="inline">=~</code>
 associates the string with the regex match and produces a true
value if the regex matched, or false if the regex did not match.  In
our case, <code class="inline"><span class="w">World</span></code>
 matches the second word in <code class="inline"><span class="q">&quot;Hello World&quot;</span></code>
, so the
expression is true.  This idea has several variations.</p>
<p>Expressions like this are useful in conditionals:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;It matches\n&quot;</span> if <span class="q">&quot;Hello World&quot;</span> =~ <span class="q">/World/</span><span class="sc">;</span></li></ol></pre><p>The sense of the match can be reversed by using <code class="inline">!~</code>
 operator:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;It doesn&#39;t match\n&quot;</span> if <span class="q">&quot;Hello World&quot;</span> !~ <span class="q">/World/</span><span class="sc">;</span></li></ol></pre><p>The literal string in the regex can be replaced by a variable:</p>
<pre class="verbatim"><ol><li>    <span class="i">$greeting</span> = <span class="q">&quot;World&quot;</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;It matches\n&quot;</span> if <span class="q">&quot;Hello World&quot;</span> =~ <span class="q">/$greeting/</span><span class="sc">;</span></li></ol></pre><p>If you're matching against <code class="inline"><span class="i">$_</span></code>
, the <code class="inline"><span class="i">$_</span> =~</code>
 part can be omitted:</p>
<pre class="verbatim"><ol><li>    <span class="i">$_</span> = <span class="q">&quot;Hello World&quot;</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;It matches\n&quot;</span> if <span class="q">/World/</span><span class="sc">;</span></li></ol></pre><p>Finally, the <code class="inline"><span class="q">//</span></code>
 default delimiters for a match can be changed to
arbitrary delimiters by putting an <code class="inline"><span class="q">&#39;m&#39;</span></code>
 out front:</p>
<pre class="verbatim"><ol><li>    <span class="q">&quot;Hello World&quot;</span> =~ <span class="q">m!World!</span><span class="sc">;</span>   <span class="c"># matches, delimited by &#39;!&#39;</span></li><li>    <span class="q">&quot;Hello World&quot;</span> =~ <span class="q">m{World}</span><span class="sc">;</span>   <span class="c"># matches, note the matching &#39;{}&#39;</span></li><li>    <span class="q">&quot;/usr/bin/perl&quot;</span> =~ <span class="q">m&quot;/perl&quot;</span><span class="sc">;</span> <span class="c"># matches after &#39;/usr/bin&#39;,</span></li><li>                                 <span class="c"># &#39;/&#39; becomes an ordinary char</span></li></ol></pre><p>Regexes must match a part of the string <i>exactly</i> in order for the
statement to be true:</p>
<pre class="verbatim"><ol><li>    <span class="q">&quot;Hello World&quot;</span> =~ <span class="q">/world/</span><span class="sc">;</span>  <span class="c"># doesn&#39;t match, case sensitive</span></li><li>    <span class="q">&quot;Hello World&quot;</span> =~ <span class="q">/o W/</span><span class="sc">;</span>    <span class="c"># matches, &#39; &#39; is an ordinary char</span></li><li>    <span class="q">&quot;Hello World&quot;</span> =~ <span class="q">/World /</span><span class="sc">;</span> <span class="c"># doesn&#39;t match, no &#39; &#39; at end</span></li></ol></pre><p>Perl will always match at the earliest possible point in the string:</p>
<pre class="verbatim"><ol><li>    <span class="q">&quot;Hello World&quot;</span> =~ <span class="q">/o/</span><span class="sc">;</span>       <span class="c"># matches &#39;o&#39; in &#39;Hello&#39;</span></li><li>    <span class="q">&quot;That hat is red&quot;</span> =~ <span class="q">/hat/</span><span class="sc">;</span> <span class="c"># matches &#39;hat&#39; in &#39;That&#39;</span></li></ol></pre><p>Not all characters can be used 'as is' in a match.  Some characters,
called <b>metacharacters</b>, are reserved for use in regex notation.
The metacharacters are</p>
<pre class="verbatim"><ol><li>    {}[]()^$.|*+?\</li></ol></pre><p>A metacharacter can be matched by putting a backslash before it:</p>
<pre class="verbatim"><ol><li>    <span class="q">&quot;2+2=4&quot;</span> =~ <span class="q">/2+2/</span><span class="sc">;</span>    <span class="c"># doesn&#39;t match, + is a metacharacter</span></li><li>    <span class="q">&quot;2+2=4&quot;</span> =~ <span class="q">/2\+2/</span><span class="sc">;</span>   <span class="c"># matches, \+ is treated like an ordinary +</span></li><li>    <span class="q">&#39;C:\WIN32&#39;</span> =~ <span class="q">/C:\\WIN/</span><span class="sc">;</span>                       <span class="c"># matches</span></li><li>    <span class="q">&quot;/usr/bin/perl&quot;</span> =~ <span class="q">/\/usr\/bin\/perl/</span><span class="sc">;</span>  <span class="c"># matches</span></li></ol></pre><p>In the last regex, the forward slash <code class="inline"><span class="q">&#39;/&#39;</span></code>
 is also backslashed,
because it is used to delimit the regex.</p>
<p>Non-printable ASCII characters are represented by <b>escape sequences</b>.
Common examples are <code class="inline">\<span class="w">t</span></code>
 for a tab, <code class="inline">\<span class="w">n</span></code>
 for a newline, and <code class="inline">\<span class="w">r</span></code>

for a carriage return.  Arbitrary bytes are represented by octal
escape sequences, e.g., <code class="inline">\<span class="n">033</span></code>
, or hexadecimal escape sequences,
e.g., <code class="inline">\<span class="w">x1B</span></code>
:</p>
<pre class="verbatim"><ol><li>    <span class="q">&quot;1000\t2000&quot;</span> =~ <span class="q">m(0\t2)</span>  <span class="c"># matches</span></li><li>    <span class="q">&quot;cat&quot;</span> =~ <span class="q">/\143\x61\x74/</span>  <span class="c"># matches in ASCII, but </span></li><li>                             <span class="c"># a weird way to spell cat</span></li></ol></pre><p>Regexes are treated mostly as double-quoted strings, so variable
substitution works:</p>
<pre class="verbatim"><ol><li>    <span class="i">$foo</span> = <span class="q">&#39;house&#39;</span><span class="sc">;</span></li><li>    <span class="q">&#39;cathouse&#39;</span> =~ <span class="q">/cat$foo/</span><span class="sc">;</span>   <span class="c"># matches</span></li><li>    <span class="q">&#39;housecat&#39;</span> =~ <span class="q">/${foo}cat/</span><span class="sc">;</span> <span class="c"># matches</span></li></ol></pre><p>With all of the regexes above, if the regex matched anywhere in the
string, it was considered a match.  To specify <i>where</i> it should
match, we would use the <b>anchor</b> metacharacters <code class="inline">^</code> and <code class="inline"><span class="i">$</span></code>
.  The
anchor <code class="inline">^</code> means match at the beginning of the string and the anchor
<code class="inline"><span class="i">$</span></code>
 means match at the end of the string, or before a newline at the
end of the string.  Some examples:</p>
<pre class="verbatim"><ol><li>    <span class="q">&quot;housekeeper&quot;</span> =~ <span class="q">/keeper/</span><span class="sc">;</span>         <span class="c"># matches</span></li><li>    <span class="q">&quot;housekeeper&quot;</span> =~ <span class="q">/^keeper/</span><span class="sc">;</span>        <span class="c"># doesn&#39;t match</span></li><li>    <span class="q">&quot;housekeeper&quot;</span> =~ <span class="q">/keeper$/</span><span class="sc">;</span>        <span class="c"># matches</span></li><li>    <span class="q">&quot;housekeeper\n&quot;</span> =~ <span class="q">/keeper$/</span><span class="sc">;</span>      <span class="c"># matches</span></li><li>    <span class="q">&quot;housekeeper&quot;</span> =~ <span class="q">/^housekeeper$/</span><span class="sc">;</span>  <span class="c"># matches</span></li></ol></pre><a name="Using-character-classes"></a><h2>Using character classes</h2>
<p>A <b>character class</b> allows a set of possible characters, rather than
just a single character, to match at a particular point in a regex.
Character classes are denoted by brackets <code class="inline"><span class="s">[</span>...<span class="s">]</span></code>
, with the set of
characters to be possibly matched inside.  Here are some examples:</p>
<pre class="verbatim"><ol><li>    <span class="q">/cat/</span><span class="sc">;</span>            <span class="c"># matches &#39;cat&#39;</span></li><li>    <span class="q">/[bcr]at/</span><span class="sc">;</span>        <span class="c"># matches &#39;bat&#39;, &#39;cat&#39;, or &#39;rat&#39;</span></li><li>    <span class="q">&quot;abc&quot;</span> =~ <span class="q">/[cab]/</span><span class="sc">;</span> <span class="c"># matches &#39;a&#39;</span></li></ol></pre><p>In the last statement, even though <code class="inline"><span class="q">&#39;c&#39;</span></code>
 is the first character in
the class, the earliest point at which the regex can match is <code class="inline"><span class="q">&#39;a&#39;</span></code>
.</p>
<pre class="verbatim"><ol><li>    <span class="q">/[yY][eE][sS]/</span><span class="sc">;</span> <span class="c"># match &#39;yes&#39; in a case-insensitive way</span></li><li>                    <span class="c"># &#39;yes&#39;, &#39;Yes&#39;, &#39;YES&#39;, etc.</span></li><li>    <span class="q">/yes/i</span><span class="sc">;</span>         <span class="c"># also match &#39;yes&#39; in a case-insensitive way</span></li></ol></pre><p>The last example shows a match with an <code class="inline"><span class="q">&#39;i&#39;</span></code>
 <b>modifier</b>, which makes
the match case-insensitive.</p>
<p>Character classes also have ordinary and special characters, but the
sets of ordinary and special characters inside a character class are
different than those outside a character class.  The special
characters for a character class are <code class="inline">-]\^$</code> and are matched using an
escape:</p>
<pre class="verbatim"><ol><li>   <span class="q">/[\]c]def/</span><span class="sc">;</span> <span class="c"># matches &#39;]def&#39; or &#39;cdef&#39;</span></li><li>   <span class="i">$x</span> = <span class="q">&#39;bcr&#39;</span><span class="sc">;</span></li><li>   <span class="q">/[$x]at/</span><span class="sc">;</span>   <span class="c"># matches &#39;bat, &#39;cat&#39;, or &#39;rat&#39;</span></li><li>   <span class="q">/[\$x]at/</span><span class="sc">;</span>  <span class="c"># matches &#39;$at&#39; or &#39;xat&#39;</span></li><li>   <span class="q">/[\\$x]at/</span><span class="sc">;</span> <span class="c"># matches &#39;\at&#39;, &#39;bat, &#39;cat&#39;, or &#39;rat&#39;</span></li></ol></pre><p>The special character <code class="inline"><span class="q">&#39;-&#39;</span></code>
 acts as a range operator within character
classes, so that the unwieldy <code class="inline">[0123456789]</code> and <code class="inline"><span class="s">[</span><span class="w">abc</span>...<span class="w">xyz</span><span class="s">]</span></code>

become the svelte <code class="inline"><span class="s">[</span><span class="n">0</span>-<span class="n">9</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>
:</p>
<pre class="verbatim"><ol><li>    <span class="q">/item[0-9]/</span><span class="sc">;</span>  <span class="c"># matches &#39;item0&#39; or ... or &#39;item9&#39;</span></li><li>    <span class="q">/[0-9a-fA-F]/</span><span class="sc">;</span>  <span class="c"># matches a hexadecimal digit</span></li></ol></pre><p>If <code class="inline"><span class="q">&#39;-&#39;</span></code>
 is the first or last character in a character class, it is
treated as an ordinary character.</p>
<p>The special character <code class="inline">^</code> in the first position of a character class
denotes a <b>negated character class</b>, which matches any character but
those in the brackets.  Both <code class="inline"><span class="s">[</span>...<span class="s">]</span></code>
 and <code class="inline">[^...]</code> must match a
character, or the match fails.  Then</p>
<pre class="verbatim"><ol><li>    <span class="q">/[^a]at/</span><span class="sc">;</span>  <span class="c"># doesn&#39;t match &#39;aat&#39; or &#39;at&#39;, but matches</span></li><li>               <span class="c"># all other &#39;bat&#39;, &#39;cat, &#39;0at&#39;, &#39;%at&#39;, etc.</span></li><li>    <span class="q">/[^0-9]/</span><span class="sc">;</span>  <span class="c"># matches a non-numeric character</span></li><li>    <span class="q">/[a^]at/</span><span class="sc">;</span>  <span class="c"># matches &#39;aat&#39; or &#39;^at&#39;; here &#39;^&#39; is ordinary</span></li></ol></pre><p>Perl has several abbreviations for common character classes. (These
definitions are those that Perl uses in ASCII-safe mode with the <code class="inline"><span class="q">/a</span></code>
 modifier.
Otherwise they could match many more non-ASCII Unicode characters as
well.  See <a href="perlrecharclass.html#Backslash-sequences">Backslash sequences in perlrecharclass</a> for details.)</p>
<ul>
<li>
<p>\d is a digit and represents</p>
<pre class="verbatim"><ol><li>    <span class="s">[</span><span class="n">0</span>-<span class="n">9</span><span class="s">]</span></li></ol></pre></li>
<li>
<p>\s is a whitespace character and represents</p>
<pre class="verbatim"><ol><li>    <span class="s">[</span>\ \<span class="w">t</span>\<span class="w">r</span>\<span class="w">n</span>\<span class="w">f</span><span class="s">]</span></li></ol></pre></li>
<li>
<p>\w is a word character (alphanumeric or _) and represents</p>
<pre class="verbatim"><ol><li>    [0-9a-zA-Z_]</li></ol></pre></li>
<li>
<p>\D is a negated \d; it represents any character but a digit</p>
<pre class="verbatim"><ol><li>    [^0-9]</li></ol></pre></li>
<li>
<p>\S is a negated \s; it represents any non-whitespace character</p>
<pre class="verbatim"><ol><li>    [^\s]</li></ol></pre></li>
<li>
<p>\W is a negated \w; it represents any non-word character</p>
<pre class="verbatim"><ol><li>    [^\w]</li></ol></pre></li>
<li>
<p>The period '.' matches any character but "\n"</p>
</li>
</ul>
<p>The <code class="inline">\d\s\w\D\S\W</code> abbreviations can be used both inside and outside
of character classes.  Here are some in use:</p>
<pre class="verbatim"><ol><li>    <span class="q">/\d\d:\d\d:\d\d/</span><span class="sc">;</span> <span class="c"># matches a hh:mm:ss time format</span></li><li>    <span class="q">/[\d\s]/</span><span class="sc">;</span>         <span class="c"># matches any digit or whitespace character</span></li><li>    <span class="q">/\w\W\w/</span><span class="sc">;</span>         <span class="c"># matches a word char, followed by a</span></li><li>                      <span class="c"># non-word char, followed by a word char</span></li><li>    <span class="q">/..rt/</span><span class="sc">;</span>           <span class="c"># matches any two chars, followed by &#39;rt&#39;</span></li><li>    <span class="q">/end\./</span><span class="sc">;</span>          <span class="c"># matches &#39;end.&#39;</span></li><li>    <span class="q">/end[.]/</span><span class="sc">;</span>         <span class="c"># same thing, matches &#39;end.&#39;</span></li></ol></pre><p>The <b>word anchor</b>  <code class="inline">\<span class="w">b</span></code>
 matches a boundary between a word
character and a non-word character <code class="inline">\<span class="w">w</span>\<span class="w">W</span></code>
 or <code class="inline">\<span class="w">W</span>\<span class="w">w</span></code>
:</p>
<pre class="verbatim"><ol><li>    <span class="i">$x</span> = <span class="q">&quot;Housecat catenates house and cat&quot;</span><span class="sc">;</span></li><li>    <span class="i">$x</span> =~ <span class="q">/\bcat/</span><span class="sc">;</span>  <span class="c"># matches cat in &#39;catenates&#39;</span></li><li>    <span class="i">$x</span> =~ <span class="q">/cat\b/</span><span class="sc">;</span>  <span class="c"># matches cat in &#39;housecat&#39;</span></li><li>    <span class="i">$x</span> =~ <span class="q">/\bcat\b/</span><span class="sc">;</span>  <span class="c"># matches &#39;cat&#39; at end of string</span></li></ol></pre><p>In the last example, the end of the string is considered a word
boundary.</p>
<p>For natural language processing (so that, for example, apostrophes are
included in words), use instead <code class="inline">\<span class="i">b</span><span class="s">{</span><span class="w">wb</span><span class="s">}</span></code>
</p>
<pre class="verbatim"><ol><li>    <span class="q">&quot;don&#39;t&quot;</span> =~ <span class="q">/ .+? \b{wb} /x</span><span class="sc">;</span>  <span class="c"># matches the whole string</span></li></ol></pre><a name="Matching-this-or-that"></a><h2>Matching this or that</h2>
<p>We can match different character strings with the <b>alternation</b>
metacharacter <code class="inline"><span class="q">&#39;|&#39;</span></code>
.  To match <code class="inline"><span class="w">dog</span></code>
 or <code class="inline"><span class="w">cat</span></code>
, we form the regex
<code class="inline"><span class="w">dog</span>|<span class="w">cat</span></code>
.  As before, Perl will try to match the regex at the
earliest possible point in the string.  At each character position,
Perl will first try to match the first alternative, <code class="inline"><span class="w">dog</span></code>
.  If
<code class="inline"><span class="w">dog</span></code>
 doesn't match, Perl will then try the next alternative, <code class="inline"><span class="w">cat</span></code>
.
If <code class="inline"><span class="w">cat</span></code>
 doesn't match either, then the match fails and Perl moves to
the next position in the string.  Some examples:</p>
<pre class="verbatim"><ol><li>    <span class="q">&quot;cats and dogs&quot;</span> =~ <span class="q">/cat|dog|bird/</span><span class="sc">;</span>  <span class="c"># matches &quot;cat&quot;</span></li><li>    <span class="q">&quot;cats and dogs&quot;</span> =~ <span class="q">/dog|cat|bird/</span><span class="sc">;</span>  <span class="c"># matches &quot;cat&quot;</span></li></ol></pre><p>Even though <code class="inline"><span class="w">dog</span></code>
 is the first alternative in the second regex,
<code class="inline"><span class="w">cat</span></code>
 is able to match earlier in the string.</p>
<pre class="verbatim"><ol><li>    <span class="q">&quot;cats&quot;</span>          =~ <span class="q">/c|ca|cat|cats/</span><span class="sc">;</span> <span class="c"># matches &quot;c&quot;</span></li><li>    <span class="q">&quot;cats&quot;</span>          =~ <span class="q">/cats|cat|ca|c/</span><span class="sc">;</span> <span class="c"># matches &quot;cats&quot;</span></li></ol></pre><p>At a given character position, the first alternative that allows the
regex match to succeed will be the one that matches. Here, all the
alternatives match at the first string position, so the first matches.</p>
<a name="Grouping-things-and-hierarchical-matching"></a><h2>Grouping things and hierarchical matching</h2>
<p>The <b>grouping</b> metacharacters <code class="inline"><span class="s">(</span><span class="s">)</span></code>
 allow a part of a regex to be
treated as a single unit.  Parts of a regex are grouped by enclosing
them in parentheses.  The regex <code class="inline"><span class="i">house</span><span class="s">(</span><span class="w">cat</span>|<span class="w">keeper</span><span class="s">)</span></code>
 means match
<code class="inline"><span class="w">house</span></code>
 followed by either <code class="inline"><span class="w">cat</span></code>
 or <code class="inline"><span class="w">keeper</span></code>
.  Some more examples
are</p>
<pre class="verbatim"><ol><li>    <span class="q">/(a|b)b/</span><span class="sc">;</span>    <span class="c"># matches &#39;ab&#39; or &#39;bb&#39;</span></li><li>    <span class="q">/(^a|b)c/</span><span class="sc">;</span>   <span class="c"># matches &#39;ac&#39; at start of string or &#39;bc&#39; anywhere</span></li><li></li><li>    <span class="q">/house(cat|)/</span><span class="sc">;</span>  <span class="c"># matches either &#39;housecat&#39; or &#39;house&#39;</span></li><li>    <span class="q">/house(cat(s|)|)/</span><span class="sc">;</span>  <span class="c"># matches either &#39;housecats&#39; or &#39;housecat&#39; or</span></li><li>                        <span class="c"># &#39;house&#39;.  Note groups can be nested.</span></li><li></li><li>    <span class="q">&quot;20&quot;</span> =~ <span class="q">/(19|20|)\d\d/</span><span class="sc">;</span>  <span class="c"># matches the null alternative &#39;()\d\d&#39;,</span></li><li>                             <span class="c"># because &#39;20\d\d&#39; can&#39;t match</span></li></ol></pre><a name="Extracting-matches"></a><h2>Extracting matches</h2>
<p>The grouping metacharacters <code class="inline"><span class="s">(</span><span class="s">)</span></code>
 also allow the extraction of the
parts of a string that matched.  For each grouping, the part that
matched inside goes into the special variables <code class="inline"><span class="i">$1</span></code>
, <code class="inline"><span class="i">$2</span></code>
, etc.
They can be used just as ordinary variables:</p>
<pre class="verbatim"><ol><li>    <span class="c"># extract hours, minutes, seconds</span></li><li>    <span class="i">$time</span> =~ <span class="q">/(\d\d):(\d\d):(\d\d)/</span><span class="sc">;</span>  <span class="c"># match hh:mm:ss format</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></ol></pre><p>In list context, a match <code class="inline"><span class="q">/regex/</span></code>
 with groupings will return the
list of matched values <code class="inline"><span class="s">(</span><span class="i">$1</span><span class="cm">,</span><span class="i">$2</span><span class="cm">,</span>...<span class="s">)</span></code>
.  So we could rewrite it as</p>
<pre class="verbatim"><ol><li>    <span class="s">(</span><span class="i">$hours</span><span class="cm">,</span> <span class="i">$minutes</span><span class="cm">,</span> <span class="i">$second</span><span class="s">)</span> = <span class="s">(</span><span class="i">$time</span> =~ <span class="q">/(\d\d):(\d\d):(\d\d)/</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>If the groupings in a regex are nested, <code class="inline"><span class="i">$1</span></code>
 gets the group with the
leftmost opening parenthesis, <code class="inline"><span class="i">$2</span></code>
 the next opening parenthesis,
etc.  For example, here is a complex regex and the matching variables
indicated below it:</p>
<pre class="verbatim"><ol><li>    /(ab(cd|ef)((gi)|j))/;</li><li>     1  2      34</li></ol></pre><p>Associated with the matching variables <code class="inline"><span class="i">$1</span></code>
, <code class="inline"><span class="i">$2</span></code>
, ... are
the <b>backreferences</b> <code class="inline">\<span class="w">g1</span></code>
, <code class="inline">\<span class="w">g2</span></code>
, ...  Backreferences are
matching variables that can be used <i>inside</i> a regex:</p>
<pre class="verbatim"><ol><li>    <span class="q">/(\w\w\w)\s\g1/</span><span class="sc">;</span> <span class="c"># find sequences like &#39;the the&#39; in string</span></li></ol></pre><p><code class="inline"><span class="i">$1</span></code>
, <code class="inline"><span class="i">$2</span></code>
, ... should only be used outside of a regex, and <code class="inline">\<span class="w">g1</span></code>
,
<code class="inline">\<span class="w">g2</span></code>
, ... only inside a regex.</p>
<a name="Matching-repetitions"></a><h2>Matching repetitions</h2>
<p>The <b>quantifier</b> metacharacters <code class="inline">?</code>, <code class="inline"><span class="i">*</span></code>
, <code class="inline">+</code>
, and <code class="inline"><span class="s">{</span><span class="s">}</span></code>
 allow us
to determine the number of repeats of a portion of a regex we
consider to be a match.  Quantifiers are put immediately after the
character, character class, or grouping that we want to specify.  They
have the following meanings:</p>
<ul>
<li>
<p><code class="inline">a?</code> = match 'a' 1 or 0 times</p>
</li>
<li>
<p><code class="inline"><span class="w">a</span>*</code>
 = match 'a' 0 or more times, i.e., any number of times</p>
</li>
<li>
<p><code class="inline"><span class="w">a</span>+</code>
 = match 'a' 1 or more times, i.e., at least once</p>
</li>
<li>
<p><code class="inline">a{n,m}</code> = match at least <code class="inline"><span class="w">n</span></code>
 times, but not more than <code class="inline"><a class="l_k" href="functions/m.html">m</a></code>
times.</p>
</li>
<li>
<p><code class="inline"><span class="w">a</span><span class="s">{</span><span class="w">n</span><span class="cm">,</span><span class="s">}</span></code>
 = match at least <code class="inline"><span class="w">n</span></code>
 or more times</p>
</li>
<li>
<p><code class="inline"><span class="i">a</span><span class="s">{</span><span class="w">n</span><span class="s">}</span></code>
 = match exactly <code class="inline"><span class="w">n</span></code>
 times</p>
</li>
</ul>
<p>Here are some examples:</p>
<pre class="verbatim"><ol><li>    <span class="q">/[a-z]+\s+\d*/</span><span class="sc">;</span>  <span class="c"># match a lowercase word, at least some space, and</span></li><li>                     <span class="c"># any number of digits</span></li><li>    <span class="q">/(\w+)\s+\g1/</span><span class="sc">;</span>    <span class="c"># match doubled words of arbitrary length</span></li><li>    <span class="i">$year</span> =~ <span class="q">/^\d{2,4}$/</span><span class="sc">;</span>  <span class="c"># make sure year is at least 2 but not more</span></li><li>                           <span class="c"># than 4 digits</span></li><li>    <span class="i">$year</span> =~ <span class="q">/^\d{4}$|^\d{2}$/</span><span class="sc">;</span> <span class="c"># better match; throw out 3 digit dates</span></li></ol></pre><p>These quantifiers will try to match as much of the string as possible,
while still allowing the regex to match.  So we have</p>
<pre class="verbatim"><ol><li>    <span class="i">$x</span> = <span class="q">&#39;the cat in the hat&#39;</span><span class="sc">;</span></li><li>    <span class="i">$x</span> =~ <span class="q">/^(.*)(at)(.*)$/</span><span class="sc">;</span> <span class="c"># matches,</span></li><li>                            <span class="c"># $1 = &#39;the cat in the h&#39;</span></li><li>                            <span class="c"># $2 = &#39;at&#39;</span></li><li>                            <span class="c"># $3 = &#39;&#39;   (0 matches)</span></li></ol></pre><p>The first quantifier <code class="inline">.*</code> grabs as much of the string as possible
while still having the regex match. The second quantifier <code class="inline">.*</code> has
no string left to it, so it matches 0 times.</p>
<a name="More-matching"></a><h2>More matching</h2>
<p>There are a few more things you might want to know about matching
operators.
The global modifier <code class="inline">/g</code> allows the matching operator to match
within a string as many times as possible.  In scalar context,
successive matches against a string will have <code class="inline">/g</code> jump from match
to match, keeping track of position in the string as it goes along.
You can get or set the position with the <code class="inline"><a class="l_k" href="functions/pos.html">pos()</a></code> function.
For example,</p>
<pre class="verbatim"><ol><li>    <span class="i">$x</span> = <span class="q">&quot;cat dog house&quot;</span><span class="sc">;</span> <span class="c"># 3 words</span></li><li>    while <span class="s">(</span><span class="i">$x</span> =~ <span class="q">/(\w+)/g</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;Word is $1, ends at position &quot;</span><span class="cm">,</span> <a class="l_k" href="functions/pos.html">pos</a> <span class="i">$x</span><span class="cm">,</span> <span class="q">&quot;\n&quot;</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>prints</p>
<pre class="verbatim"><ol><li>    Word is cat, ends at position 3</li><li>    Word is dog, ends at position 7</li><li>    Word is house, ends at position 13</li></ol></pre><p>A failed match or changing the target string resets the position.  If
you don't want the position reset after failure to match, add the
<code class="inline">/c</code>, as in <code class="inline"><span class="q">/regex/gc</span></code>
.</p>
<p>In list context, <code class="inline">/g</code> returns a list of matched groupings, or if
there are no groupings, a list of matches to the whole regex.  So</p>
<pre class="verbatim"><ol><li>    <span class="i">@words</span> = <span class="s">(</span><span class="i">$x</span> =~ <span class="q">/(\w+)/g</span><span class="s">)</span><span class="sc">;</span>  <span class="c"># matches,</span></li><li>                                <span class="c"># $word[0] = &#39;cat&#39;</span></li><li>                                <span class="c"># $word[1] = &#39;dog&#39;</span></li><li>                                <span class="c"># $word[2] = &#39;house&#39;</span></li></ol></pre><a name="Search-and-replace"></a><h2>Search and replace</h2>
<p>Search and replace is performed using <code class="inline"><a class="l_k" href="functions/s.html">s/regex/replacement/modifiers</a></code>.
The <code class="inline"><span class="w">replacement</span></code>
 is a Perl double-quoted string that replaces in the
string whatever is matched with the <code class="inline"><span class="w">regex</span></code>
.  The operator <code class="inline">=~</code>
 is
also used here to associate a string with <code class="inline"><a class="l_k" href="functions/s.html">s///</a></code>.  If matching
against <code class="inline"><span class="i">$_</span></code>
, the <code class="inline"><span class="i">$_</span> =~</code>
 can be dropped.  If there is a match,
<code class="inline"><a class="l_k" href="functions/s.html">s///</a></code> returns the number of substitutions made; otherwise it returns
false.  Here are a few examples:</p>
<pre class="verbatim"><ol><li>    <span class="i">$x</span> = <span class="q">&quot;Time to feed the cat!&quot;</span><span class="sc">;</span></li><li>    <span class="i">$x</span> =~ <span class="q">s/cat/hacker/</span><span class="sc">;</span>   <span class="c"># $x contains &quot;Time to feed the hacker!&quot;</span></li><li>    <span class="i">$y</span> = <span class="q">&quot;&#39;quoted words&#39;&quot;</span><span class="sc">;</span></li><li>    <span class="i">$y</span> =~ <span class="q">s/^&#39;(.*)&#39;$/$1/</span><span class="sc">;</span>  <span class="c"># strip single quotes,</span></li><li>                           <span class="c"># $y contains &quot;quoted words&quot;</span></li></ol></pre><p>With the <code class="inline"><a class="l_k" href="functions/s.html">s///</a></code> operator, the matched variables <code class="inline"><span class="i">$1</span></code>
, <code class="inline"><span class="i">$2</span></code>
, etc.
are immediately available for use in the replacement expression. With
the global modifier, <code class="inline"><a class="l_k" href="functions/s.html">s///g</a></code> will search and replace all occurrences
of the regex in the string:</p>
<pre class="verbatim"><ol><li>    <span class="i">$x</span> = <span class="q">&quot;I batted 4 for 4&quot;</span><span class="sc">;</span></li><li>    <span class="i">$x</span> =~ <span class="q">s/4/four/</span><span class="sc">;</span>   <span class="c"># $x contains &quot;I batted four for 4&quot;</span></li><li>    <span class="i">$x</span> = <span class="q">&quot;I batted 4 for 4&quot;</span><span class="sc">;</span></li><li>    <span class="i">$x</span> =~ <span class="q">s/4/four/g</span><span class="sc">;</span>  <span class="c"># $x contains &quot;I batted four for four&quot;</span></li></ol></pre><p>The non-destructive modifier <code class="inline"><a class="l_k" href="functions/s.html">s///r</a></code> causes the result of the substitution
to be returned instead of modifying <code class="inline"><span class="i">$_</span></code>
 (or whatever variable the
substitute was bound to with <code class="inline">=~</code>
):</p>
<pre class="verbatim"><ol><li>    <span class="i">$x</span> = <span class="q">&quot;I like dogs.&quot;</span><span class="sc">;</span></li><li>    <span class="i">$y</span> = <span class="i">$x</span> =~ <span class="q">s/dogs/cats/</span><span class="w">r</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;$x $y\n&quot;</span><span class="sc">;</span> <span class="c"># prints &quot;I like dogs. I like cats.&quot;</span></li><li></li><li>    <span class="i">$x</span> = <span class="q">&quot;Cats are great.&quot;</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/print.html">print</a> <span class="i">$x</span> =~ <span class="q">s/Cats/Dogs/</span><span class="w">r</span> =~ <span class="q">s/Dogs/Frogs/</span><span class="w">r</span> =~</li><li>        <span class="q">s/Frogs/Hedgehogs/</span><span class="w">r</span><span class="cm">,</span> <span class="q">&quot;\n&quot;</span><span class="sc">;</span></li><li>    <span class="c"># prints &quot;Hedgehogs are great.&quot;</span></li><li></li><li>    <span class="i">@foo</span> = <a class="l_k" href="functions/map.html">map</a> <span class="s">{</span> <span class="q">s/[a-z]/X/</span><span class="w">r</span> <span class="s">}</span> <span class="q">qw(a b c 1 2 3)</span><span class="sc">;</span></li><li>    <span class="c"># @foo is now qw(X X X 1 2 3)</span></li></ol></pre><p>The evaluation modifier <code class="inline"><a class="l_k" href="functions/s.html">s///e</a></code> wraps an <code class="inline"><a class="l_k" href="functions/eval.html">eval{...}</a></code> around the
replacement string and the evaluated result is substituted for the
matched substring.  Some examples:</p>
<pre class="verbatim"><ol><li>    <span class="c"># reverse all the words in a string</span></li><li>    <span class="i">$x</span> = <span class="q">&quot;the cat in the hat&quot;</span><span class="sc">;</span></li><li>    <span class="i">$x</span> =~ <span class="q">s/(\w+)/reverse $1/ge</span><span class="sc">;</span>   <span class="c"># $x contains &quot;eht tac ni eht tah&quot;</span></li><li></li><li>    <span class="c"># convert percentage to decimal</span></li><li>    <span class="i">$x</span> = <span class="q">&quot;A 39% hit rate&quot;</span><span class="sc">;</span></li><li>    <span class="i">$x</span> =~ <span class="q">s!(\d+)%!$1/100!e</span><span class="sc">;</span>       <span class="c"># $x contains &quot;A 0.39 hit rate&quot;</span></li></ol></pre><p>The last example shows that <code class="inline"><a class="l_k" href="functions/s.html">s///</a></code> can use other delimiters, such as
<code class="inline"><a class="l_k" href="functions/s.html">s!!!</a></code> and <code class="inline"><a class="l_k" href="functions/s.html">s{}{}</a></code>, and even <code class="inline"><a class="l_k" href="functions/s.html">s{}//</a></code>.  If single quotes are used
<code class="inline"><a class="l_k" href="functions/s.html">s'''</a></code>, then the regex and replacement are treated as single-quoted
strings.</p>
<a name="The-split-operator"></a><h2>The split operator</h2>
<p><code class="inline"><a class="l_k" href="functions/split.html">split</a> <span class="q">/regex/</span><span class="cm">,</span> <span class="w">string</span></code>
 splits <code class="inline"><span class="w">string</span></code>
 into a list of substrings
and returns that list.  The regex determines the character sequence
that <code class="inline"><span class="w">string</span></code>
 is split with respect to.  For example, to split a
string into words, use</p>
<pre class="verbatim"><ol><li>    <span class="i">$x</span> = <span class="q">&quot;Calvin and Hobbes&quot;</span><span class="sc">;</span></li><li>    <span class="i">@word</span> = <a class="l_k" href="functions/split.html">split</a> <span class="q">/\s+/</span><span class="cm">,</span> <span class="i">$x</span><span class="sc">;</span>  <span class="c"># $word[0] = &#39;Calvin&#39;</span></li><li>                              <span class="c"># $word[1] = &#39;and&#39;</span></li><li>                              <span class="c"># $word[2] = &#39;Hobbes&#39;</span></li></ol></pre><p>To extract a comma-delimited list of numbers, use</p>
<pre class="verbatim"><ol><li>    <span class="i">$x</span> = <span class="q">&quot;1.618,2.718,   3.142&quot;</span><span class="sc">;</span></li><li>    <span class="i">@const</span> = <a class="l_k" href="functions/split.html">split</a> <span class="q">/,\s*/</span><span class="cm">,</span> <span class="i">$x</span><span class="sc">;</span>  <span class="c"># $const[0] = &#39;1.618&#39;</span></li><li>                                <span class="c"># $const[1] = &#39;2.718&#39;</span></li><li>                                <span class="c"># $const[2] = &#39;3.142&#39;</span></li></ol></pre><p>If the empty regex <code class="inline"><span class="q">//</span></code>
 is used, the string is split into individual
characters.  If the regex has groupings, then the list produced contains
the matched substrings from the groupings as well:</p>
<pre class="verbatim"><ol><li>    <span class="i">$x</span> = <span class="q">&quot;/usr/bin&quot;</span><span class="sc">;</span></li><li>    <span class="i">@parts</span> = <a class="l_k" href="functions/split.html">split</a> <span class="q">m!(/)!</span><span class="cm">,</span> <span class="i">$x</span><span class="sc">;</span>  <span class="c"># $parts[0] = &#39;&#39;</span></li><li>                                <span class="c"># $parts[1] = &#39;/&#39;</span></li><li>                                <span class="c"># $parts[2] = &#39;usr&#39;</span></li><li>                                <span class="c"># $parts[3] = &#39;/&#39;</span></li><li>                                <span class="c"># $parts[4] = &#39;bin&#39;</span></li></ol></pre><p>Since the first character of $x matched the regex, <code class="inline"><a class="l_k" href="functions/split.html">split</a></code> prepended
an empty initial element to the list.</p>
<a name="use-re-'strict'"></a><h2><code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">re</span> <span class="q">&#39;strict&#39;</span></code>
</h2>
<p>New in v5.22, this applies stricter rules than otherwise when compiling
regular expression patterns.  It can find things that, while legal, may
not be what you intended.</p>
<p>See <a href="re.html#'strict'-mode">'strict' in re</a>.</p>
<a name="BUGS"></a><h1>BUGS</h1>
<p>None.</p>
<a name="SEE-ALSO"></a><h1>SEE ALSO</h1>
<p>This is just a quick start guide.  For a more in-depth tutorial on
regexes, see <a href="perlretut.html">perlretut</a> and for the reference page, see <a href="perlre.html">perlre</a>.</p>
<a name="AUTHOR-AND-COPYRIGHT"></a><h1>AUTHOR AND COPYRIGHT</h1>
<p>Copyright (c) 2000 Mark Kvale
All rights reserved.</p>
<p>This document may be distributed under the same terms as Perl itself.</p>
<a name="Acknowledgments"></a><h2>Acknowledgments</h2>
<p>The author would like to thank Mark-Jason Dominus, Tom Christiansen,
Ilya Zakharevich, Brad Hughes, and Mike Giroux for all their helpful
comments.</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><li><a href="#The-Guide">The Guide</a><ul><li><a href="#Simple-word-matching">Simple word matching</a><li><a href="#Using-character-classes">Using character classes</a><li><a href="#Matching-this-or-that">Matching this or that</a><li><a href="#Grouping-things-and-hierarchical-matching">Grouping things and hierarchical matching</a><li><a href="#Extracting-matches">Extracting matches</a><li><a href="#Matching-repetitions">Matching repetitions</a><li><a href="#More-matching">More matching</a><li><a href="#Search-and-replace">Search and replace</a><li><a href="#The-split-operator">The split operator</a><li><a href="#use-re-'strict'">use re 'strict'</a></ul><li><a href="#BUGS">BUGS</a><li><a href="#SEE-ALSO">SEE ALSO</a><li><a href="#AUTHOR-AND-COPYRIGHT">AUTHOR AND COPYRIGHT</a><ul><li><a href="#Acknowledgments">Acknowledgments</a></ul></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    = 'perlrequick';
  perldoc.pageAddress = 'perlrequick.html';
  perldoc.contentPage = 1;
  explorePerl.render();
  explorePerl.addEvents('explore_anchor');
</script>
    
  </body>
</html>