By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,626 Members | 1,146 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,626 IT Pros & Developers. It's quick & easy.

Splitting a string

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.