-
Notifications
You must be signed in to change notification settings - Fork 4
/
README.Rmd
188 lines (140 loc) · 6 KB
/
README.Rmd
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
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
<!-- Keep this file sync'ed with vignette -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "README-"
)
library(re2)
```
# re2: R interface to Google RE2
## Overview
re2 package provides pattern matching, extraction, replacement and other string processing operations using Google's [RE2](https://github.com/google/re2) (C++) regular-expression library. The interface is consistent, and similar to [stringr](https://github.com/tidyverse/stringr).
Why re2?
Regular expression matching can be done in two ways: using recursive
backtracking or using finite automata-based techniques.
Perl, PCRE, Python, Ruby, Java, and many other languages rely on
recursive backtracking for their regular expression implementations.
The problem with this approach is that performance can degrade very
quickly. Time complexity can be exponential.
In contrast, re2 uses finite automata-based techniques for regular
expression matching,
guaranteeing linear time execution and a fixed stack footprint. See
links to Russ Cox's excellent articles below.
## Installation
```r
# Install the released version from CRAN:
install.packages("re2")
# Install the development version from GitHub:
# install.packages("devtools")
devtools::install_github("girishji/re2")
```
## Usage
re2 provides three types of regular-expression functions:
- Find the presence of a pattern in string
- Extract substrings that match a pattern
- Replace matched groups
All functions take a vector of strings as argument. Regular-expression patterns can be compiled, and reused for performance.
Here are the primary verbs of re2:
* `re2_detect(x, pattern)` finds if a pattern is present in string
```{r}
re2_detect(c("barbazbla", "foobar", "foxy brown"), "(foo)|(bar)baz")
```
* `re2_count(x, pattern)` counts the number of matches in string
```{r}
re2_count(c("yellowgreen", "steelblue", "maroon"), "e")
```
* `re2_subset(x, pattern)` selects strings that match
```{r}
re2_subset(c("yellowgreen", "steelblue", "goldenrod"), "ee")
```
* `re2_match(x, pattern, simplify = FALSE)` extracts first matched substring
```{r}
re2_match("ruby:1234 68 red:92 blue:", "(\\w+):(\\d+)")
```
```{r}
# Groups can be named:
re2_match(c("barbazbla", "foobar"), "(foo)|(?P<TestGroup>bar)baz")
```
```{r}
# Use pre-compiled regular expression:
re <- re2_regexp("(foo)|(bar)baz", case_sensitive = FALSE)
re2_match(c("BaRbazbla", "Foobar"), re)
```
* `re2_match_all(x, pattern)` extracts all matched substrings
```{r}
re2_match_all("ruby:1234 68 red:92 blue:", "(\\w+):(\\d+)")
```
* `re2_replace(x, pattern, rewrite)` replaces first matched pattern in string
```{r}
re2_replace("yabba dabba doo", "b+", "d")
```
```{r}
# Use groups in rewrite:
re2_replace("bunny@wunnies.pl", "(.*)@([^.]*)", "\\2!\\1")
```
* `re2_replace_all(x, pattern, rewrite)` replaces all matched patterns in string
```{r}
re2_replace_all("yabba dabba doo", "b+", "d")
```
* `re2_extract_replace(x, pattern, rewrite)` extracts and substitutes (ignores non-matching portions of x)
```{r}
re2_extract_replace("bunny@wunnies.pl", "(.*)@([^.]*)", "\\2!\\1")
```
* `re2_split(x, pattern, simplify = FALSE, n = Inf)` splits string based on pattern
```{r}
re2_split("How vexingly quick daft zebras jump!", " quick | zebras")
```
* `re2_locate(x, pattern)` seeks the start and end of pattern in string
```{r}
re2_locate(c("yellowgreen", "steelblue"), "l(b)?l")
```
* `re2_locate_all(x, pattern)` locates start and end of all occurrences of pattern in string
```{r}
re2_locate_all(c("yellowgreen", "steelblue"), "l")
```
In all the above functions, regular-expression pattern is vectorized.
Regular-expression pattern can be compiled using `re2_regexp(pattern, ...)`. Here are some of the options:
* `case_sensitive`: Match is case-sensitive
* `encoding`: UTF8 or Latin1
* `literal`: Interpret pattern as literal, not regexp
* `longest_match`: Search for longest match, not first match
* `posix_syntax`: Restrict regexps to POSIX egrep syntax
`help(re2_regexp)` lists available options.
`re2_get_options(regexp_ptr)` returns a list of options stored
in the compiled regular-expression object.
## Regexp Syntax
re2 supports pearl style regular expressions (with extensions like
\\d, \\w, \\s, ...) and provides most of the functionality of
PCRE -- eschewing only backreferences and look-around
assertions.
See [RE2 Syntax](https://github.com/girishji/re2/wiki/Syntax) for the syntax supported by RE2, and a comparison with PCRE and PERL regexps.
For those not familiar with Perl's regular expressions,
here are some examples of the most commonly used extensions:
| | |
| --- | --- |
| `"hello (\\w+) world"` | `\w` matches a "word" character |
| `"version (\\d+)"` | `\d` matches a digit |
| `"hello\\s+world"` | `\s` matches any whitespace character |
| `"\\b(\\w+)\\b"` | `\b` matches non-empty string at word boundary |
| `"(?i)hello"` | `(?i)` turns on case-insensitive matching |
| `"/\\*(.*?)\\*/"` | `.*?` matches . minimum no. of times possible |
The double backslashes are needed when writing R string literals.
However, they should not be used when writing raw string literals:
| | |
| --- | --- |
| `r"(hello (\w+) world)"` | `\w` matches a "word" character |
| `r"(version (\d+))"` | `\d` matches a digit |
| `r"(hello\s+world)"` | `\s` matches any whitespace character |
| `r"(\b(\w+)\b)"` | `\b` matches non-empty string at word boundary |
| `r"((?i)hello)"` | `(?i)` turns on case-insensitive matching |
| `r"(/\*(.*?)\*/)"` | `.*?` matches `.` minimum no. of times possible |
## References
* [Regular Expression Matching Can Be Simple And Fast](https://swtch.com/~rsc/regexp/regexp1.html)
* [Regular Expression Matching: the Virtual Machine Approach](https://swtch.com/~rsc/regexp/regexp2.html)
* [Regular Expression Matching in the Wild](https://swtch.com/~rsc/regexp/regexp3.html)
* [RE2 Syntax](https://github.com/google/re2/wiki/Syntax)