> For the complete documentation index, see [llms.txt](https://fastiraz.gitbook.io/doc/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://fastiraz.gitbook.io/doc/documentations/cheat-sheet/regex.md).

# Regex

Regular expressions, commonly known as regex, are powerful sequences of characters that define a search pattern. In programming, regex serves as a versatile tool for pattern matching within strings. It allows developers to express complex search criteria, enabling the identification and manipulation of text based on specific patterns. With regex, programmers can efficiently perform tasks such as data validation, text parsing, and search and replace operations. Whether it's validating user inputs, extracting information from log files, or manipulating data in a structured manner, regex provides a concise and flexible syntax to streamline these tasks. Its widespread adoption across programming languages makes it an indispensable tool for developers seeking to enhance the efficiency and precision of their string processing tasks.

***

{% hint style="info" %}
Check the link below to:

* Learn
* Practice
* Test a regex with your data
* Or see a better cheat sheet

> <https://regexlearn.com/>
> {% endhint %}

***

## Special Characters

* `.`: Wildcard, matches any single character except a newline.
* `*`: Quantifier, matches zero or more occurrences of the preceding element.
* `?`: Quantifier (optional), matches zero or one occurrence of the preceding element.
* `^`: Asserts the start of a line.
* `$`: Asserts the end of a line.
* `+`: Quantifier, matches one or more occurrences of the preceding element.
* `(`: Opens a group for capturing and grouping.
* `)`: Closes a group initiated by an opening parenthesis.
* `[`: Opens a character class, matches any one of the characters within the brackets.
* `]`: Closes a character class initiated by an opening square bracket.
* `\`: Escape character, treats the following character literally.

## Character Classes

* `\d`: Matches any digit (0-9).
* `\D`: Matches any non-digit.
* `\w`: Matches any word character (alphanumeric + underscore).
* `\W`: Matches any non-word character.
* `\s`: Matches any whitespace character (space, tab, newline).
* `\S`: Matches any non-whitespace character.

## Quantifiers

* `{n}`: Matches exactly n occurrences of the preceding element.
* `{n,}`: Matches n or more occurrences of the preceding element.
* `{n,m}`: Matches between n and m occurrences of the preceding element.

## Anchors

* `\b`: Word boundary, asserts a position at the beginning or end of a word.
* `\B`: Non-word boundary.

## Assertions

* `(?=...)`: Positive lookahead assertion.
* `(?!...)`: Negative lookahead assertion.
* `(?<=...)`: Positive lookbehind assertion.
* `(?<!...)`: Negative lookbehind assertion.

## Escape Sequences

* `\n`: Newline.
* `\t`: Tab.
* `\r`: Carriage return.
* `\`: Escapes a metacharacter, treats it literally.

## Flags

* `g`: Global
* `m`: Multiline
* `i`: Case Insensitive

## Example Patterns

* `^\d{3}-\d{2}-\d{4}$`: Matches a social security number.
* `^([a-zA-Z0-9._%+-]+)@([a-zA-Z0-9.-]+)\.([a-zA-Z]{2,})$`: Matches an email address.
* `^\(\d{3}\) \d{3}-\d{4}$`: Matches a phone number in the format (XXX) XXX-XXXX.
* `^https?://\w+(\.\w+)+(/[\w/.]*)?$`: Matches a basic URL.
* `[\w-]{24}\.[\w-]{6}\.[\w-]{27}`: Discord token.
* `mfa\.[\w-]{84}`: Discord MFA token.
* `^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*()_+])[\w!@#$%^&*()_+]{8,}$`: Matches a password with at least 8 characters, including at least one uppercase letter, one lowercase letter, one digit, and one special character.

{% embed url="<https://media.giphy.com/media/3o6Mbik7fXdeOG1u0M/giphy.gif>" %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://fastiraz.gitbook.io/doc/documentations/cheat-sheet/regex.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
