iOS Objective-C NSRegularExpression使用

NSRegularExpression类

创建方法

+ (nullable NSRegularExpression *)regularExpressionWithPattern:(NSString *)pattern
            options:(NSRegularExpressionOptions)options error:(NSError **)error;
- (nullable instancetype)initWithPattern:(NSString *)pattern
            options:(NSRegularExpressionOptions)options error:(NSError **)error

NSRegularExpressionOptions参数

常量说明
NSRegularExpressionCaseInsensitive不区分大小写
NSRegularExpressionAllowCommentsAndWhitespace忽略空格和#
NSRegularExpressionIgnoreMetacharacters把正则表达式整体作为字符串处理
NSRegularExpressionDotMatchesLineSeparators点匹配任何字符,包括行分隔符
NSRegularExpressionAnchorsMatchLines允许^和$在匹配的开始和结束行
NSRegularExpressionUseUnixLineSeparators设置 \n 为唯一的行分隔符
NSRegularExpressionUseUnicodeWordBoundaries使用Unicode TR#29标准作为词的边界

主要方法

// 返回第一个匹配成功的结果
- (nullable NSTextCheckingResult *)firstMatchInString:(NSString *)string
            options:(NSMatchingOptions)options range:(NSRange)range;

// 返回第一个匹配成功结果的范围
- (NSRange)rangeOfFirstMatchInString:(NSString *)string
            options:(NSMatchingOptions)options range:(NSRange)range;

// 返回指定字符串范围内符合正则条件的字符串的个数
- (NSUInteger)numberOfMatchesInString:(NSString *)string
            options:(NSMatchingOptions)options range:(NSRange)range;

// 返回字符串指定范围内的符合正则的字符串结果
- (NSArray<NSTextCheckingResult *> *)matchesInString:(NSString *)string
            options:(NSMatchingOptions)options range:(NSRange)range;

// block方式遍历出合适的结果
- (void)enumerateMatchesInString:(NSString *)string
            options:(NSMatchingOptions)options
            range:(NSRange)range
            usingBlock:(void (NS_NOESCAPE ^)(NSTextCheckingResult * _Nullable result, NSMatchingFlags flags, BOOL *stop))block;

替换字符串

// 用新字段替换原文本中的对应字段,并返回操作后的NSString
- (NSString *)stringByReplacingMatchesInString:(NSString *)string 
            options:(NSMatchingOptions)options
            range:(NSRange)range
            withTemplate:(NSString *)templ;

// 用新字段替换原文本中的对应字段,并返回操作次数
- (NSUInteger)replaceMatchesInString:(NSMutableString *)string
            options:(NSMatchingOptions)options
            range:(NSRange)range
            withTemplate:(NSString *)templ;

示例代码

NSString *pattern = @"a[B|C]d";
NSRegularExpressionOptions option = NSRegularExpressionCaseInsensitive;
NSError *error = nil;

NSString *str = @"abc abd acb acd adb adc";

// 0
NSRegularExpression *regular = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:&error];
NSLog(@"%ld", [regular numberOfMatchesInString:str options:0 range:NSMakeRange(0, str.length)]);

// 2
regular = [[NSRegularExpression alloc] initWithPattern:pattern options:option error:&error];
NSLog(@"%ld", [regular numberOfMatchesInString:str options:0 range:NSMakeRange(0, str.length)]);

// abd
NSTextCheckingResult *result = [regular firstMatchInString:str options:0 range:NSMakeRange(0, str.length)];
if (result.range.location != NSNotFound) {
    NSLog(@"firstMatchInString result: %@", [str substringWithRange:result.range]);
}

// abd
NSRange range = [regular rangeOfFirstMatchInString:str options:0 range:NSMakeRange(0, str.length)];
if (range.location != NSNotFound) {
    NSLog(@"rangeOfFirstMatchInString result: %@", [str substringWithRange:range]);
}

// abd
// acd
NSArray *results = [regular matchesInString:str options:0 range:NSMakeRange(0, str.length)];
for (NSTextCheckingResult *result in results) {
    NSLog(@"matchesInString result: %@", [str substringWithRange:result.range]);
}

// abd
// acd
[regular enumerateMatchesInString:str options:0 range:NSMakeRange(0, str.length) 
            usingBlock:^(NSTextCheckingResult * _Nullable result, NSMatchingFlags flags, BOOL * _Nonnull stop) {
                NSLog(@"enumerateMatchesInString result: %@", [str substringWithRange:result.range]);
            }];

// abc ### acb ### adb adc
NSString *replaceStr = [regular stringByReplacingMatchesInString:str options:0 range:NSMakeRange(0, str.length) withTemplate:@"###"];
NSLog(@"stringByReplacingMatchesInString result: %@", replaceStr);

// 2
// abc ### acb ### adb adc
NSMutableString *mutableString = [NSMutableString stringWithString:str];
NSUInteger number = [regular replaceMatchesInString:mutableString options:0 range:NSMakeRange(0, mutableString.length) withTemplate:@"###"];
NSLog(@"replaceMatchesInString number = %ld result: %@", number, mutableString);

输出

0
2
firstMatchInString result: abd
rangeOfFirstMatchInString result: abd
matchesInString result: abd
matchesInString result: acd
enumerateMatchesInString result: abd
enumerateMatchesInString result: acd
stringByReplacingMatchesInString result: abc ### acb ### adb adc
replaceMatchesInString number = 2 result: abc ### acb ### adb adc
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值