Generic Connection String (URL Form)
Parse generic URL-form connection strings: `protocol://[user[:pass]@]host[:port][/database]`.
Try it in RegexPro →Available in
JavaScript / ECMAScript code
const re = new RegExp("([a-zA-Z][\\w+\\-.]*):\\/\\/(?:([^:@\\s]+)(?::([^@\\s]*))?@)?([^:\\/\\s]+)(?::(\\d+))?(?:\\/([^?\\s]*))?", "g");
const input = "postgres://admin:s3cret@db.example.com:5432/main";
const matches = [...input.matchAll(re)];
console.log(matches.map(m => m[0]));Uses `String.prototype.matchAll` for global iteration (Node 12+ / all modern browsers).
Python (re) code
import re
pattern = re.compile(r"([a-zA-Z][\w+\-.]*):\/\/(?:([^:@\s]+)(?::([^@\s]*))?@)?([^:\/\s]+)(?::(\d+))?(?:\/([^?\s]*))?")
input_text = "postgres://admin:s3cret@db.example.com:5432/main"
for m in pattern.finditer(input_text):
print(m.group(0))Stdlib `re` module — no third-party dependency. Works on Python 3.6+.
Go (RE2) code
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`([a-zA-Z][\w+\-.]*):\/\/(?:([^:@\s]+)(?::([^@\s]*))?@)?([^:\/\s]+)(?::(\d+))?(?:\/([^?\s]*))?`)
input := `postgres://admin:s3cret@db.example.com:5432/main`
for _, match := range re.FindAllString(input, -1) {
fmt.Println(match)
}
}Uses `regexp.MustCompile` (panics on bad patterns at startup) and `FindAllString` for all matches.
Pattern
([a-zA-Z][\w+\-.]*):\/\/(?:([^:@\s]+)(?::([^@\s]*))?@)?([^:\/\s]+)(?::(\d+))?(?:\/([^?\s]*))? (flags: g)Raw source: ([a-zA-Z][\w+\-.]*):\/\/(?:([^:@\s]+)(?::([^@\s]*))?@)?([^:\/\s]+)(?::(\d+))?(?:\/([^?\s]*))?
How it works
Examples
Input
postgres://admin:s3cret@db.example.com:5432/mainMatches
postgres://admin:s3cret@db.example.com:5432/main
Input
kafka://broker.internal:9092Matches
kafka://broker.internal:9092
Input
no connection hereNo match
—Common use cases
- •Config file linting and secret detection
- •Migration scripts that re-write connection details
- •Observability — surfacing target hosts from logs
- •Feature gating by environment (parse host suffix)
Related patterns
JDBC Connection URL
NetworkingMatch JDBC connection URLs in the standard `jdbc:driver://host[:port][/database]` form.
Redis Connection URL
NetworkingMatch Redis connection URLs in both `redis://` and `rediss://` (TLS) forms, capturing user, password, host, port, and DB index.
FTP URL
NetworkingMatch FTP and FTPS URLs, capturing optional credentials, host, port, and path.
TCP/UDP Port Number
NetworkingMatch port numbers (1–65535) following a colon, as you'd find in host:port strings.