JDBC Connection URL in GO
Match JDBC connection URLs in the standard `jdbc:driver://host[:port][/database]` form.
Try it in the GO tester →Pattern
regexGO
jdbc:[a-z0-9]+:\/\/[^\/?\s]+(?:\/[\w\-]+)?(?:\?\S*)? (flags: g)Go (RE2) code
goGo
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`jdbc:[a-z0-9]+:\/\/[^\/?\s]+(?:\/[\w\-]+)?(?:\?\S*)?`)
input := `url=jdbc:postgresql://db.internal: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.
How the pattern works
jdbc: matches the literal scheme prefix. [a-z0-9]+ captures the driver name (mysql, postgresql, oracle, sqlserver, h2, etc.). :\/\/ matches the separator. [^\/?\s]+ captures the host[:port]. (?:\/[\w\-]+)? optionally matches a database name. (?:\?\S*)? optionally matches query parameters.
Examples
Input
url=jdbc:postgresql://db.internal:5432/mainMatches
jdbc:postgresql://db.internal:5432/main
Input
jdbc:mysql://localhost:3306/test?useSSL=falseMatches
jdbc:mysql://localhost:3306/test?useSSL=false
Input
no jdbc url hereNo match
—