Slack Bot / Webhook Token Regex for Java
/^(xoxb|xoxp|xoxa|xoxr|xoxs|xapp)-[A-Za-z0-9\-]{10,200}$|^https://hooks\.slack\.com/services/T[A-Z0-9]+/B[A-Z0-9]+/[A-Za-z0-9]+$/What this pattern does
This page provides a comprehensive, battle-tested regular expression for matching slack bot / webhook token, ported and verified for Java. In security-sensitive code, using an unverified regex can open the door to both false positives and denial-of-service attacks. The snippet below is ready to drop into your Java project — whether you're validating in a Spring Boot controller, a Jakarta EE service, or a standalone utility class.
Java Implementation
// Slack Bot / Webhook Token
// ReDoS-safe | RegexVault — Security > API Keys & Tokens
import java.util.regex.Pattern;
public class SlackBotWebhookTokenValidator {
private static final Pattern PATTERN =
Pattern.compile("^(xoxb|xoxp|xoxa|xoxr|xoxs|xapp)-[A-Za-z0-9\\-]{10,200}$|^https://hooks\\.slack\\.com/services/T[A-Z0-9]+/B[A-Z0-9]+/[A-Za-z0-9]+$");
public static boolean validate(String input) {
return PATTERN.matcher(input).matches();
}
// Example
public static void main(String[] args) {
System.out.println(validate("xoxb-123456789012-123456789012-ABC123def456GHI789jkl0")); // true
}
}Test Cases
Matches (Valid) | Rejects (Invalid) |
|---|---|
xoxb-123456789012-123456789012-ABC123def456GHI789jkl0 | xox-invalid-format |
xoxp-1234-5678-9012-abcdef1234567890abcdef | slack_token_aBcDeFgHiJkLmNoPqRsTuV |
https://hooks.slack.com/services/T12345/B12345/AbCdEfGhIj | xoxb-short |
When to use this pattern
This pattern is drawn from the Security > API Keys & Tokens category and carries a ReDoS-safe certification. That matters for Java developers because critical in Java applications since the JVM regex engine uses backtracking and is susceptible to ReDoS without careful pattern design. RegexVault audits patterns against known backtracking attack vectors, ensuring you have the necessary context before using this regex in a high-stakes production environment.
Common Pitfalls
Bot tokens (xoxb) can access all channels the bot is in. User tokens (xoxp) act as that specific user. A leaked user token is equivalent to that user's password for Slack.
Technical Notes
Token prefixes: xoxb=bot token, xoxp=user token, xoxa=app-level token (legacy), xoxr=refresh token, xoxs=workspace token (legacy), xapp=Socket Mode app token. Webhook URLs are for incoming webhooks only and cannot read messages.
Have a pattern that belongs in the vault?
Submit it for review — community-verified patterns get credited to your GitHub handle. Free submissions join the queue. Priority review available for $15.
Submit a Pattern