正则表达式在OC字符串中的使用
程序员文章站
2022-03-11 09:29:10
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-z....
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
推荐阅读
-
在Python中处理字符串之isdecimal()方法的使用
-
解析使用substr截取UTF-8中文字符串出现乱码的问题
-
v-for索引index在html中的使用
-
在字符串资源文件中添加HTML元素,直接使用字符串资源,HTML元素没起作用的解决办法_html/css_WEB-ITnose
-
使用charAt()方法计算字符串中的重复字符
-
在vue中如何使用cropperjs的方法(详细教程)
-
JAVA中,如何判断一个字符串中包含的字符在另一个字符前面?
-
在一个字符串中删除在另一个字符串中出现的字符
-
Redis在springboot中的使用教程
-
详解Swift中对C语言接口缓存的使用以及数组与字符串转为指针类型的方法