在正则表达式中对阿拉伯字母使用范围 [英] Using range in regex for Arabic letters

查看:38
本文介绍了在正则表达式中对阿拉伯字母使用范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Python 中使用 Regex 时,很容易使用方括号来表示一系列字符 a-z,但这似乎不适用于其他语言,例如阿拉伯语:

导入重新模式 = '[ي-ا]'p = 重新编译(模式)

这会导致以

结尾的很长的错误报告

引发错误(错误字符范围")sre_constants.error: 错误的字符范围

如何解决这个问题?

解决方案

改用 Unicode 转义.

<预><代码>>>>re.compile('[\u0627-\u064a]')<_sre.SRE_Pattern 对象在 0x237f460>

When using Regex in Python, it's easy to use brackets to represent a range of characters a-z, but this doesn't seem to be working for other languages, like Arabic:

import re
pattern = '[ي-ا]'
p = re.compile(pattern)

This results in a long error report that ends with

raise error("bad character range")
sre_constants.error: bad character range

how can this be fixed?

解决方案

Use Unicode escapes instead.

>>> re.compile('[\u0627-\u064a]')
<_sre.SRE_Pattern object at 0x237f460>

这篇关于在正则表达式中对阿拉伯字母使用范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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