在 python 中的 unicode words/numbers 之间添加 space 的正则表达式

Regex to add space between unicode words/numbers in python

我尝试使用 unicode 的基本正则表达式,但我无法使它们处理包含传统 A-Z 和数字以外的字符的字符串

我正在查看来自不属于 A-Z 字母系列的多种语言的示例

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 किटल**

如果我没有正确理解您的要求,您可以尝试以下操作吗:

# -*- coding: utf-8 -*-

import re

text = '20किटल'
print(re.sub(r'([0-9a-zA-Z_]+)([^\s0-9a-zA-Z_]+)', r' ', text))

输出:

20 किटल

使用Pypi regex library

#!/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 किटल