将字符串转换为字符串数组,保存单词 Java 的一部分的空格

Ore*_*huk -1 java split

如果字符之间的空格是单词的一部分,如何将字符串转换为字符串数组?

我拥有的:

String phoneNumbers = "987-123-4567 123 456 7890 (123) 456-7890";
Run Code Online (Sandbox Code Playgroud)

我想要它是什么:

String[] numbers = {"987-123-4567", "123 456 7890", "(123) 456-7890"};
Run Code Online (Sandbox Code Playgroud)

当我试图分裂时:

String[] array = phoneNumbers.split(" ");
Run Code Online (Sandbox Code Playgroud)

结果是:

[987-123-4567, 123, 456, 7890, (123), 456-7890]
Run Code Online (Sandbox Code Playgroud)

Add*_*ina 6

You may use a regex, then with a matcher you can find iteratively the matches in the string

String phoneNumbersString = "987-123-4567 123 456 7890 (123) 456-7890";
String regex = "(\\d{3}-\\d{3}-\\d{4})|(\\d{3}\\s\\d{3}\\s\\d{4})|(\\(\\d{3}\\)\\s\\d{3}-\\d{4})";

List<String> phoneNumbersList = new ArrayList<>();
Matcher m = Pattern.compile(regex).matcher(phoneNumbersString);
while (m.find()) {
    phoneNumbersList.add(m.group());
}
Run Code Online (Sandbox Code Playgroud)