v # invalid expression ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Anthony Liu

    v # invalid expression ?

    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\co des\regextest.p y", line 27, in
    -toplevel-
    ultimate =
    re.split(',|。|、 |!|:|,|?|“|”|)| (|;',
    ch[0])
    File "c:\python23\li b\sre.py", line 156, in split
    return _compile(patter n, 0).split(string ,
    maxsplit)
    File "c:\python23\li b\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?



    _______________ _______________ ____
    Do you Yahoo!?
    Yahoo! Search - Find what you’re looking for faster
    The search engine that helps you find exactly what you're looking for. Find the most relevant information, video, images, and answers from all across the Web.


  • Eric Brunel

    #2
    Re: v # invalid expression ?

    Anthony Liu wrote:[color=blue]
    > 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\co des\regextest.p y", line 27, in
    > -toplevel-
    > ultimate =
    > re.split(',|。|、 |!|:|,|?|“|”|)| (|;',
    > ch[0])
    > File "c:\python23\li b\sre.py", line 156, in split
    > return _compile(patter n, 0).split(string ,
    > maxsplit)
    > File "c:\python23\li b\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?[/color]

    Because ?, (, and ) are special characters in regular expressions. You must
    escape them with a backslash:
    [color=blue][color=green][color=darkred]
    >>> re.split(',|。|、 |!|:|,|\\?|\\)| \\(|;', 'foo,bar(spam)' )[/color][/color][/color]
    ['foo', 'bar', 'spam', '']

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

    Comment

    Working...