---
title: "Introduction to Regular Expressions"
author: "Fafa"
date: "Your Date"
output:
  beamer_presentation:
    theme: "Metropolis"
    #colortheme: "dolphin"
    #fonttheme: "structurebold"
---

```{r}
library(tidyverse)

```


## Section 1: Introduction to Regular Expressions

### 1.1 What are Regular Expressions?

Regular expressions (regex) define search patterns for text within strings or files.

**Key Points:**

-   Regex is a sequence of characters defining a search pattern.
-   Used for text processing in various languages and tools (e.g., R, Python)
-   Can match, find, and replace strings.

**Applications of regular expressions**

-   Identifying patterns in texts.
-   Cleaning data
-   Data validation (e.g., format of emails)
-   Search and Replace

**Simple Example:**

Finding "cat" in a text.

```{r, echo=FALSE}

example_text <- "The cat sat on the mat."

str_extract(example_text, pattern = "cat") 


```

## Section 2: Basics of Regex Syntax

### 2.1 Fundamental Characters and Symbols

Regular expressions use specific characters and symbols to define patterns.

-   **Dot `.`** - Matches any single character except newline.
-   **Asterisk `*`** - Matches the preceding element zero or more times.

```{r}
text <- "cat mat bat"

str_extract(text, pattern = ".*") # Matches every character

```


-   **Caret `^`** - Matches the start of a string.

```{r}

text <- "First text"
text2 <- "Second text"

str_extract(text, pattern = "^First") # Matches if 'start' is at the beginning of text

str_extract(text2, pattern = "^First") # Matches if 'start' is at the beginning of text2


```

-   **Dollar `$`** - Matches the end of a string.

```{r}

text <- "here is the end"
text2 <- "here is the ending"

str_extract(text, pattern = "end$") # Matches if 'end' is at the end
str_extract(text2, pattern = "end$") # Matches if 'end' is at the end
str_extract(text2, pattern = "end.*$") # Matches if 'end' is at the end


```


-   **Plus `+`** - Matches the preceding element one or more times.

```{r}

text <- "Faaa Faaa"

str_extract(text, pattern = "a+") # Matches if 'end' is at the end

str_extract_all(text, pattern = "a+") # Matches if 'end' is at the end

```


-   **Question Mark `?`** - Matches the preceding element zero or one time.

```{r}
text <- "colour color"

str_extract_all(text, pattern = "colou?r") # Matches both 'colour' and 'color'

```

-   **Square Brackets `[]`** - Matches any one of the enclosed characters.

```{r}
text <- "cat bat rat mat"

str_extract_all(text, pattern = "[br]at") # Matches 'bat' and 'rat'\

```

-   **Parentheses `()`** - Groups elements.

```{r}

text <- "recode decode encode"

str_extract_all(text, pattern = "(re|de)code") # Matches 'bat' and 'rat'


```


-   **Pipe `|`** - Logical OR.

```{r}

text <- "cat bat mat"
str_extract_all(text, pattern = "cat|bat") # Matches 'bat' and 'rat'\

```


### 2.2 Character Classes and Sets

Character classes provide a way to match predefined sets of characters.

-   **Digit `\d`** - Matches any digit (equivalent to `[0-9]`).

```{r}

text <- "Room 101"
str_extract(text, pattern = "\\d") # Matches '1'
str_extract(text, pattern = "\\d+") # Matches the whole number

```


-   **Word `\w`** - Matches any word character (letters, digits, and underscores).

```{r}

text <- "Room 101"

str_extract(text, pattern = "\\w")
str_extract(text, pattern = "\\w+") # Matches word characters


```


-   **Whitespace `\s`** - Matches any whitespace character (spaces, tabs, line breaks)

.
```{r}

text <- "Room      101"
str_extract(text, pattern = "\\s+") # Matches whitespace including newline

```

**Custom Character Sets:** - Use square brackets to define a custom set of characters. - Example: `[A-Za-z]` matches any uppercase or lowercase letter.

### 2.3 Quantifiers and Repetitions

Quantifiers control how many times an element is matched.

-   **Curly Braces `{}`** - Specify a specific number of repetitions.

```{r}

text <- "wooooow"
str_extract(text, pattern = "o{3}") # Matches 'o' exactly 3 times


```

-   **Greedy vs Lazy Quantifiers:**
    -   Greedy quantifiers match as much text as possible.
    -   Lazy quantifiers match as little text as possible (often followed by `?`).

**Example:** Finding repeated letters.

\`\`\`{r, echo=FALSE} example_text \<- "I'm loooking for patterns in thiiis text." regmatches(example_text, gregexpr("o{2,}", example_text))[[1]]
