magna_nz
magna_nz

Reputation: 1273

Splitting string into sentences using regex

I'm trying to split a paragraph into sentences. At the moment I'm splitting by . which works fine but I can't seem to get it to split correctly when there's either . or ? or !

So far my code is:

String[] sentences = everything.split("(?<=[a-z])\\.\\s+");

Thanks

Upvotes: 0

Views: 789

Answers (2)

Bohemian
Bohemian

Reputation: 425003

Use a character class, and you don't need the look behind - use a word boundary instead:

String[] sentences = everything.split("\\b[.!?]\\s+");

"[.!?]" means "either ., ! or ?". The word boundary \b requires that a word character precede the end of sentence char.

Upvotes: 0

user4910279
user4910279

Reputation:

If you don't want to remove ., !, ? from the results.

    String[] sentences = everything.split("(?<=[a-z][!?.])\\s+"); 

Upvotes: 2

Related Questions