need help of RE

John Machin sjmachin at lexicon.net
Sun May 29 04:29:39 EDT 2005


cheng wrote:
> hi all
> a string like
> 
> "(word1 & (Word2|woRd3))"
> 
> how can i use the re to split it to
> 
> ['word1', 'word2', 'word3']
> 

OK, so you know about the re module.

Look in the manual: there's a module-level function called
"split", with an example similar to yours. Did you try that?
Let's do it now:

 >>> import re
 >>> text = "(word1 & (Word2|woRd3))".lower()
# you seem to want downshifting ...
 >>> re.split(r"\W+", text)
['', 'word1', 'word2', 'word3', '']
 >>>

Hmmm ... near, but not exactly what you want. We need to throw away 
those empty strings, which will appear if you have non-word characters 
at the ends of your text.

Two ways of doing that:

 >>> filter(None, re.split(r"\W+", text))
['word1', 'word2', 'word3']

or

 >>> [x for x in re.split(r"\W+", text) if x]
['word1', 'word2', 'word3']

Forget about "filter". Read about "list comprehensions" and "generator 
expressions" -- they are more general and powerful.

Cheers,
John




More information about the Python-list mailing list