Skip to content

Instantly share code, notes, and snippets.

@gadenbuie
Created March 9, 2018 19:21
Show Gist options
  • Save gadenbuie/c7acc2cbadf9c07bb6e806cfc5ffed85 to your computer and use it in GitHub Desktop.
Save gadenbuie/c7acc2cbadf9c07bb6e806cfc5ffed85 to your computer and use it in GitHub Desktop.
---
title: regextest notes
output:
html_document:
css: inst/style.css
keep_md: yes
---
```{r setup, include=FALSE}
library(purrr)
library(dplyr)
library(regexhelp)
```
```{r}
r <- "((\\w+)=)(\\w+).+(ch=s?p)"
text <- c("breakfast=eggs;lunch=pizza","breakfast=bacon;lunch=spaghetti", "no food here")
view_regex(text, r, knitr = TRUE)
```
From <https://github.com/wibeasley/class-regex-2015/issues/6#issuecomment-200658631>
```{r, results='asis'}
text3 <- c(
"intage2000",
"intage2005",
"intage2009",
"measure_a_2000",
"measure_a_2005",
"measure_a_2009",
"measure_b_2000",
"measure_b_2005",
"measure_b_2009"
)
iris$Species
r3 <- "^(\\w+?)_?(\\d{4})$"
view_regex(text3, r3, knitr = TRUE)
```
```{r}
text2 <- c("16_24cat", "25_34cat", "35_44catch", "45_54Cat", "55_104fat")
r2 <- "([1-3][1-9][a-z])"
view_regex(text2, r2, knitr = TRUE)
```
The following brings up the issue of how to escape the original text.
Note that `stringr::str_view` does not handle this at all.
```{r}
text4 <- c("<TAG>one<TAG>two</TAG>one</TAG>")
r4 <- "<([A-Z][A-Z0-9]*)[^>]*>(.*?)</\\1>"
view_regex(text4, r4, knitr = TRUE)
```
```{r}
t_nested <- "anestedgroupwithingroupexample"
r_nested <- "(a(nested)(group(within(group))(example)))"
view_regex(t_nested, r_nested, knitr = TRUE)
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment