| | |
Detecting start of sentence.
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Dec 2008
Posts: 53
Reputation:
Solved Threads: 6
String.split() really can help you out, I think. If I understand rightly, the thing you're dissatisfied with is that the suggestion strips off the terminating punctuation from the strings. The expression's a bit more involved, but you can do something such as the following:
See the documentation of Pattern to understand how this works. Although the expression's a bit nasty, this really is more succinct than tying yourself in knots with indexOf() and the like.
I leave it as an exercise to the reader to amend the expression to correctly split sentences such as "I asked Dr. Smith to come." :-)
Java Syntax (Toggle Plain Text)
String[] sentences = str.split("(?<=[\\.!?]+)(?![\\.!?]+) *");
See the documentation of Pattern to understand how this works. Although the expression's a bit nasty, this really is more succinct than tying yourself in knots with indexOf() and the like.
I leave it as an exercise to the reader to amend the expression to correctly split sentences such as "I asked Dr. Smith to come." :-)
•
•
•
•
Thank you for your suggestion. Can you direct to me to a good source where I find out how to use this expression? I'd like to finish this and move on to another project involving a JSlider.
Again, thanks for taking the time!
http://java.sun.com/docs/books/tutor...sential/regex/
Last edited by stephen84s; Dec 13th, 2008 at 1:49 am.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
"How to ask questions the smart way ?"
"How to ask questions the smart way ?"
![]() |
Other Threads in the Java Forum
- Previous Thread: JOptionPane tutorial
- Next Thread: How do I concatenate this string
| Thread Tools | Search this Thread |
2dgraphics account android api apple applet application array arrays automation banking binary binarytree bluetooth chat chatprogramusingobjects class classes client code component data database derby design draw eclipse encryption error event exception fractal game givemetehcodez graphics gui html ide if_statement image inheritance input integer interface j2me java javadesktopapplications javaprojects jlabel jni jpanel jtextfield julia linux list loop map method methods midlethttpconnection mobile monitoring netbeans newbie nullpointerexception open-source oracle print printing problem program programming project property recursion reference ria scanner screen search server set size sms sort sourcelabs splash sql static stop string swing testautomation threads time tree ui unicode validation windows






