简体   繁体   中英

How to search unicode string in python

I am searching some simple Cyrillic patterns in strings using python. The pattern I am using is like /[а-я]+/[а-я]+ . When I search pattern by this code

import re
re.search('/[а-я]+/[а-я]+', '/бцршб/бйцбйц')

It cannot find anything. But when I write it like this.

import re
re.search(u'/[а-я]+/[а-я]+', u'/бцршб/бйцбйц')

It works. However in my case, the pattern and text are predefined in Database, so I couldn't find a way to convert them to the Unicode string. What is the solution in this case. Any help would be appreciated.

Thank you guys. It works when decoding strings. So the code is like:

import re 
pattern = '/[а-я]+/[а-я]+'.decode('utf-8') 
text = '/йцбйц/бйцбц'.decode('utf-8') 
re.search(pattern, text)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM