POSIX File Permission Octal Regex for Python
/^([0-7]?)([0-7]{3})$/What this pattern does
This page provides a lightweight, single-purpose regular expression for matching posix file permission octal, ported and verified for Python. A rigorously tested regex reduces debugging time and protects your application from edge-case failures. 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
# POSIX File Permission Octal
# ReDoS-safe | RegexVault — Dev & Systems > File Paths
import re
posix_file_permission_octal_pattern = re.compile(r'^([0-7]?)([0-7]{3})$')
def validate_posix_file_permission_octal(value: str) -> bool:
return bool(posix_file_permission_octal_pattern.fullmatch(value))
# Example
print(validate_posix_file_permission_octal("755")) # TrueTest Cases
Matches (Valid) | Rejects (Invalid) |
|---|---|
755 | 888 |
644 | 99 |
777 | 76 |
000 | 8888 |
0755 | a644 |
4755 | 0855 |
2755 | — |
1777 | — |
When to use this pattern
This pattern is drawn from the Dev & Systems > File Paths 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
Setting 777 on sensitive files is a security risk. The sticky bit on directories (1777) prevents users from deleting each other's files even with directory write permission.
Technical Notes
Optional leading digit: 4=setuid, 2=setgid, 1=sticky. Three digits: owner/group/other (read=4, write=2, execute=1). 1777 = /tmp permissions (sticky). 4755 = setuid root (dangerous). Group 1 = special bits, group 2 = rwx permissions.
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