| 1 |
<!DOCTYPE html> |
| 2 |
|
| 3 |
<html> |
| 4 |
|
| 5 |
<head> |
| 6 |
|
| 7 |
<meta charset="utf-8" /> |
| 8 |
<meta name="generator" content="pandoc" /> |
| 9 |
<meta http-equiv="X-UA-Compatible" content="IE=EDGE" /> |
| 10 |
|
| 11 |
<meta name="viewport" content="width=device-width, initial-scale=1" /> |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
<title>Regular expressions</title> |
| 16 |
|
| 17 |
<script>// Pandoc 2.9 adds attributes on both header and div. We remove the former (to |
| 18 |
// be compatible with the behavior of Pandoc < 2.8). |
| 19 |
document.addEventListener('DOMContentLoaded', function(e) { |
| 20 |
var hs = document.querySelectorAll("div.section[class*='level'] > :first-child"); |
| 21 |
var i, h, a; |
| 22 |
for (i = 0; i < hs.length; i++) { |
| 23 |
h = hs[i]; |
| 24 |
if (!/^h[1-6]$/i.test(h.tagName)) continue; // it should be a header h1-h6 |
| 25 |
a = h.attributes; |
| 26 |
while (a.length > 0) h.removeAttribute(a[0].name); |
| 27 |
} |
| 28 |
}); |
| 29 |
</script> |
| 30 |
|
| 31 |
<style type="text/css"> |
| 32 |
code{white-space: pre-wrap;} |
| 33 |
span.smallcaps{font-variant: small-caps;} |
| 34 |
span.underline{text-decoration: underline;} |
| 35 |
div.column{display: inline-block; vertical-align: top; width: 50%;} |
| 36 |
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;} |
| 37 |
ul.task-list{list-style: none;} |
| 38 |
</style> |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
<style type="text/css"> |
| 43 |
code { |
| 44 |
white-space: pre; |
| 45 |
} |
| 46 |
.sourceCode { |
| 47 |
overflow: visible; |
| 48 |
} |
| 49 |
</style> |
| 50 |
<style type="text/css" data-origin="pandoc"> |
| 51 |
html { -webkit-text-size-adjust: 100%; } |
| 52 |
pre > code.sourceCode { white-space: pre; position: relative; } |
| 53 |
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; } |
| 54 |
pre > code.sourceCode > span:empty { height: 1.2em; } |
| 55 |
.sourceCode { overflow: visible; } |
| 56 |
code.sourceCode > span { color: inherit; text-decoration: inherit; } |
| 57 |
div.sourceCode { margin: 1em 0; } |
| 58 |
pre.sourceCode { margin: 0; } |
| 59 |
@media screen { |
| 60 |
div.sourceCode { overflow: auto; } |
| 61 |
} |
| 62 |
@media print { |
| 63 |
pre > code.sourceCode { white-space: pre-wrap; } |
| 64 |
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; } |
| 65 |
} |
| 66 |
pre.numberSource code |
| 67 |
{ counter-reset: source-line 0; } |
| 68 |
pre.numberSource code > span |
| 69 |
{ position: relative; left: -4em; counter-increment: source-line; } |
| 70 |
pre.numberSource code > span > a:first-child::before |
| 71 |
{ content: counter(source-line); |
| 72 |
position: relative; left: -1em; text-align: right; vertical-align: baseline; |
| 73 |
border: none; display: inline-block; |
| 74 |
-webkit-touch-callout: none; -webkit-user-select: none; |
| 75 |
-khtml-user-select: none; -moz-user-select: none; |
| 76 |
-ms-user-select: none; user-select: none; |
| 77 |
padding: 0 4px; width: 4em; |
| 78 |
color: #aaaaaa; |
| 79 |
} |
| 80 |
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; } |
| 81 |
div.sourceCode |
| 82 |
{ } |
| 83 |
@media screen { |
| 84 |
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; } |
| 85 |
} |
| 86 |
code span.al { color: #ff0000; font-weight: bold; } |
| 87 |
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } |
| 88 |
code span.at { color: #7d9029; } |
| 89 |
code span.bn { color: #40a070; } |
| 90 |
code span.bu { color: #008000; } |
| 91 |
code span.cf { color: #007020; font-weight: bold; } |
| 92 |
code span.ch { color: #4070a0; } |
| 93 |
code span.cn { color: #880000; } |
| 94 |
code span.co { color: #60a0b0; font-style: italic; } |
| 95 |
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } |
| 96 |
code span.do { color: #ba2121; font-style: italic; } |
| 97 |
code span.dt { color: #902000; } |
| 98 |
code span.dv { color: #40a070; } |
| 99 |
code span.er { color: #ff0000; font-weight: bold; } |
| 100 |
code span.ex { } |
| 101 |
code span.fl { color: #40a070; } |
| 102 |
code span.fu { color: #06287e; } |
| 103 |
code span.im { color: #008000; font-weight: bold; } |
| 104 |
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } |
| 105 |
code span.kw { color: #007020; font-weight: bold; } |
| 106 |
code span.op { color: #666666; } |
| 107 |
code span.ot { color: #007020; } |
| 108 |
code span.pp { color: #bc7a00; } |
| 109 |
code span.sc { color: #4070a0; } |
| 110 |
code span.ss { color: #bb6688; } |
| 111 |
code span.st { color: #4070a0; } |
| 112 |
code span.va { color: #19177c; } |
| 113 |
code span.vs { color: #4070a0; } |
| 114 |
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } |
| 115 |
</style> |
| 116 |
<script> |
| 117 |
// apply pandoc div.sourceCode style to pre.sourceCode instead |
| 118 |
(function() { |
| 119 |
var sheets = document.styleSheets; |
| 120 |
for (var i = 0; i < sheets.length; i++) { |
| 121 |
if (sheets[i].ownerNode.dataset["origin"] !== "pandoc") continue; |
| 122 |
try { var rules = sheets[i].cssRules; } catch (e) { continue; } |
| 123 |
var j = 0; |
| 124 |
while (j < rules.length) { |
| 125 |
var rule = rules[j]; |
| 126 |
// check if there is a div.sourceCode rule |
| 127 |
if (rule.type !== rule.STYLE_RULE || rule.selectorText !== "div.sourceCode") { |
| 128 |
j++; |
| 129 |
continue; |
| 130 |
} |
| 131 |
var style = rule.style.cssText; |
| 132 |
// check if color or background-color is set |
| 133 |
if (rule.style.color === '' && rule.style.backgroundColor === '') { |
| 134 |
j++; |
| 135 |
continue; |
| 136 |
} |
| 137 |
// replace div.sourceCode by a pre.sourceCode rule |
| 138 |
sheets[i].deleteRule(j); |
| 139 |
sheets[i].insertRule('pre.sourceCode{' + style + '}', j); |
| 140 |
} |
| 141 |
} |
| 142 |
})(); |
| 143 |
</script> |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
<style type="text/css">body { |
| 149 |
background-color: #fff; |
| 150 |
margin: 1em auto; |
| 151 |
max-width: 700px; |
| 152 |
overflow: visible; |
| 153 |
padding-left: 2em; |
| 154 |
padding-right: 2em; |
| 155 |
font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif; |
| 156 |
font-size: 14px; |
| 157 |
line-height: 1.35; |
| 158 |
} |
| 159 |
#TOC { |
| 160 |
clear: both; |
| 161 |
margin: 0 0 10px 10px; |
| 162 |
padding: 4px; |
| 163 |
width: 400px; |
| 164 |
border: 1px solid #CCCCCC; |
| 165 |
border-radius: 5px; |
| 166 |
background-color: #f6f6f6; |
| 167 |
font-size: 13px; |
| 168 |
line-height: 1.3; |
| 169 |
} |
| 170 |
#TOC .toctitle { |
| 171 |
font-weight: bold; |
| 172 |
font-size: 15px; |
| 173 |
margin-left: 5px; |
| 174 |
} |
| 175 |
#TOC ul { |
| 176 |
padding-left: 40px; |
| 177 |
margin-left: -1.5em; |
| 178 |
margin-top: 5px; |
| 179 |
margin-bottom: 5px; |
| 180 |
} |
| 181 |
#TOC ul ul { |
| 182 |
margin-left: -2em; |
| 183 |
} |
| 184 |
#TOC li { |
| 185 |
line-height: 16px; |
| 186 |
} |
| 187 |
table { |
| 188 |
margin: 1em auto; |
| 189 |
border-width: 1px; |
| 190 |
border-color: #DDDDDD; |
| 191 |
border-style: outset; |
| 192 |
border-collapse: collapse; |
| 193 |
} |
| 194 |
table th { |
| 195 |
border-width: 2px; |
| 196 |
padding: 5px; |
| 197 |
border-style: inset; |
| 198 |
} |
| 199 |
table td { |
| 200 |
border-width: 1px; |
| 201 |
border-style: inset; |
| 202 |
line-height: 18px; |
| 203 |
padding: 5px 5px; |
| 204 |
} |
| 205 |
table, table th, table td { |
| 206 |
border-left-style: none; |
| 207 |
border-right-style: none; |
| 208 |
} |
| 209 |
table thead, table tr.even { |
| 210 |
background-color: #f7f7f7; |
| 211 |
} |
| 212 |
p { |
| 213 |
margin: 0.5em 0; |
| 214 |
} |
| 215 |
blockquote { |
| 216 |
background-color: #f6f6f6; |
| 217 |
padding: 0.25em 0.75em; |
| 218 |
} |
| 219 |
hr { |
| 220 |
border-style: solid; |
| 221 |
border: none; |
| 222 |
border-top: 1px solid #777; |
| 223 |
margin: 28px 0; |
| 224 |
} |
| 225 |
dl { |
| 226 |
margin-left: 0; |
| 227 |
} |
| 228 |
dl dd { |
| 229 |
margin-bottom: 13px; |
| 230 |
margin-left: 13px; |
| 231 |
} |
| 232 |
dl dt { |
| 233 |
font-weight: bold; |
| 234 |
} |
| 235 |
ul { |
| 236 |
margin-top: 0; |
| 237 |
} |
| 238 |
ul li { |
| 239 |
list-style: circle outside; |
| 240 |
} |
| 241 |
ul ul { |
| 242 |
margin-bottom: 0; |
| 243 |
} |
| 244 |
pre, code { |
| 245 |
background-color: #f7f7f7; |
| 246 |
border-radius: 3px; |
| 247 |
color: #333; |
| 248 |
white-space: pre-wrap; |
| 249 |
} |
| 250 |
pre { |
| 251 |
border-radius: 3px; |
| 252 |
margin: 5px 0px 10px 0px; |
| 253 |
padding: 10px; |
| 254 |
} |
| 255 |
pre:not([class]) { |
| 256 |
background-color: #f7f7f7; |
| 257 |
} |
| 258 |
code { |
| 259 |
font-family: Consolas, Monaco, 'Courier New', monospace; |
| 260 |
font-size: 85%; |
| 261 |
} |
| 262 |
p > code, li > code { |
| 263 |
padding: 2px 0px; |
| 264 |
} |
| 265 |
div.figure { |
| 266 |
text-align: center; |
| 267 |
} |
| 268 |
img { |
| 269 |
background-color: #FFFFFF; |
| 270 |
padding: 2px; |
| 271 |
border: 1px solid #DDDDDD; |
| 272 |
border-radius: 3px; |
| 273 |
border: 1px solid #CCCCCC; |
| 274 |
margin: 0 5px; |
| 275 |
} |
| 276 |
h1 { |
| 277 |
margin-top: 0; |
| 278 |
font-size: 35px; |
| 279 |
line-height: 40px; |
| 280 |
} |
| 281 |
h2 { |
| 282 |
border-bottom: 4px solid #f7f7f7; |
| 283 |
padding-top: 10px; |
| 284 |
padding-bottom: 2px; |
| 285 |
font-size: 145%; |
| 286 |
} |
| 287 |
h3 { |
| 288 |
border-bottom: 2px solid #f7f7f7; |
| 289 |
padding-top: 10px; |
| 290 |
font-size: 120%; |
| 291 |
} |
| 292 |
h4 { |
| 293 |
border-bottom: 1px solid #f7f7f7; |
| 294 |
margin-left: 8px; |
| 295 |
font-size: 105%; |
| 296 |
} |
| 297 |
h5, h6 { |
| 298 |
border-bottom: 1px solid #ccc; |
| 299 |
font-size: 105%; |
| 300 |
} |
| 301 |
a { |
| 302 |
color: #0033dd; |
| 303 |
text-decoration: none; |
| 304 |
} |
| 305 |
a:hover { |
| 306 |
color: #6666ff; } |
| 307 |
a:visited { |
| 308 |
color: #800080; } |
| 309 |
a:visited:hover { |
| 310 |
color: #BB00BB; } |
| 311 |
a[href^="http:"] { |
| 312 |
text-decoration: underline; } |
| 313 |
a[href^="https:"] { |
| 314 |
text-decoration: underline; } |
| 315 |
|
| 316 |
code > span.kw { color: #555; font-weight: bold; } |
| 317 |
code > span.dt { color: #902000; } |
| 318 |
code > span.dv { color: #40a070; } |
| 319 |
code > span.bn { color: #d14; } |
| 320 |
code > span.fl { color: #d14; } |
| 321 |
code > span.ch { color: #d14; } |
| 322 |
code > span.st { color: #d14; } |
| 323 |
code > span.co { color: #888888; font-style: italic; } |
| 324 |
code > span.ot { color: #007020; } |
| 325 |
code > span.al { color: #ff0000; font-weight: bold; } |
| 326 |
code > span.fu { color: #900; font-weight: bold; } |
| 327 |
code > span.er { color: #a61717; background-color: #e3d2d2; } |
| 328 |
</style> |
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
</head> |
| 334 |
|
| 335 |
<body> |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
<h1 class="title toc-ignore">Regular expressions</h1> |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
<p>Regular expressions are a concise and flexible tool for describing |
| 345 |
patterns in strings. This vignette describes the key features of |
| 346 |
stringr’s regular expressions, as implemented by <a href="https://github.com/gagolews/stringi">stringi</a>. It is not a |
| 347 |
tutorial, so if you’re unfamiliar regular expressions, I’d recommend |
| 348 |
starting at <a href="https://r4ds.had.co.nz/strings.html" class="uri">https://r4ds.had.co.nz/strings.html</a>. If you want to |
| 349 |
master the details, I’d recommend reading the classic <a href="https://www.amazon.com/Mastering-Regular-Expressions-Jeffrey-Friedl/dp/0596528124"><em>Mastering |
| 350 |
Regular Expressions</em></a> by Jeffrey E. F. Friedl.</p> |
| 351 |
<p>Regular expressions are the default pattern engine in stringr. That |
| 352 |
means when you use a pattern matching function with a bare string, it’s |
| 353 |
equivalent to wrapping it in a call to <code>regex()</code>:</p> |
| 354 |
<div class="sourceCode" id="cb1"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb1-1"><a href="#cb1-1" tabindex="-1"></a><span class="co"># The regular call:</span></span> |
| 355 |
<span id="cb1-2"><a href="#cb1-2" tabindex="-1"></a><span class="fu">str_extract</span>(fruit, <span class="st">"nana"</span>)</span> |
| 356 |
<span id="cb1-3"><a href="#cb1-3" tabindex="-1"></a><span class="co"># Is shorthand for</span></span> |
| 357 |
<span id="cb1-4"><a href="#cb1-4" tabindex="-1"></a><span class="fu">str_extract</span>(fruit, <span class="fu">regex</span>(<span class="st">"nana"</span>))</span></code></pre></div> |
| 358 |
<p>You will need to use <code>regex()</code> explicitly if you want to |
| 359 |
override the default options, as you’ll see in examples below.</p> |
| 360 |
<div id="basic-matches" class="section level2"> |
| 361 |
<h2>Basic matches</h2> |
| 362 |
<p>The simplest patterns match exact strings:</p> |
| 363 |
<div class="sourceCode" id="cb2"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb2-1"><a href="#cb2-1" tabindex="-1"></a>x <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">"apple"</span>, <span class="st">"banana"</span>, <span class="st">"pear"</span>)</span> |
| 364 |
<span id="cb2-2"><a href="#cb2-2" tabindex="-1"></a><span class="fu">str_extract</span>(x, <span class="st">"an"</span>)</span> |
| 365 |
<span id="cb2-3"><a href="#cb2-3" tabindex="-1"></a><span class="co">#> [1] NA "an" NA</span></span></code></pre></div> |
| 366 |
<p>You can perform a case-insensitive match using |
| 367 |
<code>ignore_case = TRUE</code>:</p> |
| 368 |
<div class="sourceCode" id="cb3"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb3-1"><a href="#cb3-1" tabindex="-1"></a>bananas <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">"banana"</span>, <span class="st">"Banana"</span>, <span class="st">"BANANA"</span>)</span> |
| 369 |
<span id="cb3-2"><a href="#cb3-2" tabindex="-1"></a><span class="fu">str_detect</span>(bananas, <span class="st">"banana"</span>)</span> |
| 370 |
<span id="cb3-3"><a href="#cb3-3" tabindex="-1"></a><span class="co">#> [1] TRUE FALSE FALSE</span></span> |
| 371 |
<span id="cb3-4"><a href="#cb3-4" tabindex="-1"></a><span class="fu">str_detect</span>(bananas, <span class="fu">regex</span>(<span class="st">"banana"</span>, <span class="at">ignore_case =</span> <span class="cn">TRUE</span>))</span> |
| 372 |
<span id="cb3-5"><a href="#cb3-5" tabindex="-1"></a><span class="co">#> [1] TRUE TRUE TRUE</span></span></code></pre></div> |
| 373 |
<p>The next step up in complexity is <code>.</code>, which matches any |
| 374 |
character except a newline:</p> |
| 375 |
<div class="sourceCode" id="cb4"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb4-1"><a href="#cb4-1" tabindex="-1"></a><span class="fu">str_extract</span>(x, <span class="st">".a."</span>)</span> |
| 376 |
<span id="cb4-2"><a href="#cb4-2" tabindex="-1"></a><span class="co">#> [1] NA "ban" "ear"</span></span></code></pre></div> |
| 377 |
<p>You can allow <code>.</code> to match everything, including |
| 378 |
<code>\n</code>, by setting <code>dotall = TRUE</code>:</p> |
| 379 |
<div class="sourceCode" id="cb5"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb5-1"><a href="#cb5-1" tabindex="-1"></a><span class="fu">str_detect</span>(<span class="st">"</span><span class="sc">\n</span><span class="st">X</span><span class="sc">\n</span><span class="st">"</span>, <span class="st">".X."</span>)</span> |
| 380 |
<span id="cb5-2"><a href="#cb5-2" tabindex="-1"></a><span class="co">#> [1] FALSE</span></span> |
| 381 |
<span id="cb5-3"><a href="#cb5-3" tabindex="-1"></a><span class="fu">str_detect</span>(<span class="st">"</span><span class="sc">\n</span><span class="st">X</span><span class="sc">\n</span><span class="st">"</span>, <span class="fu">regex</span>(<span class="st">".X."</span>, <span class="at">dotall =</span> <span class="cn">TRUE</span>))</span> |
| 382 |
<span id="cb5-4"><a href="#cb5-4" tabindex="-1"></a><span class="co">#> [1] TRUE</span></span></code></pre></div> |
| 383 |
</div> |
| 384 |
<div id="escaping" class="section level2"> |
| 385 |
<h2>Escaping</h2> |
| 386 |
<p>If “<code>.</code>” matches any character, how do you match a literal |
| 387 |
“<code>.</code>”? You need to use an “escape” to tell the regular |
| 388 |
expression you want to match it exactly, not use its special behaviour. |
| 389 |
Like strings, regexps use the backslash, <code>\</code>, to escape |
| 390 |
special behaviour. So to match an <code>.</code>, you need the regexp |
| 391 |
<code>\.</code>. Unfortunately this creates a problem. We use strings to |
| 392 |
represent regular expressions, and <code>\</code> is also used as an |
| 393 |
escape symbol in strings. So to create the regular expression |
| 394 |
<code>\.</code> we need the string <code>"\\."</code>.</p> |
| 395 |
<div class="sourceCode" id="cb6"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb6-1"><a href="#cb6-1" tabindex="-1"></a><span class="co"># To create the regular expression, we need \\</span></span> |
| 396 |
<span id="cb6-2"><a href="#cb6-2" tabindex="-1"></a>dot <span class="ot"><-</span> <span class="st">"</span><span class="sc">\\</span><span class="st">."</span></span> |
| 397 |
<span id="cb6-3"><a href="#cb6-3" tabindex="-1"></a></span> |
| 398 |
<span id="cb6-4"><a href="#cb6-4" tabindex="-1"></a><span class="co"># But the expression itself only contains one:</span></span> |
| 399 |
<span id="cb6-5"><a href="#cb6-5" tabindex="-1"></a><span class="fu">writeLines</span>(dot)</span> |
| 400 |
<span id="cb6-6"><a href="#cb6-6" tabindex="-1"></a><span class="co">#> \.</span></span> |
| 401 |
<span id="cb6-7"><a href="#cb6-7" tabindex="-1"></a></span> |
| 402 |
<span id="cb6-8"><a href="#cb6-8" tabindex="-1"></a><span class="co"># And this tells R to look for an explicit .</span></span> |
| 403 |
<span id="cb6-9"><a href="#cb6-9" tabindex="-1"></a><span class="fu">str_extract</span>(<span class="fu">c</span>(<span class="st">"abc"</span>, <span class="st">"a.c"</span>, <span class="st">"bef"</span>), <span class="st">"a</span><span class="sc">\\</span><span class="st">.c"</span>)</span> |
| 404 |
<span id="cb6-10"><a href="#cb6-10" tabindex="-1"></a><span class="co">#> [1] NA "a.c" NA</span></span></code></pre></div> |
| 405 |
<p>If <code>\</code> is used as an escape character in regular |
| 406 |
expressions, how do you match a literal <code>\</code>? Well you need to |
| 407 |
escape it, creating the regular expression <code>\\</code>. To create |
| 408 |
that regular expression, you need to use a string, which also needs to |
| 409 |
escape <code>\</code>. That means to match a literal <code>\</code> you |
| 410 |
need to write <code>"\\\\"</code> — you need four backslashes to match |
| 411 |
one!</p> |
| 412 |
<div class="sourceCode" id="cb7"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb7-1"><a href="#cb7-1" tabindex="-1"></a>x <span class="ot"><-</span> <span class="st">"a</span><span class="sc">\\</span><span class="st">b"</span></span> |
| 413 |
<span id="cb7-2"><a href="#cb7-2" tabindex="-1"></a><span class="fu">writeLines</span>(x)</span> |
| 414 |
<span id="cb7-3"><a href="#cb7-3" tabindex="-1"></a><span class="co">#> a\b</span></span> |
| 415 |
<span id="cb7-4"><a href="#cb7-4" tabindex="-1"></a></span> |
| 416 |
<span id="cb7-5"><a href="#cb7-5" tabindex="-1"></a><span class="fu">str_extract</span>(x, <span class="st">"</span><span class="sc">\\\\</span><span class="st">"</span>)</span> |
| 417 |
<span id="cb7-6"><a href="#cb7-6" tabindex="-1"></a><span class="co">#> [1] "\\"</span></span></code></pre></div> |
| 418 |
<p>In this vignette, I use <code>\.</code> to denote the regular |
| 419 |
expression, and <code>"\\."</code> to denote the string that represents |
| 420 |
the regular expression.</p> |
| 421 |
<p>An alternative quoting mechanism is <code>\Q...\E</code>: all the |
| 422 |
characters in <code>...</code> are treated as exact matches. This is |
| 423 |
useful if you want to exactly match user input as part of a regular |
| 424 |
expression.</p> |
| 425 |
<div class="sourceCode" id="cb8"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb8-1"><a href="#cb8-1" tabindex="-1"></a>x <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">"a.b.c.d"</span>, <span class="st">"aeb"</span>)</span> |
| 426 |
<span id="cb8-2"><a href="#cb8-2" tabindex="-1"></a>starts_with <span class="ot"><-</span> <span class="st">"a.b"</span></span> |
| 427 |
<span id="cb8-3"><a href="#cb8-3" tabindex="-1"></a></span> |
| 428 |
<span id="cb8-4"><a href="#cb8-4" tabindex="-1"></a><span class="fu">str_detect</span>(x, <span class="fu">paste0</span>(<span class="st">"^"</span>, starts_with))</span> |
| 429 |
<span id="cb8-5"><a href="#cb8-5" tabindex="-1"></a><span class="co">#> [1] TRUE TRUE</span></span> |
| 430 |
<span id="cb8-6"><a href="#cb8-6" tabindex="-1"></a><span class="fu">str_detect</span>(x, <span class="fu">paste0</span>(<span class="st">"^</span><span class="sc">\\</span><span class="st">Q"</span>, starts_with, <span class="st">"</span><span class="sc">\\</span><span class="st">E"</span>))</span> |
| 431 |
<span id="cb8-7"><a href="#cb8-7" tabindex="-1"></a><span class="co">#> [1] TRUE FALSE</span></span></code></pre></div> |
| 432 |
</div> |
| 433 |
<div id="special-characters" class="section level2"> |
| 434 |
<h2>Special characters</h2> |
| 435 |
<p>Escapes also allow you to specify individual characters that are |
| 436 |
otherwise hard to type. You can specify individual unicode characters in |
| 437 |
five ways, either as a variable number of hex digits (four is most |
| 438 |
common), or by name:</p> |
| 439 |
<ul> |
| 440 |
<li><p><code>\xhh</code>: 2 hex digits.</p></li> |
| 441 |
<li><p><code>\x{hhhh}</code>: 1-6 hex digits.</p></li> |
| 442 |
<li><p><code>\uhhhh</code>: 4 hex digits.</p></li> |
| 443 |
<li><p><code>\Uhhhhhhhh</code>: 8 hex digits.</p></li> |
| 444 |
<li><p><code>\N{name}</code>, e.g. <code>\N{grinning face}</code> |
| 445 |
matches the basic smiling emoji.</p></li> |
| 446 |
</ul> |
| 447 |
<p>Similarly, you can specify many common control characters:</p> |
| 448 |
<ul> |
| 449 |
<li><p><code>\a</code>: bell.</p></li> |
| 450 |
<li><p><code>\cX</code>: match a control-X character.</p></li> |
| 451 |
<li><p><code>\e</code>: escape (<code>\u001B</code>).</p></li> |
| 452 |
<li><p><code>\f</code>: form feed (<code>\u000C</code>).</p></li> |
| 453 |
<li><p><code>\n</code>: line feed (<code>\u000A</code>).</p></li> |
| 454 |
<li><p><code>\r</code>: carriage return (<code>\u000D</code>).</p></li> |
| 455 |
<li><p><code>\t</code>: horizontal tabulation |
| 456 |
(<code>\u0009</code>).</p></li> |
| 457 |
<li><p><code>\0ooo</code> match an octal character. ‘ooo’ is from one to |
| 458 |
three octal digits, from 000 to 0377. The leading zero is |
| 459 |
required.</p></li> |
| 460 |
</ul> |
| 461 |
<p>(Many of these are only of historical interest and are only included |
| 462 |
here for the sake of completeness.)</p> |
| 463 |
</div> |
| 464 |
<div id="matching-multiple-characters" class="section level2"> |
| 465 |
<h2>Matching multiple characters</h2> |
| 466 |
<p>There are a number of patterns that match more than one character. |
| 467 |
You’ve already seen <code>.</code>, which matches any character (except |
| 468 |
a newline). A closely related operator is <code>\X</code>, which matches |
| 469 |
a <strong>grapheme cluster</strong>, a set of individual elements that |
| 470 |
form a single symbol. For example, one way of representing “á” is as the |
| 471 |
letter “a” plus an accent: <code>.</code> will match the component “a”, |
| 472 |
while <code>\X</code> will match the complete symbol:</p> |
| 473 |
<div class="sourceCode" id="cb9"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb9-1"><a href="#cb9-1" tabindex="-1"></a>x <span class="ot"><-</span> <span class="st">"a\u0301"</span></span> |
| 474 |
<span id="cb9-2"><a href="#cb9-2" tabindex="-1"></a><span class="fu">str_extract</span>(x, <span class="st">"."</span>)</span> |
| 475 |
<span id="cb9-3"><a href="#cb9-3" tabindex="-1"></a><span class="co">#> [1] "a"</span></span> |
| 476 |
<span id="cb9-4"><a href="#cb9-4" tabindex="-1"></a><span class="fu">str_extract</span>(x, <span class="st">"</span><span class="sc">\\</span><span class="st">X"</span>)</span> |
| 477 |
<span id="cb9-5"><a href="#cb9-5" tabindex="-1"></a><span class="co">#> [1] "á"</span></span></code></pre></div> |
| 478 |
<p>There are five other escaped pairs that match narrower classes of |
| 479 |
characters:</p> |
| 480 |
<ul> |
| 481 |
<li><p><code>\d</code>: matches any digit. The complement, |
| 482 |
<code>\D</code>, matches any character that is not a decimal digit.</p> |
| 483 |
<div class="sourceCode" id="cb10"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb10-1"><a href="#cb10-1" tabindex="-1"></a><span class="fu">str_extract_all</span>(<span class="st">"1 + 2 = 3"</span>, <span class="st">"</span><span class="sc">\\</span><span class="st">d+"</span>)[[<span class="dv">1</span>]]</span> |
| 484 |
<span id="cb10-2"><a href="#cb10-2" tabindex="-1"></a><span class="co">#> [1] "1" "2" "3"</span></span></code></pre></div> |
| 485 |
<p>Technically, <code>\d</code> includes any character in the Unicode |
| 486 |
Category of Nd (“Number, Decimal Digit”), which also includes numeric |
| 487 |
symbols from other languages:</p> |
| 488 |
<div class="sourceCode" id="cb11"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb11-1"><a href="#cb11-1" tabindex="-1"></a><span class="co"># Some Laotian numbers</span></span> |
| 489 |
<span id="cb11-2"><a href="#cb11-2" tabindex="-1"></a><span class="fu">str_detect</span>(<span class="st">"១២៣"</span>, <span class="st">"</span><span class="sc">\\</span><span class="st">d"</span>)</span> |
| 490 |
<span id="cb11-3"><a href="#cb11-3" tabindex="-1"></a><span class="co">#> [1] TRUE</span></span></code></pre></div></li> |
| 491 |
<li><p><code>\s</code>: matches any whitespace. This includes tabs, |
| 492 |
newlines, form feeds, and any character in the Unicode Z Category (which |
| 493 |
includes a variety of space characters and other separators.). The |
| 494 |
complement, <code>\S</code>, matches any non-whitespace character.</p> |
| 495 |
<div class="sourceCode" id="cb12"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb12-1"><a href="#cb12-1" tabindex="-1"></a>(text <span class="ot"><-</span> <span class="st">"Some </span><span class="sc">\t</span><span class="st"> badly</span><span class="sc">\n\t\t</span><span class="st">spaced </span><span class="sc">\f</span><span class="st"> text"</span>)</span> |
| 496 |
<span id="cb12-2"><a href="#cb12-2" tabindex="-1"></a><span class="co">#> [1] "Some \t badly\n\t\tspaced \f text"</span></span> |
| 497 |
<span id="cb12-3"><a href="#cb12-3" tabindex="-1"></a><span class="fu">str_replace_all</span>(text, <span class="st">"</span><span class="sc">\\</span><span class="st">s+"</span>, <span class="st">" "</span>)</span> |
| 498 |
<span id="cb12-4"><a href="#cb12-4" tabindex="-1"></a><span class="co">#> [1] "Some badly spaced text"</span></span></code></pre></div></li> |
| 499 |
<li><p><code>\p{property name}</code> matches any character with |
| 500 |
specific unicode property, like <code>\p{Uppercase}</code> or |
| 501 |
<code>\p{Diacritic}</code>. The complement, |
| 502 |
<code>\P{property name}</code>, matches all characters without the |
| 503 |
property. A complete list of unicode properties can be found at <a href="http://www.unicode.org/reports/tr44/#Property_Index" class="uri">http://www.unicode.org/reports/tr44/#Property_Index</a>.</p> |
| 504 |
<div class="sourceCode" id="cb13"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb13-1"><a href="#cb13-1" tabindex="-1"></a>(text <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">'"Double quotes"'</span>, <span class="st">"«Guillemet»"</span>, <span class="st">"“Fancy quotes”"</span>))</span> |
| 505 |
<span id="cb13-2"><a href="#cb13-2" tabindex="-1"></a><span class="co">#> [1] "\"Double quotes\"" "«Guillemet»" "“Fancy quotes”"</span></span> |
| 506 |
<span id="cb13-3"><a href="#cb13-3" tabindex="-1"></a><span class="fu">str_replace_all</span>(text, <span class="st">"</span><span class="sc">\\</span><span class="st">p{quotation mark}"</span>, <span class="st">"'"</span>)</span> |
| 507 |
<span id="cb13-4"><a href="#cb13-4" tabindex="-1"></a><span class="co">#> [1] "'Double quotes'" "'Guillemet'" "'Fancy quotes'"</span></span></code></pre></div></li> |
| 508 |
<li><p><code>\w</code> matches any “word” character, which includes |
| 509 |
alphabetic characters, marks and decimal numbers. The complement, |
| 510 |
<code>\W</code>, matches any non-word character.</p> |
| 511 |
<div class="sourceCode" id="cb14"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb14-1"><a href="#cb14-1" tabindex="-1"></a><span class="fu">str_extract_all</span>(<span class="st">"Don't eat that!"</span>, <span class="st">"</span><span class="sc">\\</span><span class="st">w+"</span>)[[<span class="dv">1</span>]]</span> |
| 512 |
<span id="cb14-2"><a href="#cb14-2" tabindex="-1"></a><span class="co">#> [1] "Don" "t" "eat" "that"</span></span> |
| 513 |
<span id="cb14-3"><a href="#cb14-3" tabindex="-1"></a><span class="fu">str_split</span>(<span class="st">"Don't eat that!"</span>, <span class="st">"</span><span class="sc">\\</span><span class="st">W"</span>)[[<span class="dv">1</span>]]</span> |
| 514 |
<span id="cb14-4"><a href="#cb14-4" tabindex="-1"></a><span class="co">#> [1] "Don" "t" "eat" "that" ""</span></span></code></pre></div> |
| 515 |
<p>Technically, <code>\w</code> also matches connector punctuation, |
| 516 |
<code>\u200c</code> (zero width connector), and <code>\u200d</code> |
| 517 |
(zero width joiner), but these are rarely seen in the wild.</p></li> |
| 518 |
<li><p><code>\b</code> matches word boundaries, the transition between |
| 519 |
word and non-word characters. <code>\B</code> matches the opposite: |
| 520 |
boundaries that have either both word or non-word characters on either |
| 521 |
side.</p> |
| 522 |
<div class="sourceCode" id="cb15"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb15-1"><a href="#cb15-1" tabindex="-1"></a><span class="fu">str_replace_all</span>(<span class="st">"The quick brown fox"</span>, <span class="st">"</span><span class="sc">\\</span><span class="st">b"</span>, <span class="st">"_"</span>)</span> |
| 523 |
<span id="cb15-2"><a href="#cb15-2" tabindex="-1"></a><span class="co">#> [1] "_The_ _quick_ _brown_ _fox_"</span></span> |
| 524 |
<span id="cb15-3"><a href="#cb15-3" tabindex="-1"></a><span class="fu">str_replace_all</span>(<span class="st">"The quick brown fox"</span>, <span class="st">"</span><span class="sc">\\</span><span class="st">B"</span>, <span class="st">"_"</span>)</span> |
| 525 |
<span id="cb15-4"><a href="#cb15-4" tabindex="-1"></a><span class="co">#> [1] "T_h_e q_u_i_c_k b_r_o_w_n f_o_x"</span></span></code></pre></div></li> |
| 526 |
</ul> |
| 527 |
<p>You can also create your own <strong>character classes</strong> using |
| 528 |
<code>[]</code>:</p> |
| 529 |
<ul> |
| 530 |
<li><code>[abc]</code>: matches a, b, or c.</li> |
| 531 |
<li><code>[a-z]</code>: matches every character between a and z (in |
| 532 |
Unicode code point order).</li> |
| 533 |
<li><code>[^abc]</code>: matches anything except a, b, or c.</li> |
| 534 |
<li><code>[\^\-]</code>: matches <code>^</code> or <code>-</code>.</li> |
| 535 |
</ul> |
| 536 |
<p>There are a number of pre-built classes that you can use inside |
| 537 |
<code>[]</code>:</p> |
| 538 |
<ul> |
| 539 |
<li><code>[:punct:]</code>: punctuation.</li> |
| 540 |
<li><code>[:alpha:]</code>: letters.</li> |
| 541 |
<li><code>[:lower:]</code>: lowercase letters.</li> |
| 542 |
<li><code>[:upper:]</code>: upperclass letters.</li> |
| 543 |
<li><code>[:digit:]</code>: digits.</li> |
| 544 |
<li><code>[:xdigit:]</code>: hex digits.</li> |
| 545 |
<li><code>[:alnum:]</code>: letters and numbers.</li> |
| 546 |
<li><code>[:cntrl:]</code>: control characters.</li> |
| 547 |
<li><code>[:graph:]</code>: letters, numbers, and punctuation.</li> |
| 548 |
<li><code>[:print:]</code>: letters, numbers, punctuation, and |
| 549 |
whitespace.</li> |
| 550 |
<li><code>[:space:]</code>: space characters (basically equivalent to |
| 551 |
<code>\s</code>).</li> |
| 552 |
<li><code>[:blank:]</code>: space and tab.</li> |
| 553 |
</ul> |
| 554 |
<p>These all go inside the <code>[]</code> for character classes, |
| 555 |
i.e. <code>[[:digit:]AX]</code> matches all digits, A, and X.</p> |
| 556 |
<p>You can also using Unicode properties, like |
| 557 |
<code>[\p{Letter}]</code>, and various set operations, like |
| 558 |
<code>[\p{Letter}--\p{script=latin}]</code>. See |
| 559 |
<code>?"stringi-search-charclass"</code> for details.</p> |
| 560 |
</div> |
| 561 |
<div id="alternation" class="section level2"> |
| 562 |
<h2>Alternation</h2> |
| 563 |
<p><code>|</code> is the <strong>alternation</strong> operator, which |
| 564 |
will pick between one or more possible matches. For example, |
| 565 |
<code>abc|def</code> will match <code>abc</code> or |
| 566 |
<code>def</code>:</p> |
| 567 |
<div class="sourceCode" id="cb16"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb16-1"><a href="#cb16-1" tabindex="-1"></a><span class="fu">str_detect</span>(<span class="fu">c</span>(<span class="st">"abc"</span>, <span class="st">"def"</span>, <span class="st">"ghi"</span>), <span class="st">"abc|def"</span>)</span> |
| 568 |
<span id="cb16-2"><a href="#cb16-2" tabindex="-1"></a><span class="co">#> [1] TRUE TRUE FALSE</span></span></code></pre></div> |
| 569 |
<p>Note that the precedence for <code>|</code> is low: |
| 570 |
<code>abc|def</code> is equivalent to <code>(abc)|(def)</code> not |
| 571 |
<code>ab(c|d)ef</code>.</p> |
| 572 |
</div> |
| 573 |
<div id="grouping" class="section level2"> |
| 574 |
<h2>Grouping</h2> |
| 575 |
<p>You can use parentheses to override the default precedence rules:</p> |
| 576 |
<div class="sourceCode" id="cb17"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb17-1"><a href="#cb17-1" tabindex="-1"></a><span class="fu">str_extract</span>(<span class="fu">c</span>(<span class="st">"grey"</span>, <span class="st">"gray"</span>), <span class="st">"gre|ay"</span>)</span> |
| 577 |
<span id="cb17-2"><a href="#cb17-2" tabindex="-1"></a><span class="co">#> [1] "gre" "ay"</span></span> |
| 578 |
<span id="cb17-3"><a href="#cb17-3" tabindex="-1"></a><span class="fu">str_extract</span>(<span class="fu">c</span>(<span class="st">"grey"</span>, <span class="st">"gray"</span>), <span class="st">"gr(e|a)y"</span>)</span> |
| 579 |
<span id="cb17-4"><a href="#cb17-4" tabindex="-1"></a><span class="co">#> [1] "grey" "gray"</span></span></code></pre></div> |
| 580 |
<p>Parentheses also define “groups” that you can refer to with |
| 581 |
<strong>backreferences</strong>, like <code>\1</code>, <code>\2</code> |
| 582 |
etc, and can be extracted with <code>str_match()</code>. For example, |
| 583 |
the following regular expression finds all fruits that have a repeated |
| 584 |
pair of letters:</p> |
| 585 |
<div class="sourceCode" id="cb18"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb18-1"><a href="#cb18-1" tabindex="-1"></a>pattern <span class="ot"><-</span> <span class="st">"(..)</span><span class="sc">\\</span><span class="st">1"</span></span> |
| 586 |
<span id="cb18-2"><a href="#cb18-2" tabindex="-1"></a>fruit <span class="sc">%>%</span> </span> |
| 587 |
<span id="cb18-3"><a href="#cb18-3" tabindex="-1"></a> <span class="fu">str_subset</span>(pattern)</span> |
| 588 |
<span id="cb18-4"><a href="#cb18-4" tabindex="-1"></a><span class="co">#> [1] "banana"</span></span> |
| 589 |
<span id="cb18-5"><a href="#cb18-5" tabindex="-1"></a></span> |
| 590 |
<span id="cb18-6"><a href="#cb18-6" tabindex="-1"></a>fruit <span class="sc">%>%</span> </span> |
| 591 |
<span id="cb18-7"><a href="#cb18-7" tabindex="-1"></a> <span class="fu">str_subset</span>(pattern) <span class="sc">%>%</span> </span> |
| 592 |
<span id="cb18-8"><a href="#cb18-8" tabindex="-1"></a> <span class="fu">str_match</span>(pattern)</span> |
| 593 |
<span id="cb18-9"><a href="#cb18-9" tabindex="-1"></a><span class="co">#> [,1] [,2]</span></span> |
| 594 |
<span id="cb18-10"><a href="#cb18-10" tabindex="-1"></a><span class="co">#> [1,] "anan" "an"</span></span></code></pre></div> |
| 595 |
<p>You can use <code>(?:...)</code>, the non-grouping parentheses, to |
| 596 |
control precedence but not capture the match in a group. This is |
| 597 |
slightly more efficient than capturing parentheses.</p> |
| 598 |
<div class="sourceCode" id="cb19"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb19-1"><a href="#cb19-1" tabindex="-1"></a><span class="fu">str_match</span>(<span class="fu">c</span>(<span class="st">"grey"</span>, <span class="st">"gray"</span>), <span class="st">"gr(e|a)y"</span>)</span> |
| 599 |
<span id="cb19-2"><a href="#cb19-2" tabindex="-1"></a><span class="co">#> [,1] [,2]</span></span> |
| 600 |
<span id="cb19-3"><a href="#cb19-3" tabindex="-1"></a><span class="co">#> [1,] "grey" "e" </span></span> |
| 601 |
<span id="cb19-4"><a href="#cb19-4" tabindex="-1"></a><span class="co">#> [2,] "gray" "a"</span></span> |
| 602 |
<span id="cb19-5"><a href="#cb19-5" tabindex="-1"></a><span class="fu">str_match</span>(<span class="fu">c</span>(<span class="st">"grey"</span>, <span class="st">"gray"</span>), <span class="st">"gr(?:e|a)y"</span>)</span> |
| 603 |
<span id="cb19-6"><a href="#cb19-6" tabindex="-1"></a><span class="co">#> [,1] </span></span> |
| 604 |
<span id="cb19-7"><a href="#cb19-7" tabindex="-1"></a><span class="co">#> [1,] "grey"</span></span> |
| 605 |
<span id="cb19-8"><a href="#cb19-8" tabindex="-1"></a><span class="co">#> [2,] "gray"</span></span></code></pre></div> |
| 606 |
<p>This is most useful for more complex cases where you need to capture |
| 607 |
matches and control precedence independently.</p> |
| 608 |
<p>You can use <code>(?<name>...)</code>, the named capture group, |
| 609 |
to provide a reference to the matched text. This is more readable and |
| 610 |
maintainable, especially with complex regular expressions, because you |
| 611 |
can reference the matched text by name instead of a potentially |
| 612 |
confusing numerical index.</p> |
| 613 |
<p><em>Note: <code><name></code> should not include an underscore |
| 614 |
because they are not supported.</em></p> |
| 615 |
<div class="sourceCode" id="cb20"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb20-1"><a href="#cb20-1" tabindex="-1"></a>date_string <span class="ot"><-</span> <span class="st">"Today's date is 2025-09-19."</span></span> |
| 616 |
<span id="cb20-2"><a href="#cb20-2" tabindex="-1"></a>pattern <span class="ot"><-</span> <span class="st">"(?<year></span><span class="sc">\\</span><span class="st">d{4})-(?<month></span><span class="sc">\\</span><span class="st">d{2})-(?<day></span><span class="sc">\\</span><span class="st">d{2})"</span></span> |
| 617 |
<span id="cb20-3"><a href="#cb20-3" tabindex="-1"></a><span class="fu">str_match</span>(date_string, pattern)</span> |
| 618 |
<span id="cb20-4"><a href="#cb20-4" tabindex="-1"></a><span class="co">#> year month day </span></span> |
| 619 |
<span id="cb20-5"><a href="#cb20-5" tabindex="-1"></a><span class="co">#> [1,] "2025-09-19" "2025" "09" "19"</span></span></code></pre></div> |
| 620 |
<p>You can then use <code>\k<name></code> to backreference the |
| 621 |
previously captured named group. It is an alternative to the standard |
| 622 |
numbered backreferences like <code>\1</code> or <code>\2</code>.</p> |
| 623 |
<div class="sourceCode" id="cb21"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb21-1"><a href="#cb21-1" tabindex="-1"></a>text <span class="ot"><-</span> <span class="st">"This is is a test test with duplicates duplicates"</span></span> |
| 624 |
<span id="cb21-2"><a href="#cb21-2" tabindex="-1"></a>pattern <span class="ot"><-</span> <span class="st">"(?<word></span><span class="sc">\\</span><span class="st">b</span><span class="sc">\\</span><span class="st">w+</span><span class="sc">\\</span><span class="st">b)</span><span class="sc">\\</span><span class="st">s+</span><span class="sc">\\</span><span class="st">k<word>"</span></span> |
| 625 |
<span id="cb21-3"><a href="#cb21-3" tabindex="-1"></a><span class="fu">str_subset</span>(text, pattern)</span> |
| 626 |
<span id="cb21-4"><a href="#cb21-4" tabindex="-1"></a><span class="co">#> [1] "This is is a test test with duplicates duplicates"</span></span> |
| 627 |
<span id="cb21-5"><a href="#cb21-5" tabindex="-1"></a><span class="fu">str_match_all</span>(text, pattern)</span> |
| 628 |
<span id="cb21-6"><a href="#cb21-6" tabindex="-1"></a><span class="co">#> [[1]]</span></span> |
| 629 |
<span id="cb21-7"><a href="#cb21-7" tabindex="-1"></a><span class="co">#> word </span></span> |
| 630 |
<span id="cb21-8"><a href="#cb21-8" tabindex="-1"></a><span class="co">#> [1,] "is is" "is" </span></span> |
| 631 |
<span id="cb21-9"><a href="#cb21-9" tabindex="-1"></a><span class="co">#> [2,] "test test" "test" </span></span> |
| 632 |
<span id="cb21-10"><a href="#cb21-10" tabindex="-1"></a><span class="co">#> [3,] "duplicates duplicates" "duplicates"</span></span></code></pre></div> |
| 633 |
</div> |
| 634 |
<div id="anchors" class="section level2"> |
| 635 |
<h2>Anchors</h2> |
| 636 |
<p>By default, regular expressions will match any part of a string. It’s |
| 637 |
often useful to <strong>anchor</strong> the regular expression so that |
| 638 |
it matches from the start or end of the string:</p> |
| 639 |
<ul> |
| 640 |
<li><code>^</code> matches the start of string.</li> |
| 641 |
<li><code>$</code> matches the end of the string.</li> |
| 642 |
</ul> |
| 643 |
<div class="sourceCode" id="cb22"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb22-1"><a href="#cb22-1" tabindex="-1"></a>x <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">"apple"</span>, <span class="st">"banana"</span>, <span class="st">"pear"</span>)</span> |
| 644 |
<span id="cb22-2"><a href="#cb22-2" tabindex="-1"></a><span class="fu">str_extract</span>(x, <span class="st">"^a"</span>)</span> |
| 645 |
<span id="cb22-3"><a href="#cb22-3" tabindex="-1"></a><span class="co">#> [1] "a" NA NA</span></span> |
| 646 |
<span id="cb22-4"><a href="#cb22-4" tabindex="-1"></a><span class="fu">str_extract</span>(x, <span class="st">"a$"</span>)</span> |
| 647 |
<span id="cb22-5"><a href="#cb22-5" tabindex="-1"></a><span class="co">#> [1] NA "a" NA</span></span></code></pre></div> |
| 648 |
<p>To match a literal “$” or “^”, you need to escape them, |
| 649 |
<code>\$</code>, and <code>\^</code>.</p> |
| 650 |
<p>For multiline strings, you can use |
| 651 |
<code>regex(multiline = TRUE)</code>. This changes the behaviour of |
| 652 |
<code>^</code> and <code>$</code>, and introduces three new |
| 653 |
operators:</p> |
| 654 |
<ul> |
| 655 |
<li><p><code>^</code> now matches the start of each line.</p></li> |
| 656 |
<li><p><code>$</code> now matches the end of each line.</p></li> |
| 657 |
<li><p><code>\A</code> matches the start of the input.</p></li> |
| 658 |
<li><p><code>\z</code> matches the end of the input.</p></li> |
| 659 |
<li><p><code>\Z</code> matches the end of the input, but before the |
| 660 |
final line terminator, if it exists.</p></li> |
| 661 |
</ul> |
| 662 |
<div class="sourceCode" id="cb23"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb23-1"><a href="#cb23-1" tabindex="-1"></a>x <span class="ot"><-</span> <span class="st">"Line 1</span><span class="sc">\n</span><span class="st">Line 2</span><span class="sc">\n</span><span class="st">Line 3</span><span class="sc">\n</span><span class="st">"</span></span> |
| 663 |
<span id="cb23-2"><a href="#cb23-2" tabindex="-1"></a><span class="fu">str_extract_all</span>(x, <span class="st">"^Line.."</span>)[[<span class="dv">1</span>]]</span> |
| 664 |
<span id="cb23-3"><a href="#cb23-3" tabindex="-1"></a><span class="co">#> [1] "Line 1"</span></span> |
| 665 |
<span id="cb23-4"><a href="#cb23-4" tabindex="-1"></a><span class="fu">str_extract_all</span>(x, <span class="fu">regex</span>(<span class="st">"^Line.."</span>, <span class="at">multiline =</span> <span class="cn">TRUE</span>))[[<span class="dv">1</span>]]</span> |
| 666 |
<span id="cb23-5"><a href="#cb23-5" tabindex="-1"></a><span class="co">#> [1] "Line 1" "Line 2" "Line 3"</span></span> |
| 667 |
<span id="cb23-6"><a href="#cb23-6" tabindex="-1"></a><span class="fu">str_extract_all</span>(x, <span class="fu">regex</span>(<span class="st">"</span><span class="sc">\\</span><span class="st">ALine.."</span>, <span class="at">multiline =</span> <span class="cn">TRUE</span>))[[<span class="dv">1</span>]]</span> |
| 668 |
<span id="cb23-7"><a href="#cb23-7" tabindex="-1"></a><span class="co">#> [1] "Line 1"</span></span></code></pre></div> |
| 669 |
</div> |
| 670 |
<div id="repetition" class="section level2"> |
| 671 |
<h2>Repetition</h2> |
| 672 |
<p>You can control how many times a pattern matches with the repetition |
| 673 |
operators:</p> |
| 674 |
<ul> |
| 675 |
<li><code>?</code>: 0 or 1.</li> |
| 676 |
<li><code>+</code>: 1 or more.</li> |
| 677 |
<li><code>*</code>: 0 or more.</li> |
| 678 |
</ul> |
| 679 |
<div class="sourceCode" id="cb24"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb24-1"><a href="#cb24-1" tabindex="-1"></a>x <span class="ot"><-</span> <span class="st">"1888 is the longest year in Roman numerals: MDCCCLXXXVIII"</span></span> |
| 680 |
<span id="cb24-2"><a href="#cb24-2" tabindex="-1"></a><span class="fu">str_extract</span>(x, <span class="st">"CC?"</span>)</span> |
| 681 |
<span id="cb24-3"><a href="#cb24-3" tabindex="-1"></a><span class="co">#> [1] "CC"</span></span> |
| 682 |
<span id="cb24-4"><a href="#cb24-4" tabindex="-1"></a><span class="fu">str_extract</span>(x, <span class="st">"CC+"</span>)</span> |
| 683 |
<span id="cb24-5"><a href="#cb24-5" tabindex="-1"></a><span class="co">#> [1] "CCC"</span></span> |
| 684 |
<span id="cb24-6"><a href="#cb24-6" tabindex="-1"></a><span class="fu">str_extract</span>(x, <span class="st">'C[LX]+'</span>)</span> |
| 685 |
<span id="cb24-7"><a href="#cb24-7" tabindex="-1"></a><span class="co">#> [1] "CLXXX"</span></span></code></pre></div> |
| 686 |
<p>Note that the precedence of these operators is high, so you can |
| 687 |
write: <code>colou?r</code> to match either American or British |
| 688 |
spellings. That means most uses will need parentheses, like |
| 689 |
<code>bana(na)+</code>.</p> |
| 690 |
<p>You can also specify the number of matches precisely:</p> |
| 691 |
<ul> |
| 692 |
<li><code>{n}</code>: exactly n</li> |
| 693 |
<li><code>{n,}</code>: n or more</li> |
| 694 |
<li><code>{n,m}</code>: between n and m</li> |
| 695 |
</ul> |
| 696 |
<div class="sourceCode" id="cb25"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb25-1"><a href="#cb25-1" tabindex="-1"></a><span class="fu">str_extract</span>(x, <span class="st">"C{2}"</span>)</span> |
| 697 |
<span id="cb25-2"><a href="#cb25-2" tabindex="-1"></a><span class="co">#> [1] "CC"</span></span> |
| 698 |
<span id="cb25-3"><a href="#cb25-3" tabindex="-1"></a><span class="fu">str_extract</span>(x, <span class="st">"C{2,}"</span>)</span> |
| 699 |
<span id="cb25-4"><a href="#cb25-4" tabindex="-1"></a><span class="co">#> [1] "CCC"</span></span> |
| 700 |
<span id="cb25-5"><a href="#cb25-5" tabindex="-1"></a><span class="fu">str_extract</span>(x, <span class="st">"C{2,3}"</span>)</span> |
| 701 |
<span id="cb25-6"><a href="#cb25-6" tabindex="-1"></a><span class="co">#> [1] "CCC"</span></span></code></pre></div> |
| 702 |
<p>By default these matches are “greedy”: they will match the longest |
| 703 |
string possible. You can make them “lazy”, matching the shortest string |
| 704 |
possible by putting a <code>?</code> after them:</p> |
| 705 |
<ul> |
| 706 |
<li><code>??</code>: 0 or 1, prefer 0.</li> |
| 707 |
<li><code>+?</code>: 1 or more, match as few times as possible.</li> |
| 708 |
<li><code>*?</code>: 0 or more, match as few times as possible.</li> |
| 709 |
<li><code>{n,}?</code>: n or more, match as few times as possible.</li> |
| 710 |
<li><code>{n,m}?</code>: between n and m, , match as few times as |
| 711 |
possible, but at least n.</li> |
| 712 |
</ul> |
| 713 |
<div class="sourceCode" id="cb26"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb26-1"><a href="#cb26-1" tabindex="-1"></a><span class="fu">str_extract</span>(x, <span class="fu">c</span>(<span class="st">"C{2,3}"</span>, <span class="st">"C{2,3}?"</span>))</span> |
| 714 |
<span id="cb26-2"><a href="#cb26-2" tabindex="-1"></a><span class="co">#> [1] "CCC" "CC"</span></span> |
| 715 |
<span id="cb26-3"><a href="#cb26-3" tabindex="-1"></a><span class="fu">str_extract</span>(x, <span class="fu">c</span>(<span class="st">"C[LX]+"</span>, <span class="st">"C[LX]+?"</span>))</span> |
| 716 |
<span id="cb26-4"><a href="#cb26-4" tabindex="-1"></a><span class="co">#> [1] "CLXXX" "CL"</span></span></code></pre></div> |
| 717 |
<p>You can also make the matches possessive by putting a <code>+</code> |
| 718 |
after them, which means that if later parts of the match fail, the |
| 719 |
repetition will not be re-tried with a smaller number of characters. |
| 720 |
This is an advanced feature used to improve performance in worst-case |
| 721 |
scenarios (called “catastrophic backtracking”).</p> |
| 722 |
<ul> |
| 723 |
<li><code>?+</code>: 0 or 1, possessive.</li> |
| 724 |
<li><code>++</code>: 1 or more, possessive.</li> |
| 725 |
<li><code>*+</code>: 0 or more, possessive.</li> |
| 726 |
<li><code>{n}+</code>: exactly n, possessive.</li> |
| 727 |
<li><code>{n,}+</code>: n or more, possessive.</li> |
| 728 |
<li><code>{n,m}+</code>: between n and m, possessive.</li> |
| 729 |
</ul> |
| 730 |
<p>A related concept is the <strong>atomic-match</strong> parenthesis, |
| 731 |
<code>(?>...)</code>. If a later match fails and the engine needs to |
| 732 |
back-track, an atomic match is kept as is: it succeeds or fails as a |
| 733 |
whole. Compare the following two regular expressions:</p> |
| 734 |
<div class="sourceCode" id="cb27"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb27-1"><a href="#cb27-1" tabindex="-1"></a><span class="fu">str_detect</span>(<span class="st">"ABC"</span>, <span class="st">"(?>A|.B)C"</span>)</span> |
| 735 |
<span id="cb27-2"><a href="#cb27-2" tabindex="-1"></a><span class="co">#> [1] FALSE</span></span> |
| 736 |
<span id="cb27-3"><a href="#cb27-3" tabindex="-1"></a><span class="fu">str_detect</span>(<span class="st">"ABC"</span>, <span class="st">"(?:A|.B)C"</span>)</span> |
| 737 |
<span id="cb27-4"><a href="#cb27-4" tabindex="-1"></a><span class="co">#> [1] TRUE</span></span></code></pre></div> |
| 738 |
<p>The atomic match fails because it matches A, and then the next |
| 739 |
character is a C so it fails. The regular match succeeds because it |
| 740 |
matches A, but then C doesn’t match, so it back-tracks and tries B |
| 741 |
instead.</p> |
| 742 |
</div> |
| 743 |
<div id="look-arounds" class="section level2"> |
| 744 |
<h2>Look arounds</h2> |
| 745 |
<p>These assertions look ahead or behind the current match without |
| 746 |
“consuming” any characters (i.e. changing the input position).</p> |
| 747 |
<ul> |
| 748 |
<li><p><code>(?=...)</code>: positive look-ahead assertion. Matches if |
| 749 |
<code>...</code> matches at the current input.</p></li> |
| 750 |
<li><p><code>(?!...)</code>: negative look-ahead assertion. Matches if |
| 751 |
<code>...</code> <strong>does not</strong> match at the current |
| 752 |
input.</p></li> |
| 753 |
<li><p><code>(?<=...)</code>: positive look-behind assertion. Matches |
| 754 |
if <code>...</code> matches text preceding the current position, with |
| 755 |
the last character of the match being the character just before the |
| 756 |
current position. Length must be bounded<br /> |
| 757 |
(i.e. no <code>*</code> or <code>+</code>).</p></li> |
| 758 |
<li><p><code>(?<!...)</code>: negative look-behind assertion. Matches |
| 759 |
if <code>...</code> <strong>does not</strong> match text preceding the |
| 760 |
current position. Length must be bounded<br /> |
| 761 |
(i.e. no <code>*</code> or <code>+</code>).</p></li> |
| 762 |
</ul> |
| 763 |
<p>These are useful when you want to check that a pattern exists, but |
| 764 |
you don’t want to include it in the result:</p> |
| 765 |
<div class="sourceCode" id="cb28"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb28-1"><a href="#cb28-1" tabindex="-1"></a>x <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">"1 piece"</span>, <span class="st">"2 pieces"</span>, <span class="st">"3"</span>)</span> |
| 766 |
<span id="cb28-2"><a href="#cb28-2" tabindex="-1"></a><span class="fu">str_extract</span>(x, <span class="st">"</span><span class="sc">\\</span><span class="st">d+(?= pieces?)"</span>)</span> |
| 767 |
<span id="cb28-3"><a href="#cb28-3" tabindex="-1"></a><span class="co">#> [1] "1" "2" NA</span></span> |
| 768 |
<span id="cb28-4"><a href="#cb28-4" tabindex="-1"></a></span> |
| 769 |
<span id="cb28-5"><a href="#cb28-5" tabindex="-1"></a>y <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">"100"</span>, <span class="st">"$400"</span>)</span> |
| 770 |
<span id="cb28-6"><a href="#cb28-6" tabindex="-1"></a><span class="fu">str_extract</span>(y, <span class="st">"(?<=</span><span class="sc">\\</span><span class="st">$)</span><span class="sc">\\</span><span class="st">d+"</span>)</span> |
| 771 |
<span id="cb28-7"><a href="#cb28-7" tabindex="-1"></a><span class="co">#> [1] NA "400"</span></span></code></pre></div> |
| 772 |
</div> |
| 773 |
<div id="comments" class="section level2"> |
| 774 |
<h2>Comments</h2> |
| 775 |
<p>There are two ways to include comments in a regular expression. The |
| 776 |
first is with <code>(?#...)</code>:</p> |
| 777 |
<div class="sourceCode" id="cb29"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb29-1"><a href="#cb29-1" tabindex="-1"></a><span class="fu">str_detect</span>(<span class="st">"xyz"</span>, <span class="st">"x(?#this is a comment)"</span>)</span> |
| 778 |
<span id="cb29-2"><a href="#cb29-2" tabindex="-1"></a><span class="co">#> [1] TRUE</span></span></code></pre></div> |
| 779 |
<p>The second is to use <code>regex(comments = TRUE)</code>. This form |
| 780 |
ignores spaces and newlines, and anything everything after |
| 781 |
<code>#</code>. To match a literal space, you’ll need to escape it: |
| 782 |
<code>"\\ "</code>. This is a useful way of describing complex regular |
| 783 |
expressions:</p> |
| 784 |
<div class="sourceCode" id="cb30"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb30-1"><a href="#cb30-1" tabindex="-1"></a>phone <span class="ot"><-</span> <span class="fu">regex</span>(<span class="st">"</span></span> |
| 785 |
<span id="cb30-2"><a href="#cb30-2" tabindex="-1"></a><span class="st"> </span><span class="sc">\\</span><span class="st">(? # optional opening parens</span></span> |
| 786 |
<span id="cb30-3"><a href="#cb30-3" tabindex="-1"></a><span class="st"> (</span><span class="sc">\\</span><span class="st">d{3}) # area code</span></span> |
| 787 |
<span id="cb30-4"><a href="#cb30-4" tabindex="-1"></a><span class="st"> </span><span class="sc">\\</span><span class="st">)? # optional closing parens</span></span> |
| 788 |
<span id="cb30-5"><a href="#cb30-5" tabindex="-1"></a><span class="st"> (?:-|</span><span class="sc">\\</span><span class="st"> )? # optional dash or space</span></span> |
| 789 |
<span id="cb30-6"><a href="#cb30-6" tabindex="-1"></a><span class="st"> (</span><span class="sc">\\</span><span class="st">d{3}) # another three numbers</span></span> |
| 790 |
<span id="cb30-7"><a href="#cb30-7" tabindex="-1"></a><span class="st"> (?:-|</span><span class="sc">\\</span><span class="st"> )? # optional dash or space</span></span> |
| 791 |
<span id="cb30-8"><a href="#cb30-8" tabindex="-1"></a><span class="st"> (</span><span class="sc">\\</span><span class="st">d{3}) # three more numbers</span></span> |
| 792 |
<span id="cb30-9"><a href="#cb30-9" tabindex="-1"></a><span class="st"> "</span>, <span class="at">comments =</span> <span class="cn">TRUE</span>)</span> |
| 793 |
<span id="cb30-10"><a href="#cb30-10" tabindex="-1"></a></span> |
| 794 |
<span id="cb30-11"><a href="#cb30-11" tabindex="-1"></a><span class="fu">str_match</span>(<span class="fu">c</span>(<span class="st">"514-791-8141"</span>, <span class="st">"(514) 791 8141"</span>), phone)</span> |
| 795 |
<span id="cb30-12"><a href="#cb30-12" tabindex="-1"></a><span class="co">#> [,1] [,2] [,3] [,4] </span></span> |
| 796 |
<span id="cb30-13"><a href="#cb30-13" tabindex="-1"></a><span class="co">#> [1,] "514-791-814" "514" "791" "814"</span></span> |
| 797 |
<span id="cb30-14"><a href="#cb30-14" tabindex="-1"></a><span class="co">#> [2,] "(514) 791 814" "514" "791" "814"</span></span></code></pre></div> |
| 798 |
</div> |
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
<script> |
| 807 |
(function () { |
| 808 |
var script = document.createElement("script"); |
| 809 |
script.type = "text/javascript"; |
| 810 |
script.src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"; |
| 811 |
document.getElementsByTagName("head")[0].appendChild(script); |
| 812 |
})(); |
| 813 |
</script> |
| 814 |
|
| 815 |
</body> |
| 816 |
</html> |