Split strings to sentences and save punctuation mark at the end by regex java -
so, have text
string s = "the if-then-else statement provides secondary path of execution when "if" clause evaluates false. use if-then-else statement in applybrakes method take action if brakes applied when bicycle not in motion. in case, action print error message stating bicycle has stopped."
i need split string in sentences save punctuation mark @ end of sentence, cant use this:
s.split("[\\.|!|\\?|:] ");
because if use receive this:
the if-then statement basic of control flow statements tells program execute section of code if particular test evaluates true example, bicycle class allow brakes decrease bicycle's speed if bicycle in motion 1 possible implementation of applybrakes method follows:
and i'm loosing punctuation mark @ end, how can it?
first of regex [\\.|!|\\?|:]
represents .
or |
or !
or |
or ?
or |
or :
because used character class [...]
. wanted use (\\.|!|\\?|:)
or better [.!?:]
(i not sure why want :
here, choice).
next thing if want split on space , make sure .
or !
or ?
or :
character before not consume preceding character use look-behind mechanism,
split("(?<=[.!?:])\\s")
but best approach using proper tool splitting sentences, breakiterator
. can find example of usage in question: split string sentences based on periods
Comments
Post a Comment