简体   繁体   English

python正则表达式也匹配特殊字符

[英]python regular expression also match special characters

currently I use this simple script to search for a tag in the string; 目前我使用这个简单的脚本来搜索字符串中的标签;

tag = "#tag"
text = "test string with #tag inserted"
match = re.search(tag, text, re.IGNORECASE) #matches

now suppose the text contains an a-acute; 现在假设文本包含a-acute;

tag = "#tag"
text = "test string with #tág inserted"
match = re.search(tag, text, re.IGNORECASE) #does not match :(

How do I make this match work? 如何使这个匹配工作? should work for other special chars too (é, è, í, etc..) 也适用于其他特殊字符(é,è,í等)

Thanks in advance! 提前致谢!

you can normalize the text with unidecode : 您可以使用unidecode对文本进行规范化

import unicodedata

tag = "#tag"
text = u"test string with #tág inserted and a #tag"
text=unidecode(text)
re.findall(tag, text, re.IGNORECASE)

out: 出:

['#tag', '#tag']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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