美文网首页
字符串正则匹配

字符串正则匹配

作者: 微凉初夏 | 来源:发表于2019-10-08 16:19 被阅读0次

APP中经常需要做正则匹配,如下是一个正则匹配的方法类封装

1. 基本方法
class XJJMatching {
//org --> Original, nor --> Normative//
class func predicate_match(_ org: String, nor: String) -> Bool {
    let predicate = NSPredicate(format: "SELF MATCHES %@", nor)
    return predicate.evaluate(with:org)
}

class func regex_match(_ org: String, nor: String) -> Bool {
    var isMatch: Bool = false
    do {
        let regex = try NSRegularExpression(pattern: nor, options: .caseInsensitive)
        let results = regex.matches(in: org, options: NSRegularExpression.MatchingOptions(rawValue: 0), range: NSMakeRange(0, org.count))
        if results.count > 0 {
            isMatch = true
        }else {
            deprint("not match!")
        }
    } catch {
        deprint("error!")
    }
    
    return isMatch
}
}
2. 常用正则表达式
struct ExpEmail {
static let general = "^[A-Za-z0-9._%+-]+@[A-Za-z0-9]+\\.[A-Za-z]{1,4}$"
}

struct ExpPassword {
//有数字和字母,至少有一个数字和字母
/*
 ^ 匹配一行的开头位置
 (?![0-9]+$) 预测该位置后面不全是数字
 (?![a-zA-Z]+$) 预测该位置后面不全是字母
 [0-9A-Za-z] {6,10} 由6-10位数字或这字母组成
 $ 匹配行结尾位置
 */
static let least_one_letter_number = "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,10}$"
static let letter_number = "^[A-Za-z0-9]+$"
}

struct ExpPhone {
static let general = "^[0-9+]{1,32}$"
static let china = "^(13[0-9]{9})|(14[57][0-9]{8})|(15([0-3]|[5-9])[0-9]{8})|(166[0-9]{8})|(17(0|[6-8])[0-9]{8})|(18[0-9]{9})|((19[0-9])[0-9]{8})$"
static let china_mobile = "(^1(3[4-9]|4[7]|5[0-27-9]|7[8]|8[2-478])\\d{8}$)|(^1705\\d{7}$)"
static let china_unicom = "(^1(3[0-2]|4[5]|5[56]|7[6]|8[56])\\d{8}$)|(^1709\\d{7}$)"
static let china_telecom = "(^1(33|53|77|8[019])\\d{8}$)|(^1700\\d{7}$)"
}

struct ExpWebsite {
static let general = "^(https?:\\/\\/)?([\\da-z\\.-]+)\\.([a-z\\.]{2,6})([\\/\\w \\.-]*)*\\/?$"
}

struct ExpText {
static let title = "^[A-Za-z0-9\\u4e00-\\u9fa5]+$" // 一般的名称、标题(只含有中文、字母、数字)

// 特殊文字(含有中文、字母、数字,以及其他一些特殊符号)
// 特殊符号可根据需要更改
static let description = "^[a-zA-Z0-9\\u4E00-\\u9FA5\\u278b-\\u2792,.;:?!。,!?-_:;\"\"“”、‘’''()() ]+$"
}

struct ExpMac {
static let mac = "^[0-9A-Fa-f:]+$"
}
3. 字符串方法扩展
extension String {

func isPhoneNumber() -> Bool {
    return XJJMatching.regex_match(self, nor: ExpPhone.china)
}

func isNormalTitle() -> Bool {
    return XJJMatching.predicate_match(self, nor: ExpText.title)
}

func isNormalDescription() -> Bool {
    return XJJMatching.predicate_match(self, nor: ExpText.description)
}

func isMac() -> Bool {
    return XJJMatching.predicate_match(self, nor: ExpMac.mac)
}

func isPsw() -> Bool {
    return XJJMatching.predicate_match(self, nor: ExpPassword.letter_number)
}
}

相关文章

  • 正则表达式

    创建正则表达式 正则.test(字符串) 正则去匹配字符串,如果匹配成功就返回真,如果匹配失败就返回假 转义字符 ...

  • 正则表达式

    创建正则表达式 正则.test(字符串) 正则去匹配字符串,如果匹配成功就返回真,如果匹配失败就返回假 转义字符 ...

  • 正则表达式

    正则表达式主要用于字符串的查找、匹配、分割 match(正则表达式字符串,需要匹配的字符串) 1.不带任何正则符号...

  • Python基础(15)——正则表达式

    re.match() re.match(正则表达式,要匹配的字符串),匹配出以字符串的起始位置开始匹配正则表达式,...

  • 提取匹配内容

    正则表达式匹配字符串并提取正则匹配的内容 this.([a-zA-z]+) = 1 代表第一个括号内匹配的字符串

  • 正则位置匹配

    正则表达式要么匹配字符,要么匹配位置。 一、字符串的位置 二、正则表达式如何匹配位置 1、es5中匹配位置的正则:...

  • 正则表达式、ES6 中的表达式的新特性

    正则表达式用于对字符串模式匹配及检索替换,是对字符串执行模式匹配的强大工具。 一、正则表达式 1、定义正则(语法)...

  • Python之路13:

    re 模块,正则匹配表达式 正则匹配表达式就是字符串的匹配规则 re 模块的匹配语法: re.match 从头开始...

  • 正则下

    一、字符串方法(匹配正则)match() 返回匹配字符串的结果,返回为数组内容或nullsearch() ...

  • Linux学习-Shell编程-正则表达式与通配符

    正则: 匹配文件中字符串 ,正则是包含匹配通配符: 匹配文件名,通配符是完全匹配image.png 前一个字符...

网友评论

      本文标题:字符串正则匹配

      本文链接:https://www.haomeiwen.com/subject/hzpoectx.html