所以,我有这样的文字
String s = "The if-then-else statement provides a secondary path of execution when an "if" clause evaluates to false. You could use an if-then-else statement in the applyBrakes method to take some action if the brakes are applied when the bicycle is not in motion. In this case, the action is to simply print an error message stating that the bicycle has already stopped."
我需要在Sentences中拆分这个字符串,但在句子结尾处保存标点符号,所以我不能使用这样的东西:
s.split("[\\.|!|\\?|:] ");
因为如果我使用它,我会收到:
The if-then statement is the most basic of all the control flow statements
It tells your program to execute a certain section of code only if a particular test evaluates to true
For example, the Bicycle class could allow the brakes to decrease the bicycle's speed only if the bicycle is already in motion
One possible implementation of the applyBrakes method could be as follows:
我最后丢失了标点符号,我该怎么办呢?
答案 0 :(得分:3)
首先,您的正则表达式[\\.|!|\\?|:]
代表.
或|
或!
或|
或?
或|
或:
,因为您使用了character class [...]
。您可能希望使用(\\.|!|\\?|:)
或更好[.!?:]
(我不确定您为何需要:
,但这是您的选择)。
接下来的事情是,如果你想分割空间并确保.
或!
或?
或:
字符在它之前但不消耗此字符字符使用look-behind机制,如
split("(?<=[.!?:])\\s")
但最好的方法是使用适当的工具来分割句子,BreakIterator
。您可以在此问题中找到使用示例:Split string into sentences based on periods
答案 1 :(得分:1)
您可以简单地在模式中使用输入结尾替换空格:
// | your original punctuation class,
// | no need for "|" between items
// | (that would include "|"
// | as a delimiter)
// | nor escapes, now that I think of it
// | | look ahead for:
// | | either whitespace
// | | | or end
System.out.println(Arrays.toString(s.split("[.!?:](?=\\s|$)")));
这将包括最后一个块,并打印(添加换行符以澄清):
[The if-then-else statement provides a secondary path of execution when an "if" clause evaluates to false,
You could use an if-then-else statement in the applyBrakes method to take some action if the brakes are applied when the bicycle is not in motion,
In this case, the action is to simply print an error message stating that the bicycle has already stopped]