Splitting a string
Peter Otten
__peter__ at web.de
Tue Feb 14 03:25:38 EST 2006
Fredrik Lundh wrote:
>>>> re.split("(?i)\s*(?:and not|and|or)\s*", s)
> ['Smith, R.', 'White', 'Blue, T.', 'Black', 'Red', 'Green']
This fails for people with nasty names:
>>> s = "James White or Andy Grove and Jack Orr and not James Grand"
>>> re.split("(?i)\s*(?:and not|and|or)\s*", s)
['James White', '', 'y Grove', 'Jack', 'r', 'James Gr', '']
Here is an alternative:
>>> [s.strip() for s in re.split(r"(?i)\b(?:and|or|not)\b", s) if s.strip()]
['James White', 'Andy Grove', 'Jack Orr', 'James Grand']
Peter
More information about the Python-list
mailing list