regex提取两个charachter或标记之间的所有子字符串

时间:2022-11-27 19:18:27

I need to extract all the strings surrounded by two characters (or maybe two tags)

我需要提取由两个字符(或者两个标签)包围的所有字符串

this is what I've done so far:

这就是我到目前为止所做的:

    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\\[(.*?)\\]" options:NSRegularExpressionCaseInsensitive error:NULL];

    NSArray *myArray = [regex matchesInString:@"[db1]+[db2]+[db3]" options:0 range:NSMakeRange(0, [@"[db1]+[db2]+[db3]" length])] ;

    NSLog(@"%@",[myArray objectAtIndex:0]);
    NSLog(@"%@",[myArray objectAtIndex:1]);
    NSLog(@"%@",[myArray objectAtIndex:2]);

In myArray there are correctly three objects but NSlog prints this:

在myArray中有三个对象,但NSlog输出如下:

<NSSimpleRegularExpressionCheckingResult: 0x926ec30>{0, 5}{<NSRegularExpression: 0x926e660> \[(.*?)\] 0x1}
<NSSimpleRegularExpressionCheckingResult: 0x926eb30>{6, 5}{<NSRegularExpression: 0x926e660> \[(.*?)\] 0x1}
<NSSimpleRegularExpressionCheckingResult: 0x926eb50>{12, 5}{<NSRegularExpression: 0x926e660> \[(.*?)\] 0x1}

instead of db1, db2 and db3

而不是db1、db2和db3

where I'm wrong?

我错了吗?

thank you

谢谢你!

2 个解决方案

#1


20  

According to the documentation matchesInString:options:range: returns an array of NSTextCheckingResults not NSStrings. You will need to loop over the results and use the ranges to get the substrings.

根据文档matchesInString:options:range:返回一个NSTextCheckingResults的数组而不是nsstring。您将需要对结果进行循环,并使用范围来获取子字符串。

NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\\[(.*?)\\]" options:NSRegularExpressionCaseInsensitive error:NULL];

NSString *input = @"[db1]+[db2]+[db3]";
NSArray *myArray = [regex matchesInString:input options:0 range:NSMakeRange(0, [input length])] ;

NSMutableArray *matches = [NSMutableArray arrayWithCapacity:[myArray count]];

for (NSTextCheckingResult *match in myArray) {
     NSRange matchRange = [match rangeAtIndex:1];
     [matches addObject:[input substringWithRange:matchRange]];
     NSLog(@"%@", [matches lastObject]);
}

#2


0  

Or this

或者这个

NSArray *results = [@"[db1]+[db2]+[db3]" matchWithRegex:@"\\[(.*?)\\]"];

//result = @["db1","db2,"db3"]

With this category https://github.com/damienromito/NSString-Matcher

与这一类https://github.com/damienromito/NSString-Matcher

#1


20  

According to the documentation matchesInString:options:range: returns an array of NSTextCheckingResults not NSStrings. You will need to loop over the results and use the ranges to get the substrings.

根据文档matchesInString:options:range:返回一个NSTextCheckingResults的数组而不是nsstring。您将需要对结果进行循环,并使用范围来获取子字符串。

NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\\[(.*?)\\]" options:NSRegularExpressionCaseInsensitive error:NULL];

NSString *input = @"[db1]+[db2]+[db3]";
NSArray *myArray = [regex matchesInString:input options:0 range:NSMakeRange(0, [input length])] ;

NSMutableArray *matches = [NSMutableArray arrayWithCapacity:[myArray count]];

for (NSTextCheckingResult *match in myArray) {
     NSRange matchRange = [match rangeAtIndex:1];
     [matches addObject:[input substringWithRange:matchRange]];
     NSLog(@"%@", [matches lastObject]);
}

#2


0  

Or this

或者这个

NSArray *results = [@"[db1]+[db2]+[db3]" matchWithRegex:@"\\[(.*?)\\]"];

//result = @["db1","db2,"db3"]

With this category https://github.com/damienromito/NSString-Matcher

与这一类https://github.com/damienromito/NSString-Matcher