Redis Connection URL in PY
Match Redis connection URLs in both `redis://` and `rediss://` (TLS) forms, capturing user, password, host, port, and DB index.
Try it in the PY tester →Pattern
regexPY
rediss?:\/\/(?:([^:@\s]+)(?::([^@\s]*))?@)?([^:\/\s]+)(?::(\d+))?(?:\/(\d+))? (flags: g)Python (re) code
pyPython
import re
pattern = re.compile(r"rediss?:\/\/(?:([^:@\s]+)(?::([^@\s]*))?@)?([^:\/\s]+)(?::(\d+))?(?:\/(\d+))?")
input_text = "REDIS_URL=redis://default:pass@redis.upstash.io:6379/0"
for m in pattern.finditer(input_text):
print(m.group(0))Stdlib `re` module — no third-party dependency. Works on Python 3.6+.
How the pattern works
rediss?:\/\/ matches either scheme. The optional auth group captures user and optional password. The host group captures the hostname. (?::(\d+))? optionally captures the port. (?:\/(\d+))? optionally captures the database index (Redis databases are numeric).
Examples
Input
REDIS_URL=redis://default:pass@redis.upstash.io:6379/0Matches
redis://default:pass@redis.upstash.io:6379/0
Input
rediss://cluster.example.com:6380Matches
rediss://cluster.example.com:6380
Input
no redis urlNo match
—