473,322 Members | 1,562 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Splitting a string

Dear Python users,

I'd like to split a string where 'and', 'or', 'and not' occurs.

Example string:
s = 'Smith, R. OR White OR Blue, T. AND Black AND Red AND NOT Green'

I need to split s in order to get this list:
['Smith, R.', 'White', 'Blue, T.', 'Back', 'Red', 'Green']

Any idea, how I can split a string where 'and', 'or', 'and not' occurs?
Thank you very much in advance,
Nico
Feb 14 '06 #1
5 1860
Take a look at:

http://docs.python.org/lib/node115.html#l2h-878

So I would try something like:

pat = re.compile(r" (?:AND|OR|AND NOT) ")
pat.split(string)

Compile the regular expression with re.IGNORECASE if you like.

Nico Grubert wrote:
Dear Python users,

I'd like to split a string where 'and', 'or', 'and not' occurs.

Example string:
s = 'Smith, R. OR White OR Blue, T. AND Black AND Red AND NOT Green'

I need to split s in order to get this list:
['Smith, R.', 'White', 'Blue, T.', 'Back', 'Red', 'Green']

Any idea, how I can split a string where 'and', 'or', 'and not' occurs?
Thank you very much in advance,
Nico


Feb 14 '06 #2
Dylan Moreland wrote:
So I would try something like:

pat = re.compile(r" (?:AND|OR|AND NOT) ")
pat.split(string)


footnote: this yields:

['Smith, R.', 'White', 'Blue, T.', 'Black', 'Red', 'NOT Green']

(the | operator picks the first (leftmost) alternative that results in an
overall match.)

</F>

Feb 14 '06 #3
Woops! Thanks for the correction. I was assuming greediness for some
reason.

Fredrik Lundh wrote:
Dylan Moreland wrote:
So I would try something like:

pat = re.compile(r" (?:AND|OR|AND NOT) ")
pat.split(string)


footnote: this yields:

['Smith, R.', 'White', 'Blue, T.', 'Black', 'Red', 'NOT Green']

(the | operator picks the first (leftmost) alternative that results in an
overall match.)

</F>


Feb 14 '06 #4
Nico Grubert <ni*********@gmail.com> writes:
I'd like to split a string where 'and', 'or', 'and not' occurs.


Other people have suggested how to do this splitting. But don't you
really want a parser?
Feb 14 '06 #5
Nico Grubert wrote:
I'd like to split a string where 'and', 'or', 'and not' occurs.
Example string:
s = 'Smith, R. OR White OR Blue, T. AND Black AND Red AND NOT Green'


Here is a solution without using the re module:
s.replace(' AND NOT ', ' OR ').replace(' AND ', ' OR ').split(' OR ')

-- Christoph
Feb 14 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: qwweeeit | last post by:
Splitting with RE has (for me!) misterious behaviour! I want to get the words from this string: s= 'This+(that)= a.string!!!' in a list like that considering "a.string" as a word. Python...
5
by: fatted | last post by:
I'm trying to write a function which splits a string (possibly multiple times) on a particular character and returns the strings which has been split. What I have below is kind of (oh dear!)...
4
by: JeffM | last post by:
Quick C# question: I have comma delimited values in a string array that I want to pass to seperate variables. Any tips on splitting the array? Thanks in advance! JM
2
by: Trint Smith | last post by:
Ok, My program has been formating .txt files for input into sql server and ran into a problem...the .txt is an export from an accounting package and is only supposed to contain comas (,) between...
20
by: Opettaja | last post by:
I am new to c# and I am currently trying to make a program to retrieve Battlefield 2 game stats from the gamespy servers. I have got it so I can retrieve the data but I do not know how to cut up...
2
by: CharChabil | last post by:
Using Vb.net 2005, I want to read each part in this string in an array (splitting the string) ----------- A1/EXT "BK82 LB73 21233" 105 061018 1804 ----------- That Code that i used is as follow:...
6
by: HMS Surprise | last post by:
The string s below has single and double qoutes in it. For testing I surrounded it with triple single quotes. I want to split off the portion before the first \, but my split that works with...
2
by: shadow_ | last post by:
Hi i m new at C and trying to write a parser and a string class. Basicly program will read data from file and splits it into lines then lines to words. i used strtok function for splitting data to...
4
by: yogi_bear_79 | last post by:
I have a simple string (i.e. February 27, 2008) that I need to split into three parts. The month, day, and year. Splitting into a string array would work, and I could convert day and years to...
37
by: xyz | last post by:
I have a string 16:23:18.659343 131.188.37.230.22 131.188.37.59.1398 tcp 168 for example lets say for the above string 16:23:18.659343 -- time 131.188.37.230 -- srcaddress 22 ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.