gpt4 book ai didi

swift - 检查或验证波斯语(波斯语)字符串 swift

转载 作者:可可西里 更新时间:2023-11-01 01:55:31 29 4
gpt4 key购买 nike

我搜索了有关波斯语(波斯语)语言字符串验证的网页和堆栈溢出。其中大部分都提到了阿拉伯字母。另外,我想知道我的字符串是否完全是波斯语(不包含)。例如,这些字符串是波斯语:

“你好”

“خابان。”

这些不是:

“خابان 5”

“caster copy caster”

此外,只允许使用波斯语或阿拉伯语数字。 [.,-!] 字符有异常(exception)(因为键盘不支持波斯语中的这些字符)

更新:我在回答中解释了使用正则表达式和谓词的快速版本。

最佳答案

基于在别处找到的这个扩展:

       extension String {
func matches(_ regex: String) -> Bool {
return self.range(of: regex, options: .regularExpression, range: nil, locale: nil) != nil
}
}

并构建包含允许字符的正则表达式

    let mystra = "چهار راه"
let mystrb = "خیابان."
let mystrc = "خیابان 5"
let mystrd = "چرا copy کردی؟" //and so on
for a in mystra {
if String(a).matches("[\u{600}-\u{6FF}\u{064b}\u{064d}\u{064c}\u{064e}\u{064f}\u{0650}\u{0651}\u{0020}]") { // add unicode for dot, comma, and other needed puctuation marks, for now I added space etc

} else { // not in range
print("oh no--\(a)---zzzz")
break // or return false
}
}

确保使用上述模型构建所需的 Unicode。其他字符串的结果对于 mystrb ...等哦不--.---zzzz哦不--5---zzzz哦不--c---zzzz

享受

关于swift - 检查或验证波斯语(波斯语)字符串 swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53668622/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com