---
title: "regex"
format: html
---

Regular Expressions are a powerful way to search for text.


# Literal Characters: 

Most letters and numbers match themselves exactly. cat matches "cat".
The Dot (`.`): This special character matches any single character except, usually, a newline.

# Character Classes (`[]`): 

Square brackets let you match any one character contained within them. `[abc]` matches "a", "b", or "c". A hyphen specifies a range, so `[a-z]` matches any lowercase letter.

# Negation (`[^]`): 

A caret `^` at the start of a character class negates it. `[^abc]` matches any character except "a", "b", or "c".

# Quantifiers (`*`, `+`, `?`): 

These control how many times the preceding element can occur.
. `*` matches zero or more occurrences (e.g., `A*` matches "", "A", "AA", etc.).
. `+` matches one or more occurrences (e.g., `A+` matches "A", "AA", etc., but not "").
. `?` matches zero or one occurrence (e.g., colou?r matches "color" or "colour").

# Anchors (`^`, `$`): 

These match a position, not a character.
. `^` matches the beginning of the string or line.
. `$` matches the end of the string or line.

# Common Shorthands (`\d`, `\w`, `\s`): 

These escape sequences provide quick access to common character sets.
. `\d` matches any digit (0-9).
. `\w` matches any word character (alphanumeric and underscore).
. `\s` matches any whitespace character (spaces, tabs, newlines).

# Alternation (`|`): 

The pipe character acts as a logical OR. cat`|`dog matches either "cat" or "dog".

# Grouping and Capturing (`()`): 

Parentheses group parts of the expression together, so quantifiers or alternation can apply to the entire group. They also create "capture groups" which allow you to extract the matched text.

# Escaping (`\`): 

To match a character that has a special meaning (like `.`, `*`, or `?`), you must "escape" it with a backslash. For example, `\.` matches a literal period. 