Java由破折号分割

Azz*_*ziz 3 java regex split hyphen

我试图用连字符和字符分割字符串,但不确定如何使用正则表达式拆分.字符串是这样的:

-u tom -p 12345 -h google.com

连字符和字符在位置上可以互换,并且可以显示多少个.我希望他们回到阵列中.这是我到目前为止:

Scanner reader = new Scanner(System.in);
String entireLine = reader.nextLine();
String[] array = entireLine.split("–", -1);
Run Code Online (Sandbox Code Playgroud)

我想要的结果是:

-u tom

-p 12345

-h google.com

谢谢.

Boh*_*ian 6

Try this:

String[] array = entireLine.split("(?<!^)(?=-)");
Run Code Online (Sandbox Code Playgroud)

The negative look behind will prevent splitting at the start of line.