简体   繁体   中英

Regex - Special alpha characters? - Python

I have a list of simple names such as Márquez ,

because of the á (?< name >[a-zA-Z]+) doesn't seem to be working!

Help would be very much appreciated!

you could use

\w+

with the unicode flag. I assume there's no risk of having digits or underscore in your names.

>>> re.findall('\w+', 'Márquez', re.U)
['Márquez']

You also seem to be missing P after the question mark: (?P< name >[a-zA-Z]+)

For Python < 3 you may want to enable locale:

import locale
locale.setlocale(locale.LC_ALL, '')

And then use re.LOCALE option with your regexpes:

re.findall('\w+', 'Márquez', re.LOCALE)

Though, probably Unicode is the better way to go, though it requires decoding the data from and encoding it to your local encoding.

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