Easy way to break up a sentence?

Ken ken at hotmail.com
Wed Oct 2 16:19:56 CEST 2002


"Padraig Brady" <Padraig at Linux.ie> wrote in message
news:3D9AFA69.2020804 at Linux.ie...
> Ken wrote:
> > Hi all, I am trying to do a simple word search engine. Is there an easy
way
> > to break up a sentence into individual words so that I can use it to
compare
> > without traversing through every character?
> >
> > Eg, something like this:
> > from: "This is an example"
> > to: ["This", "is", "an", "example"]
>
> You can use "".split() but that will not
> deal with punctuation. For that you will
> need re.
>
> import re
> re.split('\W+', "This is an, example.")
>
> This however will create an empty list item
> for the last '.'
>
> You can get around this by using the converse:
> re.findall('\w+', "This is an, example.")
>
> Pádraig.


Does string.rstrip() get rid of the commas, fullstops etc.?

Also, can you explain what the parameters "re.findall('\w+', "This is an,
example.")" mean?

Thanks





More information about the Python-list mailing list