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

Y

yichao.zhang

I'm trying to match the characters from u'\uff00' to u'\uff0f'.
the code below and get a TypeError.
p = re.compile(u'\uff00'-u'\uff0f')
Traceback (most recent call last):
File "<interactive 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!
 
M

MacDonald

yichao.zhang said:
I'm trying to match the characters from u'\uff00' to u'\uff0f'.
the code below and get a TypeError.
p = re.compile(u'\uff00'-u'\uff0f')
Traceback (most recent call last):
File "<interactive 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]')
 
J

John Machin

I'm trying to match the characters from u'\uff00' to u'\uff0f'.
the code below and get a TypeError.
p = re.compile(u'\uff00'-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 "<interactive 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
 
Y

yichao.zhang

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top