hi all
a string like
"(word1 & (Word2|woRd3))"
how can i use the re to split it to
['word1', 'word2', 'word3'] 11 1469
On May 29, 2005, at 12:39 AM, cheng wrote: 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/
---
[This E-mail scanned for viruses by Declude Virus]
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
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']
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']
On May 29, 2005, at 12:57 AM, cheng wrote: 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/
---
[This E-mail scanned for viruses by Declude Virus]
cheng wrote: 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
On Sun, 29 May 2005 at 07:39 GMT, cheng wrote: 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()
--
Chris F.A. Johnson <http://cfaj.freeshell.org>
================================================== ================
Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress
<http://www.torfree.net/~chris/books/cfaj/ssr.html>
thx for help..i got it now :)
"cheng" <ma******@spymac.com> schrieb im Newsbeitrag
news:11**********************@z14g2000cwz.googlegr oups.com...
| 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]
--
Vincent Wehren
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
John Machin wrote: >>> 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Sofia |
last post by:
My name is Sofia and I have for many years been running a personals site,
together with my partner, on a non-profit basis. The site is currently not
running due to us emigrating, but during its...
|
by: Robert Maas, see http://tinyurl.com/uh3t |
last post by:
System login message says PHP is available, so I tried this:
http://www.rawbw.com/~rem/HelloPlus/h.php
It doesn't work at all. Browser just shows the source.
What am I doing wrong?
|
by: Gregory Nans |
last post by:
hello,
i need some help to 'tree-ify' a string...
for example i have strings such as :
s = """A(here 's , B(A ) silly test) C(to show D(what kind) of stuff i
need))"""
and i need to...
|
by: Mike Kamermans |
last post by:
I hope someone can help me, because what I'm going through at the moment
trying to edit XML documents is enough to make me want to never edit XML
again.
I'm looking for an XML editor that has a...
|
by: JustSomeGuy |
last post by:
I need to write an new class derived from the list class.
This class stores data in the list to the disk if an object
that is added to the list is over 1K in size.
What methods of the std stl...
|
by: Bob.Henkel |
last post by:
I write this to tell you why we won't use postgresql even though we wish we
could at a large company. Don't get me wrong I love postgresql in many
ways and for many reasons , but fact is fact. If...
|
by: Michael R. Pierotti |
last post by:
Dim reg As New Regex("^\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}$")
Dim m As Match = reg.Match(txtIPAddress.Text)
If m.Success Then
'No need to do anything here
Else
MessageBox.Show("You need to enter a...
|
by: skumar434 |
last post by:
i need to store the data from a data base in to structure
.............the problem is like this ....suppose there is a data base
which stores the sequence no and item type etc ...but i need only...
|
by: Alan Mailer |
last post by:
A project I'm working on is going to use VB6 as a front end. The back
end is going to be pre-existing MS Access 2002 database tables which
already have records in them *but do not have any...
|
by: U S Contractors Offering Service A Non-profit |
last post by:
Brilliant technology helping those most in need Inbox
Reply
U S Contractors Offering Service A Non-profit
show details
10:37 pm (1 hour ago)
Brilliant technology helping those most in need ...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |