python - 正则表达式在 python 中的 unicode 单词/数字之间添加空格
问题描述
我尝试对 unicode 使用基本的正则表达式,但我无法使它们在具有传统 AZ 和数字以外的字符的字符串上工作
我正在查看不属于 AZ Alphabetical 家族的多种语言的示例
text = "20किटल"
res = re.sub("^[^\W\d_]+$", lambda ele: " " + ele[0] + " ", text)
Output:
20किटल
第二次尝试:
regexp1 = re.compile('^[^\W\d_]+$', re.IGNORECASE | re.UNICODE)
regexp1.sub("^[^\W\d_]+$", lambda ele: " " + ele[0] + " ", text)
Output:
20किटल
Expected output:
**20 किटल**
解决方案
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import regex
text = "20किटल"
pat = regex.compile(r"(?<=\d)(?=\p{L})", re.UNICODE)
res = pat.sub(" ", text)
print res
代表\p{L}
任何语言的任何字母
输出:
20 किटल
推荐阅读
- django - 无法在 VueJs 中从 Django API 渲染图像
- javascript - 方法不等待异步 REST 调用
- c# - 为什么 C# 列表由带符号的 int 索引
- django-rest-framework - 如何在服务器启动时激活虚拟环境并运行 celery 命令来激活 djcelery?
- javascript - 确保组件只能是特定父组件的子组件
- javascript - Browser 和 Pure Data 之间的 OSC 通信(两台计算机)
- php - 如何将连接表的结果数组存储在mysql的行结果中?
- java - 未设置模板变量时,我可以使用 Thymeleaf 引发可捕获的异常吗?
- mysql - 来自多个有关系的表的 2 个日期之间的 Mysql 查询
- java - 在 Java 中,我们应该在处理多个接口时使用泛型吗?