Splitting a string

Dylan Moreland dylan.moreland at gmail.com
Tue Feb 14 03:03:42 EST 2006


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




More information about the Python-list mailing list