REGEXVAULTv2.0
Security/Injection Patterns
Verified Safe

SQL Injection Pattern (Basic Detection) Regex for Python

/(?:;|--|#|/\*|\*/|\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 Python. 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 Python project — whether you're validating in a Django view, a FastAPI endpoint, or a standalone data processing script.

Python Implementation

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

import re

sql_injection_pattern_basic_detection_pattern = re.compile(r'(?:;|--|#|/\*|\*/|\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)')

def validate_sql_injection_pattern_basic_detection(value: str) -> bool:
    return bool(sql_injection_pattern_basic_detection_pattern.fullmatch(value))

# Example
print(validate_sql_injection_pattern_basic_detection("' 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 Python developers because particularly important in Python web servers where CPU-bound regex operations can stall concurrent request handling. 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