当前位置: 移动技术网 > IT编程>开发语言>正则 > 正则表达式在OC字符串中的使用

正则表达式在OC字符串中的使用

2020年07月17日  | 移动技术网IT编程  | 我要评论

1.  判断字符串是否合法

使用正则表达式可以判断某些字符串是否符合预期结果.例如常用的判断手机号是否合法,判断字符串中是否包含中文字符串等.

判断完整字符串是否合法.例如验证手机号,身份证号是否合法,网址链接是否合法等.

手机号: ^1[3-9]\\d{9}$
身份证号: ^[0-9]{15}$)|([0-9]{17}([0-9]|X)$
中文姓名: ^[\u4E00-\u9FA5]{2,}
网址链接: ^(http|https|ftp)\://([a-zA-Z0-9\.\-]+(\:[a-zA-Z0-9\.&%\$\-]+)*@)?((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+\.[a-zA-Z]{2,4})(\:[0-9]+)?(/[^/][a-zA-Z0-9\.\,\?\'\\/\+&%\$#\=~_\-@]*)*$

// 判断手机号是否合法
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", @"^1[3-9]\\d{9}$"];
    NSArray <NSString *> * mobiles = @[
        @"17826830415",
        @"12826830415",
        @"27826830415",
        @"1782683041"
    ];

    for (NSString *mobile in mobiles) {
        BOOL match = [predicate evaluateWithObject:mobile];
        NSLog(@"手机号码[%@]%@", mobile, match ? @"合法" : @"不合法");
    }

 

2. 筛选字符串中符合要求的结果

使用正则表达式可以从字符串中筛选出符合要求的子串.

// 例如在字符串中筛选出以Window开头的所有子串.
    NSError *error = nil;
    NSRegularExpression *reg = [NSRegularExpression regularExpressionWithPattern:@"\\bWindows[0-9a-zA-Z]+" options:(NSRegularExpressionAllowCommentsAndWhitespace) error:&error];
    
    NSString *content = @"历史上比较受欢迎的电脑操作系统版本有: WindowsXp, WindowsNT, Windows98, Windows95等";
    if (!error) {
        NSArray<NSTextCheckingResult *> * result = [reg matchesInString:content options:(NSMatchingReportCompletion) range:(NSRange){0, content.length}];
        
        for (NSTextCheckingResult *ele in result) {
            NSLog(@"element == %@\n", [content substringWithRange:ele.range]);
        }
    }

3.  替换字符串中的某些内容

使用正则表达式可以动态修改/替换字符串中的某些内容,将字符串修改为符合要求的内容.

// 将电话号码中中间四位替换为****

    NSMutableString *content = @"17826830415".mutableCopy;
    NSError *error = nil;
    NSRegularExpression *reg = [NSRegularExpression regularExpressionWithPattern:@"(?<=1[3-9]{2})([0-9]{4})(?=[0-9]{4})" options:(NSRegularExpressionAllowCommentsAndWhitespace) error:&error];
    if (!error) {
        [reg replaceMatchesInString:content options:(NSMatchingReportCompletion) range:(NSRange){0, content.length} withTemplate:@"****"];
        
        NSLog(@"content == %@", content);
    }


3.  在原始字符串查找符合条件的子串,保留子串并在子串指定位置新增内容

使用正则表达式可以保存匹配到的字符串,并在子串的指定位置作新增.

// 例如将js方法调用都通过统一的方法进行转发

    NSMutableString *content = [NSMutableString stringWithString:@"var view = UIView.alloc().init();per.setFrame({x:20, y:30, width:200, height:400});"];
    
    NSError *error = nil;
    NSRegularExpression *reg = [NSRegularExpression regularExpressionWithPattern:@"(?<!\\\\)\\.\\s*(\\w+)\\s*\\(" options:(NSRegularExpressionAllowCommentsAndWhitespace) error:&error];
    
    if (!error) {
        [reg replaceMatchesInString:content options:(NSMatchingReportCompletion) range:(NSRange){0, content.length} withTemplate:@".__c(\"$1\")("];
        NSLog(@"content == %@", content);
    }


// 输出结果:
content == var view = UIView.__c("alloc")().__c("init")();view.__c("setFrame")({x:20, y:30, width:200, height:400});

这就是JSPatch的一个重要基础.

本文地址:https://blog.csdn.net/WangErice/article/details/107362458

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网