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

标签 swift string validation persian farsi

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

“凯瑟琳”

“阿巴尼。”

这些不是:

“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/

相关文章:

ios - SnapKit 和动态 UITableViewCell 布局不正确

string - 系统级别的整数和字符串比较

java - textarea.getText() 在 Java 中无法正常工作

Angular 6 + Angular Material - 提交时的表单验证

javascript - ExtJS 4 - 如果自定义验证失败,如何将表单字段标记为无效并在其周围显示红色边框(ExtJS 默认完成)?

swift - 看不到 UIView 的 UIView subview

ios - 无法从 MasterViewController 访问 DetailViewController

php - Strpos 找不到特殊字符

javascript - express-validator 什么都不做

swift - 网络服务返回零