views:

40

answers:

2

I need a regular expression to validate string with one or more of these characters:

  • a-z
  • A-Z
  • '
  • àòèéùì
  • simple white space

FOR EXAMPLE these string are valide:

D' argon calabrò

maryòn l' Ancol

these string are NOT valide:

hello38239

my_house 

work [tab] with me

I tryed this:

re.match(r"^[a-zA-Z 'òàèéìù]+$", string )

It seems to work in my python shell but in Django I get this error:

SyntaxError at /home/

("Non-ASCII character '\\xc3' ...

Why ?


Edit:

I have added # -- coding: utf-8 -- at the top of my forms.py but the strings with à,è,ò,ù,é or ì doesn't match never.

This is my forms.py clean method:

    def clean_title(self):

        if(re.match(r"^[a-zA-Z 'òàèéìù]+$", self.cleaned_data['title'].strip())):
            return self.cleaned_data['title'].strip()               
        raise forms.ValidationError(_("This title is not valid."))
+1  A: 

Well, those are pretty much all non-ascii characters. So i'd figure that it's using just ascii for character encoding. Maybe you need to configure it to using UTF-8?

JHollanti
How can I configure django to using UTF-8 ?
xRobot
I wouldn't know, i've never touched Django ;) But maybe this might help you: http://stackoverflow.com/questions/2743070/removing-non-ascii-characters-from-a-string-using-python-django
JHollanti
+2  A: 

If you user Non-ASCII characters in your python source files you should add proper encoding to the top of your source file like this:

# -*- coding: utf-8 -*-
utf_string='čćžđšp'

Defining Python Source Code Encodings

This seems to work fine for me:

>>> import re
>>> mystring = "D' argon calabrò"
>>> matched = re.match(r"^([a-zA-Z 'òàèéìù]+)$", mystring)
>>> print matched.groups()
("D' argon calabr\xc3\xb2",)
rebus
I have added # -*- coding: utf-8 -*- at the top of my forms.py .Now I don't reiceve the error above ("Non-ASCII character '\\xc3' ) but if the string cointain à,é,è,ò,ù or ì then doesn't match. For other chars match. Why ? Thanks ^_^
xRobot
I have just added some other information above :)
xRobot
Not sure, seems to me that regex works ok...
rebus
yes in my python shell it works... but in django not. The strings with à,è,ò,ù,é or ì doesn't match never.
xRobot