如何匹配正则表达式中的所有 unicode 字母字符和空格?
How to match all unicode alphabetic characters and spaces in a regex?
我正在尝试验证 python 3/ django 表单中的地名。我想获得与以下字符串的匹配项:Los Angeles
、Canada
、中国
和 Россия
。即字符串包含:
- 空间
- 字母字符(来自任何语言)
- 没有数字
- 没有特殊字符(标点、符号等)
我目前使用的模式是 r'^[^\W\d]+$'
,如 How to match alphabetical chars without numeric chars with Python regexp? 中所建议。然而,它似乎只匹配模式 r'^[a-zA-Z]+$
。也就是说,Россия
、Los Angeles
和 中国
不匹配,只有 Canada
匹配。
我的代码示例:
import re
re.search(r'^[^\W\d]+$', 'Россия')
哪个returns什么都没有。
您的示例对我有用,但会找到下划线而不是空格。这有效:
>>> re.search(r'^(?:[^\W\d_]| )+$', 'Los Angeles')
<_sre.SRE_Match object at 0x0000000003C612A0>
>>> re.search(r'^(?:[^\W\d_]| )+$', 'Россия')
<_sre.SRE_Match object at 0x0000000003A0D030>
>>> re.search(r'^(?:[^\W\d_]| )+$', 'Los_Angeles') # not found
>>> re.search(r'^(?:[^\W\d_]| )+$', '中国')
<_sre.SRE_Match object at 0x0000000003C612A0>
我正在尝试验证 python 3/ django 表单中的地名。我想获得与以下字符串的匹配项:Los Angeles
、Canada
、中国
和 Россия
。即字符串包含:
- 空间
- 字母字符(来自任何语言)
- 没有数字
- 没有特殊字符(标点、符号等)
我目前使用的模式是 r'^[^\W\d]+$'
,如 How to match alphabetical chars without numeric chars with Python regexp? 中所建议。然而,它似乎只匹配模式 r'^[a-zA-Z]+$
。也就是说,Россия
、Los Angeles
和 中国
不匹配,只有 Canada
匹配。
我的代码示例:
import re
re.search(r'^[^\W\d]+$', 'Россия')
哪个returns什么都没有。
您的示例对我有用,但会找到下划线而不是空格。这有效:
>>> re.search(r'^(?:[^\W\d_]| )+$', 'Los Angeles')
<_sre.SRE_Match object at 0x0000000003C612A0>
>>> re.search(r'^(?:[^\W\d_]| )+$', 'Россия')
<_sre.SRE_Match object at 0x0000000003A0D030>
>>> re.search(r'^(?:[^\W\d_]| )+$', 'Los_Angeles') # not found
>>> re.search(r'^(?:[^\W\d_]| )+$', '中国')
<_sre.SRE_Match object at 0x0000000003C612A0>