Splitting a string

N

Nico Grubert

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
 
F

Fredrik Lundh

Dylan said:
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>
 
D

Dylan Moreland

Woops! Thanks for the correction. I was assuming greediness for some
reason.

Fredrik said:
Dylan said:
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>
 
P

Paul Rubin

Nico Grubert said:
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?
 
C

Christoph Zwerschke

Nico said:
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
 

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

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top