Over a million developers have joined DZone.

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

·
http://mail.python.org/pipermail/python-list/2002-October/125367.html

// I found this on a python mailling list site

Ken wrote:
> "Padraig Brady"  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.
Topics:

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}