Over a million developers have joined DZone.

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

DZone's Guide to

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

· ·
Free Resource

// 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 


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.")

 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



Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}