By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,562 Members | 964 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,562 IT Pros & Developers. It's quick & easy.

Accent character problem

P: n/a
Hi all and one
i wrote this script, working fine without fail( just run it)

import re
value='This is Praveen'
print value
#value = 'riché gerry'
#words=str(value.split()).strip('[]').replace(', ', '') ( here i tried
to convert in to list and then back to string)
#print words
richre=re.compile(r'[a-zA-Z0-9]')

if(richre.match(value)):
print "Valid"
else:
print "Not allowed special characters"

Output 1: (Fair)
This is Praveen
Valid
but when i change the value like
value='éhis is Praveen'
then

Output 2:(Fair)
éhis is Praveen
Not allowed special characters
(because i wanted to check out the ascent(é) character so its working
fine no issue)

but when i give ascent(é) character in middle like
value='This és Praveen'

Output 3:(not fair)

This és Praveen
Valid
even it have ascent character it should display message "Not allowed
special characters"
Please help me out.
Thanks

Jun 27 '08 #1
Share this Question
Share on Google+
2 Replies

P: n/a

"Sallu" <pr*****************@gmail.comwrote in message
news:fa**********************************@t12g2000 prg.googlegroups.com...
Hi all and one
i wrote this script, working fine without fail( just run it)

import re
value='This is Praveen'
print value
#value = 'riché gerry'
#words=str(value.split()).strip('[]').replace(', ', '') ( here i tried
to convert in to list and then back to string)
#print words
richre=re.compile(r'[a-zA-Z0-9]')

if(richre.match(value)):
print "Valid"
else:
print "Not allowed special characters"

Output 1: (Fair)
This is Praveen
Valid
but when i change the value like
value='éhis is Praveen'
then

Output 2:(Fair)
éhis is Praveen
Not allowed special characters
(because i wanted to check out the ascent(é) character so its working
fine no issue)

but when i give ascent(é) character in middle like
value='This és Praveen'

Output 3:(not fair)

This és Praveen
Valid
even it have ascent character it should display message "Not allowed
special characters"
Please help me out.
Thanks
The match function only matches the pattern at the start of a string. Use
search instead. Printing the results of a successful match will help you
debug the problems also.

matchobj = richre.match(value)
if matchobj:
print matchobj.group()
else:
print 'no match'

As written you will only get a successful match if your string starts with
a-zA-Z0-9, which is why #1 and #3 print 'Valid'.

You also should declare the encoding of the file and consider using Unicode
strings.

-Mark

Jun 27 '08 #2

P: n/a
Sallu schrieb:
Hi all and one
i wrote this script, working fine without fail( just run it)

import re
value='This is Praveen'
print value
#value = 'riché gerry'
#words=str(value.split()).strip('[]').replace(', ', '') ( here i tried
to convert in to list and then back to string)
#print words
richre=re.compile(r'[a-zA-Z0-9]')

if(richre.match(value)):
print "Valid"
else:
print "Not allowed special characters"

Output 1: (Fair)
This is Praveen
Valid
but when i change the value like
value='éhis is Praveen'
then

Output 2:(Fair)
éhis is Praveen
Not allowed special characters
(because i wanted to check out the ascent(é) character so its working
fine no issue)

but when i give ascent(é) character in middle like
value='This és Praveen'

Output 3:(not fair)

This és Praveen
Valid
even it have ascent character it should display message "Not allowed
special characters"
Please help me out.
You only test the *first* character. You need to modify your rex using a
+, like this:

richre=re.compile(r'[a-zA-Z0-9]+')

Only then it will match the whole word.

Diez
Jun 27 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.