469,271 Members | 829 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

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 1659
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by qwweeeit | last post: by
5 posts views Thread by fatted | last post: by
2 posts views Thread by Trint Smith | last post: by
20 posts views Thread by Opettaja | last post: by
2 posts views Thread by CharChabil | last post: by
6 posts views Thread by HMS Surprise | last post: by
2 posts views Thread by shadow_ | last post: by
4 posts views Thread by yogi_bear_79 | last post: by
37 posts views Thread by xyz | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.