v # invalid expression ?

Eric Brunel eric_brunel at despammed.com
Thu Mar 11 12:38:26 CET 2004


Anthony Liu wrote:
> I am trying to split a string like so:
> 
> re.split(',|。|、|!|:|,|?|“|”|)|(|;',
> my_string)
> 
> Python complains like so:
> 
> Traceback (most recent call last):
>   File "C:\Python23\codes\regextest.py", line 27, in
> -toplevel-
>     ultimate =
> re.split(',|。|、|!|:|,|?|“|”|)|(|;',
> ch[0])
>   File "c:\python23\lib\sre.py", line 156, in split
>     return _compile(pattern, 0).split(string,
> maxsplit)
>   File "c:\python23\lib\sre.py", line 229, in _compile
>     raise error, v # invalid expression
> error: nothing to repeat
> 
> Does this mean that the regular expression I pass to
> the split function is not valid?
> 
> That expression is just like ',|)|#|!', that is, an
> or-ed list of punctuations. Then why invalid?

Because ?, (, and ) are special characters in regular expressions. You must 
escape them with a backslash:

 >>> re.split(',|。|、|!|:|,|\\?|\\)|\\(|;', 'foo,bar(spam)')
['foo', 'bar', 'spam', '']

HTH
-- 
- Eric Brunel <eric (underscore) brunel (at) despammed (dot) com> -
PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com




More information about the Python-list mailing list