In this string: jdbc:sqlserver://testserver.apple.com\A4534:54623
I want to match "testserver" "apple.com//A4534" and "54623"
Regex:
jdbc:sqlserver:(.+)\.(.+):(.+)
But I get, "testserver.apple" "com//A4534" and "54623"
In this string: jdbc:sqlserver://testserver.apple.com\A4534:54623
I want to match "testserver" "apple.com//A4534" and "54623"
Regex:
jdbc:sqlserver:(.+)\.(.+):(.+)
But I get, "testserver.apple" "com//A4534" and "54623"
I suggest the following regex:
jdbc:sqlserver:\/\/(\w+).(.+):(\w+)
Explanation:
\/
specifies the /
\w+
specifies word character i.e. [A-Za-z0-9_]+
Check this for a demo.
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
String str = " jdbc:sqlserver://testserver.apple.com\\A4534:54623";
Pattern pattern = Pattern.compile("jdbc:sqlserver:\\/\\/(\\w+).(.+):(\\w+)");
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
System.out.println(matcher.group(1) + "\n" + matcher.group(2) + "\n" + matcher.group(3));
}
}
}
Output:
testserver
apple.com\A4534
54623