正则表达式匹配所有 unicode 引号 [英] Regex to match all unicode quotation marks

查看:110
本文介绍了正则表达式匹配所有 unicode 引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一个简单的正则表达式来匹配所有 unicode 引号?还是必须像这样手动编码:

quotes = ur"[\"'\u2018\u2019\u201c\u201d]"

感谢您的阅读.

布莱恩

解决方案

Python 不支持 Unicode 属性,因此您不能使用 PiPf 属性,所以我想你的解决方案是最好的.

您可能还想考虑令人遗憾地使用的假引号" - 重音符(´ 和 `` ):\u0060<代码>和\u00B4`.

然后有 guillemets (« » ‹ ›),你也想要那些吗?对于那些使用 \u00BB\u203A\u00AB\u2039.

此外,您的命令有一个小错误:您在 quotes 字符串中添加了反斜杠(因为您使用的是原始字符串).改用三引号字符串.

<预><代码>>>>引号 = ur"[\"'\u2018\u2019\u201c\u201d\u0060\u00b4]">>>引号中的\\"真的>>>引号u'[\\"\'\u2018\u2019\u201c\u201d`\xb4]'>>>引号 = ur"""["'\u2018\u2019\u201c\u201d\u0060\u00b4]""">>>引号中的\\"错误的>>>引号u'["\'\u2018\u2019\u201c\u201d`\xb4]'

Is there a simple regular expression to match all unicode quotes? Or does one have to hand-code it like this:

quotes = ur"[\"'\u2018\u2019\u201c\u201d]"

Thank you for reading.

Brian

解决方案

Python doesn't support Unicode properties, therefore you can't use the Pi and Pf properties, so I guess your solution is as good as it gets.

You might also want to consider the "false quotation marks" that are sadly being used - the acute and grave accent (´ and `` ):\u0060and\u00B4`.

Then there are guillemets (« » ‹ ›), do you want those, too? Use \u00BB\u203A\u00AB\u2039 for those.

Also, your command has a little bug: you're adding the backslash to the quotes string (because you're using a raw string). Use a triple-quoted string instead.

>>> quotes = ur"[\"'\u2018\u2019\u201c\u201d\u0060\u00b4]"
>>> "\\" in quotes
True
>>> quotes
u'[\\"\'\u2018\u2019\u201c\u201d`\xb4]'
>>> quotes = ur"""["'\u2018\u2019\u201c\u201d\u0060\u00b4]"""
>>> "\\" in quotes
False
>>> quotes
u'["\'\u2018\u2019\u201c\u201d`\xb4]'

这篇关于正则表达式匹配所有 unicode 引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆