Split a sentence by punctuations using Python

Peter Otten __peter__ at web.de
Fri Mar 5 18:07:30 EST 2004


chad wrote:

> I want to split sentences by using punctuations, numeric numbers as
> the delimiters.
> 
> For example, suppose I have a text that contains sentences like so:
> 
> "To help you get there a bit faster, I will be driving at 120 miles an
> hour (I am just kidding). Is that OK?"
> 
> Now, I want to get the following segments from this text:
> 
> To help you get there a bit faster
> I will be driving at
> miles an hour
> I am just kidding
> Is that OK
> 
> You know, string.split does not work for this objective.  So how can i
> achieve this?  Thanks.

>>> s
'To help you get there a bit faster, I will be driving at 120 miles an hour
(I am just kidding). Is that OK?'
>>> r = re.compile("[,.?()\\d]+ *")
>>> print "\n".join(r.split(s))
To help you get there a bit faster
I will be driving at
miles an hour
I am just kidding
Is that OK

>>>

Is this OK? Note the trailing empty line. If this is not desired, just
remove the last list item if empty.

Peter



More information about the Python-list mailing list