how to match u'\uff00' - u'\uff0f' in re module?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • yichao.zhang

    #1

    how to match u'\uff00' - u'\uff0f' in re module?

    I'm trying to match the characters from u'\uff00' to u'\uff0f'.
    the code below and get a TypeError.
    p = re.compile(u'\u ff00'-u'\uff0f')
    Traceback (most recent call last):
    File "<interacti ve input>", line 1, in ?
    TypeError: unsupported operand type(s) for -: 'unicode' and 'unicode'


    so re module does NOT support this operation
    however, is there any alternative way to solve my problem?

    Any comments/suggestions much appreciated!

  • MacDonald

    #2
    Re: how to match u'\uff00' - u'\uff0f' in re module?


    yichao.zhang wrote:
    I'm trying to match the characters from u'\uff00' to u'\uff0f'.
    the code below and get a TypeError.
    p = re.compile(u'\u ff00'-u'\uff0f')
    Traceback (most recent call last):
    File "<interacti ve input>", line 1, in ?
    TypeError: unsupported operand type(s) for -: 'unicode' and 'unicode'
    >
    >
    so re module does NOT support this operation
    however, is there any alternative way to solve my problem?
    >
    Any comments/suggestions much appreciated!
    re DOES work with unicode, you're just subtracting two unicode stings
    as the agrument, which is not defined. Try this:

    p = re.compile(u'[\uff00-\uff0f]')

    Comment

    • John Machin

      #3
      Re: how to match u'\uff00' - u'\uff0f' in re module?

      On 11/07/2006 12:32 PM, yichao.zhang wrote:
      I'm trying to match the characters from u'\uff00' to u'\uff0f'.
      the code below and get a TypeError.
      p = re.compile(u'\u ff00'-u'\uff0f')
      That is not a valid regex. It is an attempt to subtract one unicode char
      from another, but this is (correctly) not supported, as the error
      message says. re.compile expects a string (8-bit or Unicode).

      If you wanted to match ASCII characters from 'A' to 'Z', you wouldn't
      put re.compile('A'-'Z'), would you? Well I hope not, I hope you would
      use re.compile('[A-Z]') -- does that give you a clue?
      Traceback (most recent call last):
      File "<interacti ve input>", line 1, in ?
      TypeError: unsupported operand type(s) for -: 'unicode' and 'unicode'
      >
      >
      so re module does NOT support this operation
      Incorrect conclusion. The argument that you attempted to supply is not a
      valid expression and thus not a valid argument for *any* function. It
      was intercepted before it got to re.compile. re.compile is innocent.
      however, is there any alternative way to solve my problem?
      >
      Any comments/suggestions much appreciated!
      1. Read the fantastic manual.
      2. Learn to understand error messages.
      3. Assume the most plausible cause (you stuffed up, not the people who
      worked on the re module).

      HTH,
      John

      Comment

      • yichao.zhang

        #4
        Re: how to match u'\uff00' - u'\uff0f' in re module?

        MacDonald
        Thanks!

        John
        Now I know how silly a question that is !
        However I'll be not so silly thanks to your help !
        I'll follow your suggestions! That's very nice.

        Comment

        Working...