Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python regular exp. with a unicode char

Tags:

python

regex

I need a reg exp that will parse something like-

"2 * 240pin"

where the * can be either the regular star or unicode char \u00d7 or just an x. This is what I have but its not working:

multiple= r'^(\d+)\s?x|*|\\u00d7\s?(\d+)(\w{2,4})$'
multiplepat= re.compile(multiple, re.I)
print multiplepat.search(u'1 X 240pin').groups()

returns

multiplepat= re.compile(multiple, re.I)
File "C:\Python26\lib\re.py", line 188, in compile
return _compile(pattern, flags)
File "C:\Python26\lib\re.py", line 243, in _compile
raise error, v # invalid expression
error: nothing to repeat

1 Answers

You need to escape the * as it is a quantifier in the context you use it. But you could also use a character class. So try this:

ur'^(\d+)\s?[x*\u00d7]\s?(\d+)(\w{2,4})$'
like image 184
Gumbo Avatar answered Nov 26 '25 19:11

Gumbo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!