DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Python String Breaking And Beginners Reg Ex All-in-One !

09.29.2006
| 8365 views |
  • submit to reddit
        http://mail.python.org/pipermail/python-list/2002-October/125367.html

// I found this on a python mailling list site

Ken wrote:
> "Padraig Brady" <Padraig@Linux.ie> wrote in message
> news:3D9AFA69.2020804@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.?

Don't get mixed up between strip and split.
The help for rstrip says "returns a string with trailing whitespace removed".
I.E. "123  " -> "123" however "123   ." doesn't change.

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

The \w+ is a regular expression that matches one or more
characters in the set [a-zA-Z0-9_]. I.E. it matches words.
Anything else (like spaces, punctuation) is not returned.

import re
mystring="This is an, example."
re.findall(r'\w+', mystring)     #all words
re.findall(r'\w*i\w*', mystring) #all words containing letter i
re.findall(r'\w{4,}', mystring)  #all words >= 4 letters

Pádraig.