REGEXVAULTv2.0
Security/Injection Patterns
Verified Safe

SQL Injection Pattern (Basic Detection) Regex for Java

/(?:;|--|#|/\*|\*/|\bUNION\b|\bSELECT\b|\bINSERT\b|\bUPDATE\b|\bDELETE\b|\bDROP\b|\bCREATE\b|\bALTER\b|\bEXEC\b|\bEXECUTE\b|\bxp_|\bsp_|'(?:--|OR|AND)\s+|OR\s+1\s*=\s*1|AND\s+1\s*=\s*1|'\s*OR\s*'|"\s*OR\s*"|CHAR\s*\(|CONCAT\s*\(|SLEEP\s*\(|WAITFOR\s+DELAY)/i

What this pattern does

This page provides a comprehensive, battle-tested regular expression for matching sql injection pattern (basic detection), 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

Java
// SQL Injection Pattern (Basic Detection)
// ReDoS-safe | RegexVault — Security > Injection Patterns

import java.util.regex.Pattern;

public class SqlInjectionPatternBasicDetectionValidator {
    private static final Pattern PATTERN =
        Pattern.compile("(?:;|--|#|/\\*|\\*/|\\bUNION\\b|\\bSELECT\\b|\\bINSERT\\b|\\bUPDATE\\b|\\bDELETE\\b|\\bDROP\\b|\\bCREATE\\b|\\bALTER\\b|\\bEXEC\\b|\\bEXECUTE\\b|\\bxp_|\\bsp_|\'(?:--|OR|AND)\\s+|OR\\s+1\\s*=\\s*1|AND\\s+1\\s*=\\s*1|\'\\s*OR\\s*\'|\"\\s*OR\\s*\"|CHAR\\s*\\(|CONCAT\\s*\\(|SLEEP\\s*\\(|WAITFOR\\s+DELAY)");

    public static boolean validate(String input) {
        return PATTERN.matcher(input).matches();
    }

    // Example
    public static void main(String[] args) {
        System.out.println(validate("' OR 1=1--")); // true
    }
}

Test Cases

Matches (Valid)
Rejects (Invalid)
' OR 1=1--hello world
1; DROP TABLE users;--John's Coffee Shop
UNION SELECT password FROM users
' OR '1'='1

When to use this pattern

This pattern is drawn from the Security > Injection Patterns 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

Regex-based SQL injection detection is insufficient as a primary defense — it cannot keep up with encoding variations (Unicode, URL encoding, hex encoding). The only reliable defense is parameterized queries (prepared statements) with an ORM.

Technical Notes

Detection pattern for WAF rules and input validation logging. HIGH false positive rate — legitimate inputs can match (e.g., user names with apostrophes, technical documentation). Use for alerting/logging rather than hard blocking. Always use parameterized queries as the real defense.

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