-2

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"

1 Answers1

0

I suggest the following regex:

jdbc:sqlserver:\/\/(\w+).(.+):(\w+)

Explanation:

  1. \/ specifies the /
  2. \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
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110