---
title: "Quantifiers and Scope"
author: "Statistical Computing, 36-350"
date: "Wednesday September 14, 2016"
---

Quantifiers
===

**Quantifiers** in a regex allow us to express: how often?

- "+" means "1 or more times"
- "*" means "0 or more times"
- "?" means "0 or 1 times" (optional, once)
- "{n}" means "exactly n times"
- "{n,}" means "n or more times"
- "{n,m}" means "between n and m times" (inclusive)

Examples
===

```{r}
grep("Oh+", c("O my gosh!", "Oh wow!", "Ohhhhh no!"), value=TRUE)
grep("Oh*", c("O my gosh!", "Oh wow!", "Ohhhhh no!"), value=TRUE)
grep("colou?r", c("Americans love 3 colors: red, white, and blue",
                  "Brits love 3 colours: red, white, and dark blue"), val=TRUE)
grep("Bonds?\\?", c("Have you seen Barry Bonds? That guy can play",
                   "Have you seen James Bond? That guy is cool",
                   "Bond v United States, 529 U.S. 334 (2000)"), value=TRUE)
```

More examples
===

```{r}
grep("10{1,2} ", c("10 dollars", "100 dollars", "1000 dollars"), value=TRUE)
grep("10{1,2}", c("10 dollars", "100 dollars", "1000 dollars"), value=TRUE)
grep("10{2,}", c("10 dollars", "100 dollars", "1000 dollars"), value=TRUE)
grep("[0-9]{3}-[0-9]{4}", c("My office number is 268-1884",
                            "Bryan's cell phone is 353-1890",
                            "The police's number is 911"), value=TRUE)
```

Scope
===

What exactly does a quantifier apply to? This is called its **scope**

- By default, it applies to the last (meta)character 
- Use parentheses to have it apply to a whole group

```{r}
grep("ha{2,}", c("haaa", "haha"), value=TRUE)
grep("(ha){2,}", c("haaa", "haha"), value=TRUE)
grep("[0-9][[:alpha:]]{2}", c("2L2Q", "21YO"), value=TRUE)
grep("([0-9][[:alpha:]]){2}", c("2L2Q", "21YO"), value=TRUE)
```

More examples
===

```{r}
grep("[0-9]{3}(-[0-9]{4})?", c("My office number is 268-1884",
                             "The police's number is 911",
                             "Wait, 911- isn't that the police?"), value=TRUE)
grep("[0-9]{3}-([0-9]{4})?", c("My office number is 268-1884",
                             "The police's number is 911",
                             "Wait, 911- isn't that the police?"), value=TRUE)
grep("ton.*", c("ton", "tone", "ton ", "son"), value=TRUE)
grep("(ton.)*", c("ton", "tone", "ton ", "son"), value=TRUE)
```

Anchoring
===

- When "^" is used outside of square brackers, it means we will only look for a match at the start of a line
- When "$" is used, it means we will only look for a match at the end of a line
- These are called **anchors**;  "^" is applied on the left, "$" on the right

```{r}
grep("^Win", c("Winning is my favorite pasttime",
               "We love statistics", "I hate Windows"), value=TRUE)
grep("[a-z]$", c("I like lasers", "I like LASERS"), value=TRUE)
```

More examples
===

```{r}
grep("Do.*\\?$", c("Do you like cherries?", "Don't you like terriers?",
                   "Don't you know that terriers like cherries"), value=TRUE)
grep("^<.+>|<.+>$", c("<HTML> hi", "bye </HTML>", "a <b> c"), value=TRUE)
```
