-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfile.regexp.html
709 lines (547 loc) · 66.1 KB
/
file.regexp.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset='UTF-8'>
<meta name='viewport' content='width=device-width, initial-scale=1.0, user-scalable=no'>
<meta name='apple-touch-fullscreen' content='yes'>
<meta name='apple-mobile-web-app-capable' content='yes'>
<meta name='apple-mobile-web-app-status-bar-style' content='rgba(228,228,228,1.0)'>
<title>File: Regexp — Ruby-2.6.10</title>
<link rel='stylesheet' type='text/css' href='../css/y_fonts.css' />
<link rel='stylesheet' type='text/css' href='../css/highlight.github.css' />
<link rel='stylesheet' type='text/css' href='../css/y_style.css' />
<link rel='stylesheet' type='text/css' href='../css/y_list.css' />
<link rel='stylesheet' type='text/css' href='../css/y_color.css' />
<script type='text/javascript'>
var pathId = "regexp",
relpath = '';
var t2Info = {
CSEP: '.',
ISEP: '#',
NSEP: '::'
};
</script>
<script type='text/javascript' charset='utf-8' src='../js/highlight.pack.js'></script>
<script type='text/javascript' charset='utf-8' src='../js/y_app.js'></script>
</head>
<body>
<svg id='y_wait' class viewBox='0 0 90 90'></svg>
<div id='settings' class='hidden'></div>
<div id='y_list' class='d h'>
<header id='list_header'></header>
<nav id= 'list_nav' class='y_nav l_nav'>
<ul id='list_items'></ul>
</nav>
</div>
<div id='y_toc' class='f h'>
<header id='toc_header'></header>
<nav id= 'toc_nav' class='y_nav t_nav'>
<ol id='toc_items'></ol>
</nav>
</div>
<div id='y_main' tabindex='-1'>
<header id='y_header'>
<div id='y_menu'>
<a id='home_no_xhr' href='/'>Home</a> »
<a href='.'>Ruby-2.6.10</a> »
<a href='_index.html'>Index</a> »
<span class='title'><a id='t2_doc_top' href='#'>File: Regexp ▲</a></span>
</div>
<a id='list_href' href="file_list.html"></a>
<div id='y_measure_em' class='y_measure'></div>
<div id='y_measure_vh' class='y_measure'></div>
<span id='y_measure_50pre' class='y_measure'><code>123456789_123456789_123456789_123456789_123456789_</code></span>
</header>
<div id='content' class='file'>
<p>Regular expressions (<em>regexp</em>s) are patterns which describe the contents of a string. They’re used for testing whether a string contains a given pattern, or extracting the portions that match. They are created with the <code>/</code><em>pat</em><code>/</code> and <code>%r{</code><em>pat</em><code>}</code> literals or the <code>Regexp.new</code> constructor.</p>
<p>A regexp is usually delimited with forward slashes (<code>/</code>). For example:</p>
<pre class="code ruby"><code class="ruby"><span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>hay</span><span class='regexp_end'>/</span></span> <span class='op'>=~</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>haystack</span><span class='tstring_end'>'</span></span> <span class='comment'>#=> 0
</span><span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>y</span><span class='regexp_end'>/</span></span>.<span class='id identifier rubyid_match'>match</span>(<span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>haystack</span><span class='tstring_end'>'</span></span>) <span class='comment'>#=> #<MatchData "y"></span></code></pre>
<p>If a string contains the pattern it is said to <em>match</em>. A literal string matches itself.</p>
<p>Here ‘haystack’ does not contain the pattern ‘needle’, so it doesn’t match:</p>
<pre class="code ruby"><code class="ruby"><span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>needle</span><span class='regexp_end'>/</span></span>.<span class='id identifier rubyid_match'>match</span>(<span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>haystack</span><span class='tstring_end'>'</span></span>) <span class='comment'>#=> nil</span></code></pre>
<p>Here ‘haystack’ contains the pattern ‘hay’, so it matches:</p>
<pre class="code ruby"><code class="ruby"><span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>hay</span><span class='regexp_end'>/</span></span>.<span class='id identifier rubyid_match'>match</span>(<span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>haystack</span><span class='tstring_end'>'</span></span>) <span class='comment'>#=> #<MatchData "hay"></span></code></pre>
<p>Specifically, <code>/st/</code> requires that the string contains the letter <em>s</em> followed by the letter <em>t</em>, so it matches <em>haystack</em>, also.</p>
<h2 id="label-3D~+and+Regexp-23match"><code>=~</code> and Regexp#match</h2>
<p>Pattern matching may be achieved by using <code>=~</code> operator or Regexp#match method.</p>
<h3 id="label-3D~+operator"><code>=~</code> operator</h3>
<p><code>=~</code> is Ruby’s basic pattern-matching operator. When one operand is a regular expression and the other is a string then the regular expression is used as a pattern to match against the string. (This operator is equivalently defined by Regexp and String so the order of String and Regexp do not matter. Other classes may have different implementations of <code>=~</code>.) If a match is found, the operator returns index of first match in string, otherwise it returns <code>nil</code>.</p>
<pre class="code ruby"><code class="ruby"><span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>hay</span><span class='regexp_end'>/</span></span> <span class='op'>=~</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>haystack</span><span class='tstring_end'>'</span></span> <span class='comment'>#=> 0
</span><span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>haystack</span><span class='tstring_end'>'</span></span> <span class='op'>=~</span> <span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>hay</span><span class='regexp_end'>/</span></span> <span class='comment'>#=> 0
</span><span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>a</span><span class='regexp_end'>/</span></span> <span class='op'>=~</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>haystack</span><span class='tstring_end'>'</span></span> <span class='comment'>#=> 1
</span><span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>u</span><span class='regexp_end'>/</span></span> <span class='op'>=~</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>haystack</span><span class='tstring_end'>'</span></span> <span class='comment'>#=> nil</span></code></pre>
<p>Using <code>=~</code> operator with a String and Regexp the <code>$~</code> global variable is set after a successful match. <code>$~</code> holds a MatchData object. Regexp.last_match is equivalent to <code>$~</code>.</p>
<h3 id="label-Regexp-23match+method">Regexp#match method</h3>
<p>The #match method returns a MatchData object:</p>
<pre class="code ruby"><code class="ruby"><span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>st</span><span class='regexp_end'>/</span></span>.<span class='id identifier rubyid_match'>match</span>(<span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>haystack</span><span class='tstring_end'>'</span></span>) <span class='comment'>#=> #<MatchData "st"></span></code></pre>
<h2 id="label-Metacharacters+and+Escapes">Metacharacters and Escapes</h2>
<p>The following are <em>metacharacters</em> <code>(</code>, <code>)</code>, <code>[</code>, <code>]</code>, <code>{</code>, <code>}</code>, <code>.</code>, <code>?</code>, <code>+</code>, <code>*</code>. They have a specific meaning when appearing in a pattern. To match them literally they must be backslash-escaped. To match a backslash literally, backslash-escape it: <code>\\</code>.</p>
<pre class="code ruby"><code class="ruby"><span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>1 \+ 2 = 3\?</span><span class='regexp_end'>/</span></span>.<span class='id identifier rubyid_match'>match</span>(<span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>Does 1 + 2 = 3?</span><span class='tstring_end'>'</span></span>) <span class='comment'>#=> #<MatchData "1 + 2 = 3?">
</span><span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>a\\\\b</span><span class='regexp_end'>/</span></span>.<span class='id identifier rubyid_match'>match</span>(<span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>a\\\\b</span><span class='tstring_end'>'</span></span>) <span class='comment'>#=> #<MatchData "a\\b"></span></code></pre>
<p>Patterns behave like double-quoted strings so can contain the same backslash escapes.</p>
<pre class="code ruby"><code class="ruby"><span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>\s\u{6771 4eac 90fd}</span><span class='regexp_end'>/</span></span>.<span class='id identifier rubyid_match'>match</span>(<span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>Go to 東京都</span><span class='tstring_end'>"</span></span>)
<span class='comment'>#=> #<MatchData " 東京都"></span></code></pre>
<p>Arbitrary Ruby expressions can be embedded into patterns with the <code>#{...}</code> construct.</p>
<pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_place'>place</span> <span class='op'>=</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>東京都</span><span class='tstring_end'>"</span></span>
<span class='tstring'><span class='regexp_beg'>/</span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_place'>place</span><span class='embexpr_end'>}</span><span class='regexp_end'>/</span></span>.<span class='id identifier rubyid_match'>match</span>(<span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>Go to 東京都</span><span class='tstring_end'>"</span></span>)
<span class='comment'>#=> #<MatchData "東京都"></span></code></pre>
<h2 id="label-Character+Classes">Character Classes</h2>
<p>A <em>character class</em> is delimited with square brackets (<code>[</code>, <code>]</code>) and lists characters that may appear at that point in the match. <code>/[ab]/</code> means <em>a</em> or <em>b</em>, as opposed to <code>/ab/</code> which means <em>a</em> followed by <em>b</em>.</p>
<pre class="code ruby"><code class="ruby"><span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>W[aeiou]rd</span><span class='regexp_end'>/</span></span>.<span class='id identifier rubyid_match'>match</span>(<span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>Word</span><span class='tstring_end'>"</span></span>) <span class='comment'>#=> #<MatchData "Word"></span></code></pre>
<p>Within a character class the hyphen (<code>-</code>) is a metacharacter denoting an inclusive range of characters. <code>[abcd]</code> is equivalent to <code>[a-d]</code>. A range can be followed by another range, so <code>[abcdwxyz]</code> is equivalent to <code>[a-dw-z]</code>. The order in which ranges or individual characters appear inside a character class is irrelevant.</p>
<pre class="code ruby"><code class="ruby"><span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>[0-9a-f]</span><span class='regexp_end'>/</span></span>.<span class='id identifier rubyid_match'>match</span>(<span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>9f</span><span class='tstring_end'>'</span></span>) <span class='comment'>#=> #<MatchData "9">
</span><span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>[9f]</span><span class='regexp_end'>/</span></span>.<span class='id identifier rubyid_match'>match</span>(<span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>9f</span><span class='tstring_end'>'</span></span>) <span class='comment'>#=> #<MatchData "9"></span></code></pre>
<p>If the first character of a character class is a caret (<code>^</code>) the class is inverted: it matches any character <em>except</em> those named.</p>
<pre class="code ruby"><code class="ruby"><span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>[^a-eg-z]</span><span class='regexp_end'>/</span></span>.<span class='id identifier rubyid_match'>match</span>(<span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>f</span><span class='tstring_end'>'</span></span>) <span class='comment'>#=> #<MatchData "f"></span></code></pre>
<p>A character class may contain another character class. By itself this isn’t useful because <code>[a-z[0-9]]</code> describes the same set as <code>[a-z0-9]</code>. However, character classes also support the <code>&&</code> operator which performs set intersection on its arguments. The two can be combined as follows:</p>
<pre class="code ruby"><code class="ruby"><span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>[a-w&&[^c-g]z]</span><span class='regexp_end'>/</span></span> <span class='comment'># ([a-w] AND ([^c-g] OR z))</span></code></pre>
<p>This is equivalent to:</p>
<pre class="code ruby"><code class="ruby"><span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>[abh-w]</span><span class='regexp_end'>/</span></span></code></pre>
<p>The following metacharacters also behave like character classes:</p>
<ul><li>
<p><code>/./</code> - Any character except a newline.</p>
</li><li>
<p><code>/./m</code> - Any character (the <code>m</code> modifier enables multiline mode)</p>
</li><li>
<p><code>/\w/</code> - A word character (<code>[a-zA-Z0-9_]</code>)</p>
</li><li>
<p><code>/\W/</code> - A non-word character (<code>[^a-zA-Z0-9_]</code>). Please take a look at <a href="https://bugs.ruby-lang.org/issues/4044" target="_parent" title="Bug #4044">Bug #4044</a> if using <code>/\W/</code> with the <code>/i</code> modifier.</p>
</li><li>
<p><code>/\d/</code> - A digit character (<code>[0-9]</code>)</p>
</li><li>
<p><code>/\D/</code> - A non-digit character (<code>[^0-9]</code>)</p>
</li><li>
<p><code>/\h/</code> - A hexdigit character (<code>[0-9a-fA-F]</code>)</p>
</li><li>
<p><code>/\H/</code> - A non-hexdigit character (<code>[^0-9a-fA-F]</code>)</p>
</li><li>
<p><code>/\s/</code> - A whitespace character: <code>/[ \t\r\n\f\v]/</code></p>
</li><li>
<p><code>/\S/</code> - A non-whitespace character: <code>/[^ \t\r\n\f\v]/</code></p>
</li></ul>
<p>POSIX <em>bracket expressions</em> are also similar to character classes. They provide a portable alternative to the above, with the added benefit that they encompass non-ASCII characters. For instance, <code>/\d/</code> matches only the ASCII decimal digits (0-9); whereas <code>/[[:digit:]]/</code> matches any character in the Unicode <em>Nd</em> category.</p>
<ul><li>
<p><code>/[[:alnum:]]/</code> - Alphabetic and numeric character</p>
</li><li>
<p><code>/[[:alpha:]]/</code> - Alphabetic character</p>
</li><li>
<p><code>/[[:blank:]]/</code> - Space or tab</p>
</li><li>
<p><code>/[[:cntrl:]]/</code> - Control character</p>
</li><li>
<p><code>/[[:digit:]]/</code> - Digit</p>
</li><li>
<p><code>/[[:graph:]]/</code> - Non-blank character (excludes spaces, control characters, and similar)</p>
</li><li>
<p><code>/[[:lower:]]/</code> - Lowercase alphabetical character</p>
</li><li>
<p><code>/[[:print:]]/</code> - Like [:graph:], but includes the space character</p>
</li><li>
<p><code>/[[:punct:]]/</code> - Punctuation character</p>
</li><li>
<p><code>/[[:space:]]/</code> - Whitespace character (<code>[:blank:]</code>, newline, carriage return, etc.)</p>
</li><li>
<p><code>/[[:upper:]]/</code> - Uppercase alphabetical</p>
</li><li>
<p><code>/[[:xdigit:]]/</code> - Digit allowed in a hexadecimal number (i.e., 0-9a-fA-F)</p>
</li></ul>
<p>Ruby also supports the following non-POSIX character classes:</p>
<ul><li>
<p><code>/[[:word:]]/</code> - A character in one of the following Unicode general categories <em>Letter</em>, <em>Mark</em>, <em>Number</em>, <em>Connector_Punctuation</em></p>
</li><li>
<p><code>/[[:ascii:]]/</code> - A character in the ASCII character set</p>
<pre class="code ruby"><code class="ruby"><span class='comment'># U+06F2 is "EXTENDED ARABIC-INDIC DIGIT TWO"
</span><span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>[[:digit:]]</span><span class='regexp_end'>/</span></span>.<span class='id identifier rubyid_match'>match</span>(<span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>\u06F2</span><span class='tstring_end'>"</span></span>) <span class='comment'>#=> #<MatchData "\u{06F2}">
</span><span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>[[:upper:]][[:lower:]]</span><span class='regexp_end'>/</span></span>.<span class='id identifier rubyid_match'>match</span>(<span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>Hello</span><span class='tstring_end'>"</span></span>) <span class='comment'>#=> #<MatchData "He">
</span><span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>[[:xdigit:]][[:xdigit:]]</span><span class='regexp_end'>/</span></span>.<span class='id identifier rubyid_match'>match</span>(<span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>A6</span><span class='tstring_end'>"</span></span>) <span class='comment'>#=> #<MatchData "A6"></span></code></pre>
</li></ul>
<h2 id="label-Repetition">Repetition</h2>
<p>The constructs described so far match a single character. They can be followed by a repetition metacharacter to specify how many times they need to occur. Such metacharacters are called <em>quantifiers</em>.</p>
<ul><li>
<p><code>*</code> - Zero or more times</p>
</li><li>
<p><code>+</code> - One or more times</p>
</li><li>
<p><code>?</code> - Zero or one times (optional)</p>
</li><li>
<p><code>{</code><em>n</em><code>}</code> - Exactly <em>n</em> times</p>
</li><li>
<p><code>{</code><em>n</em><code>,}</code> - <em>n</em> or more times</p>
</li><li>
<p><code>{,</code><em>m</em><code>}</code> - <em>m</em> or less times</p>
</li><li>
<p><code>{</code><em>n</em><code>,</code><em>m</em><code>}</code> - At least <em>n</em> and at most <em>m</em> times</p>
</li></ul>
<p>At least one uppercase character (‘H’), at least one lowercase character (‘e’), two ‘l’ characters, then one ‘o’:</p>
<pre class="code ruby"><code class="ruby"><span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>Hello</span><span class='tstring_end'>"</span></span>.<span class='id identifier rubyid_match'>match</span>(<span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>[[:upper:]][[:lower:]]l{2}o</span><span class='regexp_end'>/</span></span>) <span class='comment'>#=> #<MatchData "Hello"></span></code></pre>
<p>Repetition is <em>greedy</em> by default: as many occurrences as possible are matched while still allowing the overall match to succeed. By contrast, <em>lazy</em> matching makes the minimal amount of matches necessary for overall success. A greedy metacharacter can be made lazy by following it with <code>?</code>.</p>
<p>Both patterns below match the string. The first uses a greedy quantifier so ‘.+’ matches ‘<a><b>’; the second uses a lazy quantifier so ‘.+?’ matches ‘<a>’:</p>
<pre class="code ruby"><code class="ruby"><span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'><.+></span><span class='regexp_end'>/</span></span>.<span class='id identifier rubyid_match'>match</span>(<span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'><a><b></span><span class='tstring_end'>"</span></span>) <span class='comment'>#=> #<MatchData "<a><b>">
</span><span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'><.+?></span><span class='regexp_end'>/</span></span>.<span class='id identifier rubyid_match'>match</span>(<span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'><a><b></span><span class='tstring_end'>"</span></span>) <span class='comment'>#=> #<MatchData "<a>"></span></code></pre>
<p>A quantifier followed by <code>+</code> matches <em>possessively</em>: once it has matched it does not backtrack. They behave like greedy quantifiers, but having matched they refuse to “give up” their match even if this jeopardises the overall match.</p>
<h2 id="label-Capturing">Capturing</h2>
<p>Parentheses can be used for <em>capturing</em>. The text enclosed by the <em>n</em><sup>th</sup> group of parentheses can be subsequently referred to with <em>n</em>. Within a pattern use the <em>backreference</em> <code>\n</code>; outside of the pattern use <code>MatchData[</code><em>n</em><code>]</code>.</p>
<p>‘at’ is captured by the first group of parentheses, then referred to later with <code>\1</code>:</p>
<pre class="code ruby"><code class="ruby"><span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>[csh](..) [csh]\1 in</span><span class='regexp_end'>/</span></span>.<span class='id identifier rubyid_match'>match</span>(<span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>The cat sat in the hat</span><span class='tstring_end'>"</span></span>)
<span class='comment'>#=> #<MatchData "cat sat in" 1:"at"></span></code></pre>
<p>Regexp#match returns a MatchData object which makes the captured text available with its #[] method:</p>
<pre class="code ruby"><code class="ruby"><span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>[csh](..) [csh]\1 in</span><span class='regexp_end'>/</span></span>.<span class='id identifier rubyid_match'>match</span>(<span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>The cat sat in the hat</span><span class='tstring_end'>"</span></span>)[<span class='int'>1</span>] <span class='comment'>#=> 'at'</span></code></pre>
<p>Capture groups can be referred to by name when defined with the <code>(?<</code><em>name</em><code>>)</code> or <code>(?'</code><em>name</em><code>')</code> constructs.</p>
<pre class="code ruby"><code class="ruby"><span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>\$(?<dollars>\d)\.(?<cents>\d)</span><span class='regexp_end'>/</span></span>.<span class='id identifier rubyid_match'>match</span>(<span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>$3.67</span><span class='tstring_end'>"</span></span>)
<span class='comment'>#=> #<MatchData "$3.67" dollars:"3" cents:"67">
</span><span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>\$(?<dollars>\d)\.(?<cents>\d)</span><span class='regexp_end'>/</span></span>.<span class='id identifier rubyid_match'>match</span>(<span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>$3.67</span><span class='tstring_end'>"</span></span>)[<span class='symbeg'>:</span><span class='id identifier rubyid_dollars'>dollars</span>] <span class='comment'>#=> "3"</span></code></pre>
<p>Named groups can be backreferenced with <code>\k<</code><em>name</em><code>></code>, where <em>name</em> is the group name.</p>
<pre class="code ruby"><code class="ruby"><span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>(?<vowel>[aeiou]).\k<vowel>.\k<vowel></span><span class='regexp_end'>/</span></span>.<span class='id identifier rubyid_match'>match</span>(<span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>ototomy</span><span class='tstring_end'>'</span></span>)
<span class='comment'>#=> #<MatchData "ototo" vowel:"o"></span></code></pre>
<p><strong>Note</strong>: A regexp can’t use named backreferences and numbered backreferences simultaneously.</p>
<p>When named capture groups are used with a literal regexp on the left-hand side of an expression and the <code>=~</code> operator, the captured text is also assigned to local variables with corresponding names.</p>
<pre class="code ruby"><code class="ruby"><span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>\$(?<dollars>\d)\.(?<cents>\d)</span><span class='regexp_end'>/</span></span> <span class='op'>=~</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>$3.67</span><span class='tstring_end'>"</span></span> <span class='comment'>#=> 0
</span><span class='id identifier rubyid_dollars'>dollars</span> <span class='comment'>#=> "3"</span></code></pre>
<h2 id="label-Grouping">Grouping</h2>
<p>Parentheses also <em>group</em> the terms they enclose, allowing them to be quantified as one <em>atomic</em> whole.</p>
<p>The pattern below matches a vowel followed by 2 word characters:</p>
<pre class="code ruby"><code class="ruby"><span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>[aeiou]\w{2}</span><span class='regexp_end'>/</span></span>.<span class='id identifier rubyid_match'>match</span>(<span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>Caenorhabditis elegans</span><span class='tstring_end'>"</span></span>) <span class='comment'>#=> #<MatchData "aen"></span></code></pre>
<p>Whereas the following pattern matches a vowel followed by a word character, twice, i.e. <code>[aeiou]\w[aeiou]\w</code>: ‘enor’.</p>
<pre class="code ruby"><code class="ruby"><span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>([aeiou]\w){2}</span><span class='regexp_end'>/</span></span>.<span class='id identifier rubyid_match'>match</span>(<span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>Caenorhabditis elegans</span><span class='tstring_end'>"</span></span>)
<span class='comment'>#=> #<MatchData "enor" 1:"or"></span></code></pre>
<p>The <code>(?:</code>…<code>)</code> construct provides grouping without capturing. That is, it combines the terms it contains into an atomic whole without creating a backreference. This benefits performance at the slight expense of readability.</p>
<p>The first group of parentheses captures ‘n’ and the second ‘ti’. The second group is referred to later with the backreference <code>\2</code>:</p>
<pre class="code ruby"><code class="ruby"><span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>I(n)ves(ti)ga\2ons</span><span class='regexp_end'>/</span></span>.<span class='id identifier rubyid_match'>match</span>(<span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>Investigations</span><span class='tstring_end'>"</span></span>)
<span class='comment'>#=> #<MatchData "Investigations" 1:"n" 2:"ti"></span></code></pre>
<p>The first group of parentheses is now made non-capturing with ‘?:’, so it still matches ‘n’, but doesn’t create the backreference. Thus, the backreference <code>\1</code> now refers to ‘ti’.</p>
<pre class="code ruby"><code class="ruby"><span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>I(?:n)ves(ti)ga\1ons</span><span class='regexp_end'>/</span></span>.<span class='id identifier rubyid_match'>match</span>(<span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>Investigations</span><span class='tstring_end'>"</span></span>)
<span class='comment'>#=> #<MatchData "Investigations" 1:"ti"></span></code></pre>
<h3 id="label-Atomic+Grouping">Atomic Grouping</h3>
<p>Grouping can be made <em>atomic</em> with <code>(?></code><em>pat</em><code>)</code>. This causes the subexpression <em>pat</em> to be matched independently of the rest of the expression such that what it matches becomes fixed for the remainder of the match, unless the entire subexpression must be abandoned and subsequently revisited. In this way <em>pat</em> is treated as a non-divisible whole. Atomic grouping is typically used to optimise patterns so as to prevent the regular expression engine from backtracking needlessly.</p>
<p>The <code>"</code> in the pattern below matches the first character of the string, then <code>.*</code> matches <em>Quote“</em>. This causes the overall match to fail, so the text matched by <code>.*</code> is backtracked by one position, which leaves the final character of the string available to match <code>"</code></p>
<pre class="code ruby"><code class="ruby"><span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>".*"</span><span class='regexp_end'>/</span></span>.<span class='id identifier rubyid_match'>match</span>(<span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>"Quote"</span><span class='tstring_end'>'</span></span>) <span class='comment'>#=> #<MatchData "\"Quote\""></span></code></pre>
<p>If <code>.*</code> is grouped atomically, it refuses to backtrack <em>Quote“</em>, even though this means that the overall match fails</p>
<pre class="code ruby"><code class="ruby"><span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>"(?>.*)"</span><span class='regexp_end'>/</span></span>.<span class='id identifier rubyid_match'>match</span>(<span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>"Quote"</span><span class='tstring_end'>'</span></span>) <span class='comment'>#=> nil</span></code></pre>
<h2 id="label-Subexpression+Calls">Subexpression Calls</h2>
<p>The <code>\g<</code><em>name</em><code>></code> syntax matches the previous subexpression named <em>name</em>, which can be a group name or number, again. This differs from backreferences in that it re-executes the group rather than simply trying to re-match the same text.</p>
<p>This pattern matches a <em>(</em> character and assigns it to the <code>paren</code> group, tries to call that the <code>paren</code> sub-expression again but fails, then matches a literal <em>)</em>:</p>
<pre class="code ruby"><code class="ruby"><span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>\A(?<paren>\(\g<paren>*\))*\z</span><span class='regexp_end'>/</span></span> <span class='op'>=~</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>()</span><span class='tstring_end'>'</span></span>
<span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>\A(?<paren>\(\g<paren>*\))*\z</span><span class='regexp_end'>/</span></span> <span class='op'>=~</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>(())</span><span class='tstring_end'>'</span></span> <span class='comment'>#=> 0
</span><span class='comment'># ^1
</span><span class='comment'># ^2
</span><span class='comment'># ^3
</span><span class='comment'># ^4
</span><span class='comment'># ^5
</span><span class='comment'># ^6
</span><span class='comment'># ^7
</span><span class='comment'># ^8
</span><span class='comment'># ^9
</span><span class='comment'># ^10</span></code></pre>
<ol><li>
<p>Matches at the beginning of the string, i.e. before the first character.</p>
</li><li>
<p>Enters a named capture group called <code>paren</code></p>
</li><li>
<p>Matches a literal <em>(</em>, the first character in the string</p>
</li><li>
<p>Calls the <code>paren</code> group again, i.e. recurses back to the second step</p>
</li><li>
<p>Re-enters the <code>paren</code> group</p>
</li><li>
<p>Matches a literal <em>(</em>, the second character in the string</p>
</li><li>
<p>Try to call <code>paren</code> a third time, but fail because doing so would prevent an overall successful match</p>
</li><li>
<p>Match a literal <em>)</em>, the third character in the string. Marks the end of the second recursive call</p>
</li><li>
<p>Match a literal <em>)</em>, the fourth character in the string</p>
</li><li>
<p>Match the end of the string</p>
</li></ol>
<h2 id="label-Alternation">Alternation</h2>
<p>The vertical bar metacharacter (<code>|</code>) combines two expressions into a single one that matches either of the expressions. Each expression is an <em>alternative</em>.</p>
<pre class="code ruby"><code class="ruby"><span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>\w(and|or)\w</span><span class='regexp_end'>/</span></span>.<span class='id identifier rubyid_match'>match</span>(<span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>Feliformia</span><span class='tstring_end'>"</span></span>) <span class='comment'>#=> #<MatchData "form" 1:"or">
</span><span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>\w(and|or)\w</span><span class='regexp_end'>/</span></span>.<span class='id identifier rubyid_match'>match</span>(<span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>furandi</span><span class='tstring_end'>"</span></span>) <span class='comment'>#=> #<MatchData "randi" 1:"and">
</span><span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>\w(and|or)\w</span><span class='regexp_end'>/</span></span>.<span class='id identifier rubyid_match'>match</span>(<span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>dissemblance</span><span class='tstring_end'>"</span></span>) <span class='comment'>#=> nil</span></code></pre>
<h2 id="label-Character+Properties">Character Properties</h2>
<p>The <code>\p{}</code> construct matches characters with the named property, much like POSIX bracket classes.</p>
<ul><li>
<p><code>/\p{Alnum}/</code> - Alphabetic and numeric character</p>
</li><li>
<p><code>/\p{Alpha}/</code> - Alphabetic character</p>
</li><li>
<p><code>/\p{Blank}/</code> - Space or tab</p>
</li><li>
<p><code>/\p{Cntrl}/</code> - Control character</p>
</li><li>
<p><code>/\p{Digit}/</code> - Digit</p>
</li><li>
<p><code>/\p{Graph}/</code> - Non-blank character (excludes spaces, control characters, and similar)</p>
</li><li>
<p><code>/\p{Lower}/</code> - Lowercase alphabetical character</p>
</li><li>
<p><code>/\p{Print}/</code> - Like <code>\p{Graph}</code>, but includes the space character</p>
</li><li>
<p><code>/\p{Punct}/</code> - Punctuation character</p>
</li><li>
<p><code>/\p{Space}/</code> - Whitespace character (<code>[:blank:]</code>, newline, carriage return, etc.)</p>
</li><li>
<p><code>/\p{Upper}/</code> - Uppercase alphabetical</p>
</li><li>
<p><code>/\p{XDigit}/</code> - Digit allowed in a hexadecimal number (i.e., 0-9a-fA-F)</p>
</li><li>
<p><code>/\p{Word}/</code> - A member of one of the following Unicode general category <em>Letter</em>, <em>Mark</em>, <em>Number</em>, <em>Connector_Punctuation</em></p>
</li><li>
<p><code>/\p{ASCII}/</code> - A character in the ASCII character set</p>
</li><li>
<p><code>/\p{Any}/</code> - Any Unicode character (including unassigned characters)</p>
</li><li>
<p><code>/\p{Assigned}/</code> - An assigned character</p>
</li></ul>
<p>A Unicode character’s <em>General Category</em> value can also be matched with <code>\p{</code><em>Ab</em><code>}</code> where <em>Ab</em> is the category’s abbreviation as described below:</p>
<ul><li>
<p><code>/\p{L}/</code> - ‘Letter’</p>
</li><li>
<p><code>/\p{Ll}/</code> - ‘Letter: Lowercase’</p>
</li><li>
<p><code>/\p{Lm}/</code> - ‘Letter: Mark’</p>
</li><li>
<p><code>/\p{Lo}/</code> - ‘Letter: Other’</p>
</li><li>
<p><code>/\p{Lt}/</code> - ‘Letter: Titlecase’</p>
</li><li>
<p><code>/\p{Lu}/</code> - ‘Letter: Uppercase</p>
</li><li>
<p><code>/\p{Lo}/</code> - ‘Letter: Other’</p>
</li><li>
<p><code>/\p{M}/</code> - ‘Mark’</p>
</li><li>
<p><code>/\p{Mn}/</code> - ‘Mark: Nonspacing’</p>
</li><li>
<p><code>/\p{Mc}/</code> - ‘Mark: Spacing Combining’</p>
</li><li>
<p><code>/\p{Me}/</code> - ‘Mark: Enclosing’</p>
</li><li>
<p><code>/\p{N}/</code> - ‘Number’</p>
</li><li>
<p><code>/\p{Nd}/</code> - ‘Number: Decimal Digit’</p>
</li><li>
<p><code>/\p{Nl}/</code> - ‘Number: Letter’</p>
</li><li>
<p><code>/\p{No}/</code> - ‘Number: Other’</p>
</li><li>
<p><code>/\p{P}/</code> - ‘Punctuation’</p>
</li><li>
<p><code>/\p{Pc}/</code> - ‘Punctuation: Connector’</p>
</li><li>
<p><code>/\p{Pd}/</code> - ‘Punctuation: Dash’</p>
</li><li>
<p><code>/\p{Ps}/</code> - ‘Punctuation: Open’</p>
</li><li>
<p><code>/\p{Pe}/</code> - ‘Punctuation: Close’</p>
</li><li>
<p><code>/\p{Pi}/</code> - ‘Punctuation: Initial Quote’</p>
</li><li>
<p><code>/\p{Pf}/</code> - ‘Punctuation: Final Quote’</p>
</li><li>
<p><code>/\p{Po}/</code> - ‘Punctuation: Other’</p>
</li><li>
<p><code>/\p{S}/</code> - ‘Symbol’</p>
</li><li>
<p><code>/\p{Sm}/</code> - ‘Symbol: Math’</p>
</li><li>
<p><code>/\p{Sc}/</code> - ‘Symbol: Currency’</p>
</li><li>
<p><code>/\p{Sc}/</code> - ‘Symbol: Currency’</p>
</li><li>
<p><code>/\p{Sk}/</code> - ‘Symbol: Modifier’</p>
</li><li>
<p><code>/\p{So}/</code> - ‘Symbol: Other’</p>
</li><li>
<p><code>/\p{Z}/</code> - ‘Separator’</p>
</li><li>
<p><code>/\p{Zs}/</code> - ‘Separator: Space’</p>
</li><li>
<p><code>/\p{Zl}/</code> - ‘Separator: Line’</p>
</li><li>
<p><code>/\p{Zp}/</code> - ‘Separator: Paragraph’</p>
</li><li>
<p><code>/\p{C}/</code> - ‘Other’</p>
</li><li>
<p><code>/\p{Cc}/</code> - ‘Other: Control’</p>
</li><li>
<p><code>/\p{Cf}/</code> - ‘Other: Format’</p>
</li><li>
<p><code>/\p{Cn}/</code> - ‘Other: Not Assigned’</p>
</li><li>
<p><code>/\p{Co}/</code> - ‘Other: Private Use’</p>
</li><li>
<p><code>/\p{Cs}/</code> - ‘Other: Surrogate’</p>
</li></ul>
<p>Lastly, <code>\p{}</code> matches a character’s Unicode <em>script</em>. The following scripts are supported: <em>Arabic</em>, <em>Armenian</em>, <em>Balinese</em>, <em>Bengali</em>, <em>Bopomofo</em>, <em>Braille</em>, <em>Buginese</em>, <em>Buhid</em>, <em>Canadian_Aboriginal</em>, <em>Carian</em>, <em>Cham</em>, <em>Cherokee</em>, <em>Common</em>, <em>Coptic</em>, <em>Cuneiform</em>, <em>Cypriot</em>, <em>Cyrillic</em>, <em>Deseret</em>, <em>Devanagari</em>, <em>Ethiopic</em>, <em>Georgian</em>, <em>Glagolitic</em>, <em>Gothic</em>, <em>Greek</em>, <em>Gujarati</em>, <em>Gurmukhi</em>, <em>Han</em>, <em>Hangul</em>, <em>Hanunoo</em>, <em>Hebrew</em>, <em>Hiragana</em>, <em>Inherited</em>, <em>Kannada</em>, <em>Katakana</em>, <em>Kayah_Li</em>, <em>Kharoshthi</em>, <em>Khmer</em>, <em>Lao</em>, <em>Latin</em>, <em>Lepcha</em>, <em>Limbu</em>, <em>Linear_B</em>, <em>Lycian</em>, <em>Lydian</em>, <em>Malayalam</em>, <em>Mongolian</em>, <em>Myanmar</em>, <em>New_Tai_Lue</em>, <em>Nko</em>, <em>Ogham</em>, <em>Ol_Chiki</em>, <em>Old_Italic</em>, <em>Old_Persian</em>, <em>Oriya</em>, <em>Osmanya</em>, <em>Phags_Pa</em>, <em>Phoenician</em>, <em>Rejang</em>, <em>Runic</em>, <em>Saurashtra</em>, <em>Shavian</em>, <em>Sinhala</em>, <em>Sundanese</em>, <em>Syloti_Nagri</em>, <em>Syriac</em>, <em>Tagalog</em>, <em>Tagbanwa</em>, <em>Tai_Le</em>, <em>Tamil</em>, <em>Telugu</em>, <em>Thaana</em>, <em>Thai</em>, <em>Tibetan</em>, <em>Tifinagh</em>, <em>Ugaritic</em>, <em>Vai</em>, and <em>Yi</em>.</p>
<p>Unicode codepoint U+06E9 is named “ARABIC PLACE OF SAJDAH” and belongs to the Arabic script:</p>
<pre class="code ruby"><code class="ruby"><span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>\p{Arabic}</span><span class='regexp_end'>/</span></span>.<span class='id identifier rubyid_match'>match</span>(<span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>\u06E9</span><span class='tstring_end'>"</span></span>) <span class='comment'>#=> #<MatchData "\u06E9"></span></code></pre>
<p>All character properties can be inverted by prefixing their name with a caret (<code>^</code>).</p>
<p>Letter ‘A’ is not in the Unicode Ll (Letter; Lowercase) category, so this match succeeds:</p>
<pre class="code ruby"><code class="ruby"><span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>\p{^Ll}</span><span class='regexp_end'>/</span></span>.<span class='id identifier rubyid_match'>match</span>(<span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>A</span><span class='tstring_end'>"</span></span>) <span class='comment'>#=> #<MatchData "A"></span></code></pre>
<h2 id="label-Anchors">Anchors</h2>
<p>Anchors are metacharacter that match the zero-width positions between characters, <em>anchoring</em> the match to a specific position.</p>
<ul><li>
<p><code>^</code> - Matches beginning of line</p>
</li><li>
<p><code>$</code> - Matches end of line</p>
</li><li>
<p><code>\A</code> - Matches beginning of string.</p>
</li><li>
<p><code>\Z</code> - Matches end of string. If string ends with a newline, it matches just before newline</p>
</li><li>
<p><code>\z</code> - Matches end of string</p>
</li><li>
<p><code>\G</code> - Matches first matching position:</p>
<p>In methods like <code>String#gsub</code> and <code>String#scan</code>, it changes on each iteration. It initially matches the beginning of subject, and in each following iteration it matches where the last match finished.</p>
<pre class="code ruby"><code class="ruby"><span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'> a b c</span><span class='tstring_end'>"</span></span>.<span class='id identifier rubyid_gsub'>gsub</span>(<span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'> </span><span class='regexp_end'>/</span></span><span class='comma'>,</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>_</span><span class='tstring_end'>'</span></span>) <span class='comment'>#=> "____a_b_c"
</span><span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'> a b c</span><span class='tstring_end'>"</span></span>.<span class='id identifier rubyid_gsub'>gsub</span>(<span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>\G </span><span class='regexp_end'>/</span></span><span class='comma'>,</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>_</span><span class='tstring_end'>'</span></span>) <span class='comment'>#=> "____a b c"</span></code></pre>
<p>In methods like <code>Regexp#match</code> and <code>String#match</code> that take an (optional) offset, it matches where the search begins.</p>
<pre class="code ruby"><code class="ruby"><span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>hello, world</span><span class='tstring_end'>"</span></span>.<span class='id identifier rubyid_match'>match</span>(<span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>,</span><span class='regexp_end'>/</span></span><span class='comma'>,</span> <span class='int'>3</span>) <span class='comment'>#=> #<MatchData ",">
</span><span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>hello, world</span><span class='tstring_end'>"</span></span>.<span class='id identifier rubyid_match'>match</span>(<span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>\G,</span><span class='regexp_end'>/</span></span><span class='comma'>,</span> <span class='int'>3</span>) <span class='comment'>#=> nil</span></code></pre>
</li><li>
<p><code>\b</code> - Matches word boundaries when outside brackets; backspace (0x08) when inside brackets</p>
</li><li>
<p><code>\B</code> - Matches non-word boundaries</p>
</li><li>
<p><code>(?=</code><em>pat</em><code>)</code> - <em>Positive lookahead</em> assertion: ensures that the following characters match <em>pat</em>, but doesn’t include those characters in the matched text</p>
</li><li>
<p><code>(?!</code><em>pat</em><code>)</code> - <em>Negative lookahead</em> assertion: ensures that the following characters do not match <em>pat</em>, but doesn’t include those characters in the matched text</p>
</li><li>
<p><code>(?<=</code><em>pat</em><code>)</code> - <em>Positive lookbehind</em> assertion: ensures that the preceding characters match <em>pat</em>, but doesn’t include those characters in the matched text</p>
</li><li>
<p><code>(?<!</code><em>pat</em><code>)</code> - <em>Negative lookbehind</em> assertion: ensures that the preceding characters do not match <em>pat</em>, but doesn’t include those characters in the matched text</p>
</li></ul>
<p>If a pattern isn’t anchored it can begin at any point in the string:</p>
<pre class="code ruby"><code class="ruby"><span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>real</span><span class='regexp_end'>/</span></span>.<span class='id identifier rubyid_match'>match</span>(<span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>surrealist</span><span class='tstring_end'>"</span></span>) <span class='comment'>#=> #<MatchData "real"></span></code></pre>
<p>Anchoring the pattern to the beginning of the string forces the match to start there. ‘real’ doesn’t occur at the beginning of the string, so now the match fails:</p>
<pre class="code ruby"><code class="ruby"><span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>\Areal</span><span class='regexp_end'>/</span></span>.<span class='id identifier rubyid_match'>match</span>(<span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>surrealist</span><span class='tstring_end'>"</span></span>) <span class='comment'>#=> nil</span></code></pre>
<p>The match below fails because although ‘Demand’ contains ‘and’, the pattern does not occur at a word boundary.</p>
<pre class="code ruby"><code class="ruby"><span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>\band</span><span class='regexp_end'>/</span></span>.<span class='id identifier rubyid_match'>match</span>(<span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>Demand</span><span class='tstring_end'>"</span></span>)</code></pre>
<p>Whereas in the following example ‘and’ has been anchored to a non-word boundary so instead of matching the first ‘and’ it matches from the fourth letter of ‘demand’ instead:</p>
<pre class="code ruby"><code class="ruby"><span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>\Band.+</span><span class='regexp_end'>/</span></span>.<span class='id identifier rubyid_match'>match</span>(<span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>Supply and demand curve</span><span class='tstring_end'>"</span></span>) <span class='comment'>#=> #<MatchData "and curve"></span></code></pre>
<p>The pattern below uses positive lookahead and positive lookbehind to match text appearing in tags without including the tags in the match:</p>
<pre class="code ruby"><code class="ruby"><span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>(?<=<b>)\w+(?=<\/b>)</span><span class='regexp_end'>/</span></span>.<span class='id identifier rubyid_match'>match</span>(<span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>Fortune favours the <b>bold</b></span><span class='tstring_end'>"</span></span>)
<span class='comment'>#=> #<MatchData "bold"></span></code></pre>
<h2 id="label-Options">Options</h2>
<p>The end delimiter for a regexp can be followed by one or more single-letter options which control how the pattern can match.</p>
<ul><li>
<p><code>/pat/i</code> - Ignore case</p>
</li><li>
<p><code>/pat/m</code> - Treat a newline as a character matched by <code>.</code></p>
</li><li>
<p><code>/pat/x</code> - Ignore whitespace and comments in the pattern</p>
</li><li>
<p><code>/pat/o</code> - Perform <code>#{}</code> interpolation only once</p>
</li></ul>
<p><code>i</code>, <code>m</code>, and <code>x</code> can also be applied on the subexpression level with the <code>(?</code><em>on</em><code>-</code><em>off</em><code>)</code> construct, which enables options <em>on</em>, and disables options <em>off</em> for the expression enclosed by the parentheses:</p>
<pre class="code ruby"><code class="ruby"><span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>a(?i:b)c</span><span class='regexp_end'>/</span></span>.<span class='id identifier rubyid_match'>match</span>(<span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>aBc</span><span class='tstring_end'>'</span></span>) <span class='comment'>#=> #<MatchData "aBc">
</span><span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>a(?-i:b)c</span><span class='regexp_end'>/i</span></span>.<span class='id identifier rubyid_match'>match</span>(<span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>ABC</span><span class='tstring_end'>'</span></span>) <span class='comment'>#=> nil</span></code></pre>
<p>Additionally, these options can also be toggled for the remainder of the pattern:</p>
<pre class="code ruby"><code class="ruby"><span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>a(?i)bc</span><span class='regexp_end'>/</span></span>.<span class='id identifier rubyid_match'>match</span>(<span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>abC</span><span class='tstring_end'>'</span></span>) <span class='comment'>#=> #<MatchData "abC"></span></code></pre>
<p>Options may also be used with <code>Regexp.new</code>:</p>
<pre class="code ruby"><code class="ruby"><span class='const'>Regexp</span>.<span class='id identifier rubyid_new'>new</span>(<span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>abc</span><span class='tstring_end'>"</span></span><span class='comma'>,</span> <span class='const'>Regexp</span><span class='op'>::</span><span class='const'>IGNORECASE</span>) <span class='comment'>#=> /abc/i
</span><span class='const'>Regexp</span>.<span class='id identifier rubyid_new'>new</span>(<span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>abc</span><span class='tstring_end'>"</span></span><span class='comma'>,</span> <span class='const'>Regexp</span><span class='op'>::</span><span class='const'>MULTILINE</span>) <span class='comment'>#=> /abc/m
</span><span class='const'>Regexp</span>.<span class='id identifier rubyid_new'>new</span>(<span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>abc # Comment</span><span class='tstring_end'>"</span></span><span class='comma'>,</span> <span class='const'>Regexp</span><span class='op'>::</span><span class='const'>EXTENDED</span>) <span class='comment'>#=> /abc # Comment/x
</span><span class='const'>Regexp</span>.<span class='id identifier rubyid_new'>new</span>(<span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>abc</span><span class='tstring_end'>"</span></span><span class='comma'>,</span> <span class='const'>Regexp</span><span class='op'>::</span><span class='const'>IGNORECASE</span> <span class='op'>|</span> <span class='const'>Regexp</span><span class='op'>::</span><span class='const'>MULTILINE</span>) <span class='comment'>#=> /abc/mi</span></code></pre>
<h2 id="label-Free-Spacing+Mode+and+Comments">Free-Spacing Mode and Comments</h2>
<p>As mentioned above, the <code>x</code> option enables <em>free-spacing</em> mode. Literal white space inside the pattern is ignored, and the octothorpe (<code>#</code>) character introduces a comment until the end of the line. This allows the components of the pattern to be organized in a potentially more readable fashion.</p>
<p>A contrived pattern to match a number with optional decimal places:</p>
<pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_float_pat'>float_pat</span> <span class='op'>=</span> <span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>\A
[[:digit:]]+ # 1 or more digits before the decimal point
(\. # Decimal point
[[:digit:]]+ # 1 or more digits after the decimal point
)? # The decimal point and following digits are optional
\Z</span><span class='regexp_end'>/x</span></span>
<span class='id identifier rubyid_float_pat'>float_pat</span>.<span class='id identifier rubyid_match'>match</span>(<span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>3.14</span><span class='tstring_end'>'</span></span>) <span class='comment'>#=> #<MatchData "3.14" 1:".14"></span></code></pre>
<p>There are a number of strategies for matching whitespace:</p>
<ul><li>
<p>Use a pattern such as <code>\s</code> or <code>\p{Space}</code>.</p>
</li><li>
<p>Use escaped whitespace such as <code>\ </code>, i.e. a space preceded by a backslash.</p>
</li><li>
<p>Use a character class such as <code>[ ]</code>.</p>
</li></ul>
<p>Comments can be included in a non-<code>x</code> pattern with the <code>(?#</code><em>comment</em><code>)</code> construct, where <em>comment</em> is arbitrary text ignored by the regexp engine.</p>
<p>Comments in regexp literals cannot include unescaped terminator characters.</p>
<h2 id="label-Encoding">Encoding</h2>
<p>Regular expressions are assumed to use the source encoding. This can be overridden with one of the following modifiers.</p>
<ul><li>
<p><code>/</code><em>pat</em><code>/u</code> - UTF-8</p>
</li><li>
<p><code>/</code><em>pat</em><code>/e</code> - EUC-JP</p>
</li><li>
<p><code>/</code><em>pat</em><code>/s</code> - Windows-31J</p>
</li><li>
<p><code>/</code><em>pat</em><code>/n</code> - ASCII-8BIT</p>
</li></ul>
<p>A regexp can be matched against a string when they either share an encoding, or the regexp’s encoding is <em>US-ASCII</em> and the string’s encoding is ASCII-compatible.</p>
<p>If a match between incompatible encodings is attempted an <code>Encoding::CompatibilityError</code> exception is raised.</p>
<p>The <code>Regexp#fixed_encoding?</code> predicate indicates whether the regexp has a <em>fixed</em> encoding, that is one incompatible with ASCII. A regexp’s encoding can be explicitly fixed by supplying <code>Regexp::FIXEDENCODING</code> as the second argument of <code>Regexp.new</code>:</p>
<pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_r'>r</span> <span class='op'>=</span> <span class='const'>Regexp</span>.<span class='id identifier rubyid_new'>new</span>(<span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>a</span><span class='tstring_end'>"</span></span>.<span class='id identifier rubyid_force_encoding'>force_encoding</span>(<span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>iso-8859-1</span><span class='tstring_end'>"</span></span>)<span class='comma'>,</span><span class='const'>Regexp</span><span class='op'>::</span><span class='const'>FIXEDENCODING</span>)
<span class='id identifier rubyid_r'>r</span> <span class='op'>=~</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>a\u3042</span><span class='tstring_end'>"</span></span>
<span class='comment'># raises Encoding::CompatibilityError: incompatible encoding regexp match
</span> <span class='comment'># (ISO-8859-1 regexp with UTF-8 string)</span></code></pre>
<h2 id="label-Special+global+variables">Special global variables</h2>
<p>Pattern matching sets some global variables :</p>
<ul><li>
<p><code>$~</code> is equivalent to Regexp.last_match;</p>
</li><li>
<p><code>$&</code> contains the complete matched text;</p>
</li><li>
<p><code>$`</code> contains string before match;</p>
</li><li>
<p><code>$'</code> contains string after match;</p>
</li><li>
<p><code>$1</code>, <code>$2</code> and so on contain text matching first, second, etc capture group;</p>
</li><li>
<p><code>$+</code> contains last capture group.</p>
</li></ul>
<p>Example:</p>
<pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_m'>m</span> <span class='op'>=</span> <span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>s(\w{2}).*(c)</span><span class='regexp_end'>/</span></span>.<span class='id identifier rubyid_match'>match</span>(<span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>haystack</span><span class='tstring_end'>'</span></span>) <span class='comment'>#=> #<MatchData "stac" 1:"ta" 2:"c">
</span><span class='gvar'>$~</span> <span class='comment'>#=> #<MatchData "stac" 1:"ta" 2:"c">
</span><span class='const'>Regexp</span>.<span class='id identifier rubyid_last_match'>last_match</span> <span class='comment'>#=> #<MatchData "stac" 1:"ta" 2:"c">
</span>
<span class='backref'>$&</span> <span class='comment'>#=> "stac"
</span> <span class='comment'># same as m[0]
</span><span class='backref'>$`</span> <span class='comment'>#=> "hay"
</span> <span class='comment'># same as m.pre_match
</span><span class='backref'>$'</span> <span class='comment'>#=> "k"
</span> <span class='comment'># same as m.post_match
</span><span class='backref'>$1</span> <span class='comment'>#=> "ta"
</span> <span class='comment'># same as m[1]
</span><span class='backref'>$2</span> <span class='comment'>#=> "c"
</span> <span class='comment'># same as m[2]
</span><span class='backref'>$3</span> <span class='comment'>#=> nil
</span> <span class='comment'># no third group in pattern
</span><span class='backref'>$+</span> <span class='comment'>#=> "c"
</span> <span class='comment'># same as m[-1]</span></code></pre>
<p>These global variables are thread-local and method-local variables.</p>
<h2 id="label-Performance">Performance</h2>
<p>Certain pathological combinations of constructs can lead to abysmally bad performance.</p>
<p>Consider a string of 25 <em>a</em>s, a <em>d</em>, 4 <em>a</em>s, and a <em>c</em>.</p>
<pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_s'>s</span> <span class='op'>=</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>a</span><span class='tstring_end'>'</span></span> <span class='op'>*</span> <span class='int'>25</span> <span class='op'>+</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>d</span><span class='tstring_end'>'</span></span> <span class='op'>+</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>a</span><span class='tstring_end'>'</span></span> <span class='op'>*</span> <span class='int'>4</span> <span class='op'>+</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>c</span><span class='tstring_end'>'</span></span>
<span class='comment'>#=> "aaaaaaaaaaaaaaaaaaaaaaaaadaaaac"</span></code></pre>
<p>The following patterns match instantly as you would expect:</p>
<pre class="code ruby"><code class="ruby"><span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>(b|a)</span><span class='regexp_end'>/</span></span> <span class='op'>=~</span> <span class='id identifier rubyid_s'>s</span> <span class='comment'>#=> 0
</span><span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>(b|a+)</span><span class='regexp_end'>/</span></span> <span class='op'>=~</span> <span class='id identifier rubyid_s'>s</span> <span class='comment'>#=> 0
</span><span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>(b|a+)*</span><span class='regexp_end'>/</span></span> <span class='op'>=~</span> <span class='id identifier rubyid_s'>s</span> <span class='comment'>#=> 0</span></code></pre>
<p>However, the following pattern takes appreciably longer:</p>
<pre class="code ruby"><code class="ruby"><span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>(b|a+)*c</span><span class='regexp_end'>/</span></span> <span class='op'>=~</span> <span class='id identifier rubyid_s'>s</span> <span class='comment'>#=> 26</span></code></pre>
<p>This happens because an atom in the regexp is quantified by both an immediate <code>+</code> and an enclosing <code>*</code> with nothing to differentiate which is in control of any particular character. The nondeterminism that results produces super-linear performance. (Consult <em>Mastering Regular Expressions</em> (3rd ed.), pp 222, by <em>Jeffery Friedl</em>, for an in-depth analysis). This particular case can be fixed by use of atomic grouping, which prevents the unnecessary backtracking:</p>
<pre class="code ruby"><code class="ruby">(<span class='id identifier rubyid_start'>start</span> <span class='op'>=</span> <span class='const'>Time</span>.<span class='id identifier rubyid_now'>now</span>) <span class='op'>&&</span> <span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>(b|a+)*c</span><span class='regexp_end'>/</span></span> <span class='op'>=~</span> <span class='id identifier rubyid_s'>s</span> <span class='op'>&&</span> (<span class='const'>Time</span>.<span class='id identifier rubyid_now'>now</span> <span class='op'>-</span> <span class='id identifier rubyid_start'>start</span>)
<span class='comment'>#=> 24.702736882
</span>(<span class='id identifier rubyid_start'>start</span> <span class='op'>=</span> <span class='const'>Time</span>.<span class='id identifier rubyid_now'>now</span>) <span class='op'>&&</span> <span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>(?>b|a+)*c</span><span class='regexp_end'>/</span></span> <span class='op'>=~</span> <span class='id identifier rubyid_s'>s</span> <span class='op'>&&</span> (<span class='const'>Time</span>.<span class='id identifier rubyid_now'>now</span> <span class='op'>-</span> <span class='id identifier rubyid_start'>start</span>)
<span class='comment'>#=> 0.000166571</span></code></pre>
<p>A similar case is typified by the following example, which takes approximately 60 seconds to execute for me:</p>
<p>Match a string of 29 <em>a</em>s against a pattern of 29 optional <em>a</em>s followed by 29 mandatory <em>a</em>s:</p>
<pre class="code ruby"><code class="ruby"><span class='const'>Regexp</span>.<span class='id identifier rubyid_new'>new</span>(<span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>a?</span><span class='tstring_end'>'</span></span> <span class='op'>*</span> <span class='int'>29</span> <span class='op'>+</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>a</span><span class='tstring_end'>'</span></span> <span class='op'>*</span> <span class='int'>29</span>) <span class='op'>=~</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>a</span><span class='tstring_end'>'</span></span> <span class='op'>*</span> <span class='int'>29</span></code></pre>
<p>The 29 optional <em>a</em>s match the string, but this prevents the 29 mandatory <em>a</em>s that follow from matching. Ruby must then backtrack repeatedly so as to satisfy as many of the optional matches as it can while still matching the mandatory 29. It is plain to us that none of the optional matches can succeed, but this fact unfortunately eludes Ruby.</p>
<p>The best way to improve performance is to significantly reduce the amount of backtracking needed. For this case, instead of individually matching 29 optional <em>a</em>s, a range of optional <em>a</em>s can be matched all at once with <em>a<code>0,29</code></em>:</p>
<pre class="code ruby"><code class="ruby"><span class='const'>Regexp</span>.<span class='id identifier rubyid_new'>new</span>(<span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>a{0,29}</span><span class='tstring_end'>'</span></span> <span class='op'>+</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>a</span><span class='tstring_end'>'</span></span> <span class='op'>*</span> <span class='int'>29</span>) <span class='op'>=~</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>a</span><span class='tstring_end'>'</span></span> <span class='op'>*</span> <span class='int'>29</span></code></pre>
<div id='footer'></div>
</div> <!-- content -->
</div> <!-- y_main -->
</body>
</html>