Sadiksha Gautam
Sadiksha Gautam

Reputation: 5152

checking unicode with special characters

I have unicode strings like

u'0d7a6b6b37682bab6d8eda97cda4bad7'

and

u'Brauers, A.'

I would like to distinguish between two of these. I tried using regex with \p{Alphabet} but it does not work with the second example as the second example contains , and .. Can anyone help me with this?

Upvotes: 1

Views: 187

Answers (2)

Ned Batchelder
Ned Batchelder

Reputation: 375494

The simplest thing might be to check for non-hex digits:

if re.match(r'[^0-9a-f]', my_string):
    # This is a u'Brauers, A.' kind of string
else:
    # This is a u'0d7a6b6b37682bab6d8eda97cda4bad7' kind of string

Upvotes: 2

Maria Zverina
Maria Zverina

Reputation: 11163

Just check for hex digits?

>>> re.match(r'^[0-9a-f]*$', u'0d7a6b6b37682bab6d8eda97cda4bad7') != None
True
>>> re.match(r'^[0-9a-f]*$', u'Brauers, A.') != None
False

Upvotes: 4

Related Questions