need help of RE

C

cheng

hi all
a string like

"(word1 & (Word2|woRd3))"

how can i use the re to split it to

['word1', 'word2', 'word3']
 
E

Elliot Temple

hi all
a string like

"(word1 & (Word2|woRd3))"

how can i use the re to split it to

['word1', 'word2', 'word3']

Could you be more exact about what the string is like? Does it
literally contain the characters '&' and '|' ? If so, just split at
them.

-- Elliot Temple
http://www.curi.us/
 
C

cheng

im sorry, my engilsh is not vell well,

the string not only contain '&' and '|' and it can be anyting

i just want to split out the "whole word" inside the string
 
C

cheng

i try

query = query.lower()
print re.split(r'\W+',theString)

the reslut is :
['', 'word1', 'word2', 'word3', '']

how can i fix the statment to get

['word1', 'word2', 'word3']
 
C

cheng

i try

theString= theString.lower()
print re.split(r'\W+',theString)

the reslut is :
['', 'word1', 'word2', 'word3', '']

how can i fix the statment to get

['word1', 'word2', 'word3']
 
E

Elliot Temple

im sorry, my engilsh is not vell well,

the string not only contain '&' and '|' and it can be anyting

i just want to split out the "whole word" inside the string

If the string could be anything, how do you know where the words are?

If it's whitespace that separates words, try out str.split()

-- Elliot Temple
http://www.curi.us/
 
T

tiissa

cheng said:
im sorry, my engilsh is not vell well,

That's alright, you could have been french. ;)
the string not only contain '&' and '|' and it can be anyting

i just want to split out the "whole word" inside the string

Look at the example for split function of re module in the doc [1].

In short:
>>> import re
>>> s="(word1 & (Word2|woRd3))"
>>> re.split("\W+",s) ['', 'word1', 'Word2', 'woRd3', '']
>>> [w.lower() for w in re.split("\W+",s) if w != ''] ['word1', 'word2', 'word3']
>>>


[1]http://python.org/doc/lib/node114.html
 
C

Chris F.A. Johnson

hi all
a string like

"(word1 & (Word2|woRd3))"

how can i use the re to split it to

['word1', 'word2', 'word3']

This splits the string on one or more occurrences of any character
that is not alphanumeric:

import re
str = "(word1 & (Word2|woRd3))"
s = re.sub("[^a-zA-Z0-9]+"," ",str).split()
 
V

vincent wehren

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

import re
s = "(word1 & (Word2|woRd3)"
parts = re.split('\W+', s)
print [p for p in parts if p]
 
J

John Machin

cheng said:
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:
['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
 
S

Steven Bethard

John said:
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.

You can also avoid the empty strings at the end by using re.findall with
\w instead of re.split with \W:

py> import re
py> text = "(word1 & (Word2|woRd3))".lower()
py> re.findall(r"\w+", text)
['word1', 'word2', 'word3']

STeVe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
474,240
Messages
2,571,211
Members
47,845
Latest member
vojosay

Latest Threads

Top