Networkingflags: g
MongoDB Connection URI
Match MongoDB connection URIs in both standard `mongodb://` and SRV `mongodb+srv://` formats.
Try it in RegexPro →Available in
Pattern
regexengine-agnostic
mongodb(?:\+srv)?:\/\/(?:[^:@\s]+(?::[^@\s]*)?@)?[^\/?\s]+(?:\/[^?\s]*)?(?:\?\S*)? (flags: g)Raw source: mongodb(?:\+srv)?:\/\/(?:[^:@\s]+(?::[^@\s]*)?@)?[^\/?\s]+(?:\/[^?\s]*)?(?:\?\S*)?
How it works
mongodb(?:\+srv)?:\/\/ matches either scheme. (?:[^:@\s]+(?::[^@\s]*)?@)? optionally matches user[:password]@. [^\/?\s]+ matches the host (potentially comma-separated for replica sets, though this regex captures only up to the first / or ?). (?:\/[^?\s]*)? optionally matches the database path. (?:\?\S*)? optionally matches query parameters.
Examples
Input
Conn: mongodb://user:pass@cluster0.mongodb.net:27017/mydbMatches
mongodb://user:pass@cluster0.mongodb.net:27017/mydb
Input
mongodb+srv://admin@prod.example.com/main?retryWrites=trueMatches
mongodb+srv://admin@prod.example.com/main?retryWrites=true
Input
no connection hereNo match
—Common use cases
- •Linting config files for connection strings
- •Detecting committed credentials in PRs
- •Migration scripts that need to parse URIs
- •Observability — extracting cluster names from logs