From 5e65371942d13421bc5925338aea6ba8313ed1cd Mon Sep 17 00:00:00 2001 From: hadley Date: Tue, 27 Oct 2015 11:03:27 -0500 Subject: [PATCH] Use str_view htmlwidget --- .travis.yml | 2 +- strings.Rmd | 10 +++++----- www/.gitignore | 4 +--- 3 files changed, 7 insertions(+), 9 deletions(-) diff --git a/.travis.yml b/.travis.yml index 35c22e0..2c47a5e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -23,7 +23,7 @@ install: # Install R packages - ./travis-tool.sh r_binary_install knitr png - ./travis-tool.sh r_install ggplot2 dplyr tidyr pryr stringr - - ./travis-tool.sh github_package hadley/bookdown garrettgman/DSR hadley/readr gaborcsardi/rcorpora + - ./travis-tool.sh github_package hadley/bookdown garrettgman/DSR hadley/readr gaborcsardi/rcorpora hadley/stringr script: jekyll build diff --git a/strings.Rmd b/strings.Rmd index 33e83ab..140121c 100644 --- a/strings.Rmd +++ b/strings.Rmd @@ -176,7 +176,7 @@ common <- rcorpora::corpora("words/common")$commonWords ### Match anything and escaping ```{r} -str_subset(c("abc", "adc", "bef"), "a.c") +str_view(c("abc", "adc", "bef"), "a.c") ``` But if "`.`" matches any character, how do you match an actual "`.`"? You need to use an "escape" to tell the regular expression you want to match it exactly, not use the special behaviour. The escape character used by regular expressions is `\`. Unfortunately, that's also the escape character used by strings, so to match a literal "`.`" you need to use `\\.`. @@ -189,7 +189,7 @@ dot <- "\\." cat(dot, "\n") # And this tells R to look for explicit . -str_subset(c("abc", "a.c", "bef"), "a\\.c") +str_view(c("abc", "a.c", "bef"), "a\\.c") ``` If `\` is used an escape character, how do you match a literal `\`? Well you need to escape it, creating the regular expression `\\`. And in R that needs to be in a string, so you need to write `"\\\\"` - that's right, you need four backslashes to match one! @@ -218,7 +218,7 @@ Regular expressions can also match things that are not characters. The most impo To force a regular expression to only match a complete string, anchor it with both `^` and `$`.: ```{r} -str_detect(c("abcdef", "bcd"), "^bcd$") +str_view(c("abcdef", "bcd"), "^bcd$") ``` My favourite mneomic for rememember which is which (from [Evan Misshula](https://twitter.com/emisshula/status/323863393167613953): begin with power (`^`), end with money (`$`). @@ -246,13 +246,13 @@ Remember, to create a regular expression containing `\d` or `\s`, you'll need to A similar idea is alternation: `x|y` matches either x or y. Note that the precedence for `|` is low, so that `abc|xyz` matches either `abc` or `xyz` not `abcyz` or `abxyz`: ```{r} -str_detect(c("abc", "xyz"), "abc|xyz") +str_view(c("abc", "xyz"), "abc|xyz") ``` Like with mathematical expression, if precedence ever gets confusing, use parentheses to make it clear what you want: ```{r} -str_detect(c("grey", "gray"), "gr(e|a)y") +str_view(c("grey", "gray"), "gr(e|a)y") ``` Practice these by finding: diff --git a/www/.gitignore b/www/.gitignore index 51c709b..72e8ffc 100644 --- a/www/.gitignore +++ b/www/.gitignore @@ -1,3 +1 @@ -bootstrap-2.3.2/ -highlight/ -jquery-1.11.0/ \ No newline at end of file +*