我在网页和堆栈溢出中搜索了有关波斯语(波斯语)语言字符串验证的信息。他们中的大多数都提到了阿拉伯字母。另外,我想知道我的字符串是否完全是波斯语(不包含)。 例如,这些字符串是波斯语:
“凯瑟琳”
“阿巴尼。”
这些不是:
“5”
“复制复制”
此外,仅允许使用波斯语或阿拉伯数字。 [.,-!] 字符有异常(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 中的 a ... 等等 哦不------zzzz 哦不--5---zzzz 哦不--c---zzzz
享受
关于swift - 快速检查或验证波斯语(波斯语)字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53668622/