1

我正在尝试通过使用或是否可以解决任何问题来检查str这是否只是泰语字符regex

我正在尝试使用

re.compile(u"[^\u0E00-\u0E7F']|^'|'$|''")
ret = regexp_thai.sub("", s)

切片另一种语言或数字的方式只是切片而不是返回布尔值

我希望输出像

s = "engภาษาไทยที่มีสระ123!@"
regexp_thai = re.compile(u"[^\u0E00-\u0E7F']|^'|'$|''") 
ret = regexp_thai.sub("", s)
print(ret)             # ภาษาไทยที่มีสระ
print(isthai(ret))     # True

u0E00-u0E7F是泰语的 unicode 怎么写isthai函数

4

1 回答 1

4

我不太确定期望的输出是什么。但是,我猜我们喜欢捕获 Tai 字母,根据您的原始表达,我们可能只想添加一个简单的字符列表,用捕获组包装它,然后从左到右滑动我们想要的 Tai 字母,可能类似于:

([\u0E00-\u0E7F]+)

演示

测试

# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility

import re

regex = r"([\u0E00-\u0E7F]+)"

test_str = "engภาษาไทยที่มีสระ123!@"

matches = re.finditer(regex, test_str, re.MULTILINE | re.UNICODE)

for matchNum, match in enumerate(matches, start=1):

    print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))

    for groupNum in range(0, len(match.groups())):
        groupNum = groupNum + 1

        print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))

# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.

演示

const regex = /([\u0E00-\u0E7F]+)/gmu;
const str = `engภาษาไทยที่มีสระ123!@`;
let m;

while ((m = regex.exec(str)) !== null) {
    // This is necessary to avoid infinite loops with zero-width matches
    if (m.index === regex.lastIndex) {
        regex.lastIndex++;
    }
    
    // The result can be accessed through the `m`-variable.
    m.forEach((match, groupIndex) => {
        console.log(`Found match, group ${groupIndex}: ${match}`);
    });
}

正则表达式

如果不需要此表达式,可以在regex101.com中对其进行修改或更改。

正则表达式电路

jex.im可视化正则表达式:

在此处输入图像描述

参考

正则表达式接受python中的所有泰语字符和英文字母

于 2019-05-24T03:59:48.223 回答