import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "^(?:(?:(jdbc)\\:{1})?(?:(\\w+):/{2})?(?:([^@\\/?!\\\"':#\\s]+(?::\\w+)?)@)?)?(?:([^@\\/?!\\\"':#\\s]+(?::\\d+)?)(?=(?:$)|(?:/)))?(?:/([^@?!\\\"':#\\s]*)(?=(?:$)|(?:\\?)))?(?:[?]([^#?!\\s]+))?\\S*$";
final String string = "postgresql://\n"
+ "postgresql://localhost\n"
+ "postgresql://localhost:5432\n"
+ "postgresql://localhost/mydb\n"
+ "postgresql://user@localhost\n"
+ "postgresql://user:secret@localhost\n"
+ "postgresql://user:secret@localhost:1533\n"
+ "postgresql://user:secret@localhost:1533/\n"
+ "postgresql://user:secret@localhost:1533/db?\n"
+ "postgresql://user::se:::cret@localhost::port\n"
+ "postgresql://user::secret@local::host::po::::rt/dd::b:::b\n"
+ "postgresql://other@localhost/otherdb?connect_timeout=10&application_name=myapp\n"
+ "postgresql://localhost/mydb?user=other&password=secret\n"
+ "jdbc:postgresql://localhost\n"
+ "jdbc:postgresql://other@localhost/otherdb?connect_timeout=10&application_name=myapp\n"
+ "jdbc:postgresql://other@localhost:5432/otherdb?connect_timeout=10&application_name=myapp\n"
+ "jdbc:postgresql://other:pass@localhost:5432/otherdb?connect_timeout=10&application_name=myapp\n"
+ "jdbc:postgresql://other:pass@localhost:5432/other/db?connect_timeout=10&application_name=myapp\n"
+ "jdbc:postgresql://other:pass@localhost:5432/other/db??connect_timeout=10&application_name=myapp\n"
+ "jdbc:postgresql://other::pass@localhost::5432/oth:erdb?connect_timeout=10&application_name=myapp\n"
+ "\"\"''\":::abc;\n"
+ "postgresql://127.0.0.1::5432\n"
+ "127.0.0.1::5432\n"
+ "localhost\n"
+ "user@localhost\n"
+ "user@localhost:1533\n"
+ "user@localhost:1533/db\n"
+ "user@localhost:1533/db?\n"
+ "user:secret@localhost\n"
+ "user:secret@localhost/db\n"
+ "user:secret@localhost/db?\n"
+ "user:secret@local:host/otherdb\n"
+ "user:secret@local:1533/othe1//db\n"
+ "user:secret@localhost/otherdb?connect_timeout=10&application_name=myapp\n"
+ "user:secret@localhost/other/db?connect_timeout=10&application_name=myapp\n"
+ "user:secret@localhost/other/db??connect_timeout=10&application_name=myapp\n"
+ "us\"\"er:se::cret@loca''lhost\n"
+ "user:secret@local:1533/otherdb\n"
+ "jdbc:postgresql://user:secret@localhost:1533/otherdb\n"
+ "jdbc:postgresql://user:secret@@localhost:1533/otherdb\n"
+ "jdbc:postgresql://user:secret@localhost:1533/otherdb?\n"
+ "jdbc:postgresql://:secret@localhost:1533/otherdb?\n"
+ "jdbc:postgresql://user:secret@:1533/otherdb?\n"
+ "jdbc:postgresql://user:secret@localhost:1533/@otherdb?\n"
+ "jdbc:postgresql://user:secret@localhost/otherdb?connect_timeout=10&application_name=myapp\n"
+ "jdbc:postgresql://user:secret@localhost:1533/otherdb?connect_timeout=10&application_name=myapp\n"
+ "jd''bc:post\"\"gresql://us..er:sec,,ret@localh++ost/ot+-herdb?connect_timeout=10&application_name=myapp";
final String subst = "$1 \\n$2 \\n$3 \\n$4 \\n$5 \\n---\\n";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);
// The substituted value will be contained in the result variable
final String result = matcher.replaceAll(subst);
System.out.println("Substitution result: " + result);
}
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Java, please visit: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html