-1

我有这个 Ruby 代码:

line = "this is u.s.a. please come home. where are you"
res = line.split(/(.*?[.!?](?:["')\]}]|(?:<.*>))*[\s])/)

它返回["", "this is u.s.a. ", "", "please come home. ", "where are you"]

但是当我在 Java 中尝试相同的正则表达式和相同的代码时:

String line = "this is u.s.a. please come home. where are you";
String[] res = line.split("^(.*?[.!?]((?:([\"')\\]}])|(?:<.*>))*)[\\s])");

它只返回两个元素:{ "" , "please come home. where are you"}.

我的 Java 正则表达式有什么问题?

4

1 回答 1

2
String line = "this is u.s.a. please come home. where are you";
String[] res = line.split("(?<=[.!?])\\s+");

"this is u.s.a." "please come home." "where are you"

与您的 Ruby 版本不一样,但无论如何,那个版本对我来说并不好听。

于 2013-09-12T12:27:05.710 回答