Regex
Documentation for the /regex command to work with regular expressions
/regex
Command
The /regex
command allows you to work with regular expressions (regex) directly in Discord and Slack. Regular expressions are powerful pattern matching tools used for text manipulation, validation, and searching.
Available Subcommands
/regex test
Tests if a regex pattern matches a text string.
Usage
Options
Option | Description | Required | Platform |
---|---|---|---|
pattern | The regex pattern to test | Yes | Discord (parameter), Slack (flag with —pattern) |
text | The text to test against the pattern | Yes | Discord (parameter), Slack (flag with —text) |
Example
This will test if “hello” matches the pattern ”^[a-z]+$” (only lowercase letters).
/regex match
Finds all matches of a regex pattern in text.
Usage
Options
Option | Description | Required | Platform |
---|---|---|---|
pattern | The regex pattern to match | Yes | Discord (parameter), Slack (flag with —pattern) |
text | The text to find matches in | Yes | Discord (parameter), Slack (flag with —text) |
limit | Maximum number of matches to return (default: 10) | No | Discord (parameter), Slack (flag with —limit) |
Example
This will find all numbers in the text (“42” and “15”).
/regex replace
Replaces text matching a regex pattern.
Usage
Options
Option | Description | Required | Platform |
---|---|---|---|
pattern | The regex pattern to match | Yes | Discord (parameter), Slack (flag with —pattern) |
text | The text to perform replacements on | Yes | Discord (parameter), Slack (flag with —text) |
replacement | The replacement text (can include 2 for captured groups) | Yes | Discord (parameter), Slack (flag with —replacement) |
limit | Maximum number of replacements (0 for all, default: 0) | No | Discord (parameter), Slack (flag with —limit) |
Example
This will replace all email addresses with @example.com domain to @company.com while preserving the username part.
/regex split
Splits text based on a regex pattern.
Usage
Options
Option | Description | Required | Platform |
---|---|---|---|
pattern | The regex pattern to split on | Yes | Discord (parameter), Slack (flag with —pattern) |
text | The text to split | Yes | Discord (parameter), Slack (flag with —text) |
limit | Maximum number of splits (0 for all, default: 0) | No | Discord (parameter), Slack (flag with —limit) |
Example
This will split the text on commas and semicolons, resulting in [“apple”, “banana”, “orange”, “grape”].
/regex analyze
Analyzes a regex pattern and provides information about it.
Usage
Options
Option | Description | Required | Platform |
---|---|---|---|
pattern | The regex pattern to analyze | Yes | Discord (parameter), Slack (flag with —pattern) |
Example
This will analyze the pattern for a social security number format (###-##-####) and provide information about the components of the regex.
Platform-Specific Implementation
Discord Implementation
- Uses Discord’s named parameter system (e.g.,
pattern:
,text:
,replacement:
) - Responses appear as embedded messages with color-coding
- Analysis results are displayed in an organized, hierarchical format
- Test results use color-coded indicators for matches/non-matches
Slack Implementation
- Uses a flag-based approach for parameters (e.g.,
--pattern
,--text
,--replacement
) - Arguments following flags that contain spaces should be enclosed in quotes
- Responses are shown with formatted blocks displaying the result and related information
- Test results use colored attachments to clearly indicate success or failure
- Analysis results are split into manageable chunks if too large
- Captured groups in matches are clearly labeled and formatted
Common Regex Patterns
Here are some commonly used regex patterns:
Pattern | Description | Example |
---|---|---|
\d+ | One or more digits | ”123”, “42” |
\w+ | One or more word characters (letters, digits, underscore) | “hello_123” |
[a-zA-Z]+ | One or more letters | ”HelloWorld” |
^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$ | Email validation | ”user@example.com” |
^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$ | Password with at least 8 characters, one letter and one number | ”password123” |
^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$ | URL validation | ”https://example.com” |
Using Captured Groups
Regular expressions can capture parts of the matched text using parentheses ()
. These captured groups can be referenced in replacements using $1
, $2
, etc.
For example:
This will convert “score:42 lives:3” to “score = 42 lives = 3”.
Notes
- Complex regular expressions may have performance implications
- The regex engine uses Go’s standard library implementation
- Invalid regex patterns will return appropriate error messages
- Very long inputs or results may be truncated in the output
Was this page helpful?