i have
NSString *str = @"<0x112233><0x112233><0x112233>0a";
i want this data to be saved in nsarray
我希望这些数据保存在nsarray中
nsarray : { 0x112233 0x112233 0x112233 }
only save data which is enclosed in "<>" and ignore all other.
仅保存包含在“<>”中的数据并忽略所有其他数据。
i tried regex like this
我试过像这样的正则表达式
NSString *str = @"<0x112233><0x112233><0x112233>0a";
NSError *error2;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"<(.*)>"
options:NSRegularExpressionCaseInsensitive
error:&error2];
if (error2)
{
NSLog(@"Error %@", [error2 description]);
}
NSArray *payloadRanges=[regex matchesInString:str options:0 range:NSMakeRange(0, [str length])];
NSLog(@"data %@",payloadRanges);
but i gives me this output:
但我给了我这个输出:
data (
"<NSSimpleRegularExpressionCheckingResult: 0x1ddc5e30>{0, 30}{<NSRegularExpression: 0x1f078710> <(.*)> 0x1}"
)
4 个解决方案
#1
5
(As Pat Teen said in a comment), your pattern is greedy, which means that the first match gets as much as possible, which is the entire string.
(正如Pat Teen在评论中所说),你的模式是贪婪的,这意味着第一场比赛尽可能多,这就是整个弦乐。
You can fix that by changing the pattern @"<(.*)>"
to @"<(.*?)>"
.
您可以通过将模式@“<(。*)>”更改为@“<(。*?)>”来解决此问题。
Then payloadRanges
is an array of three NSTextCheckingResult
objects:
然后payloadRanges是一个包含三个NSTextCheckingResult对象的数组:
(
"<NSSimpleRegularExpressionCheckingResult: 0x10010af40>{0, 10}{<NSRegularExpression: 0x100108960> <(.*?)> 0x1}",
"<NSSimpleRegularExpressionCheckingResult: 0x10010c210>{10, 10}{<NSRegularExpression: 0x100108960> <(.*?)> 0x1}",
"<NSSimpleRegularExpressionCheckingResult: 0x10010c250>{20, 10}{<NSRegularExpression: 0x100108960> <(.*?)> 0x1}"
)
Now you have to extract the substring matching the (...)
group from each result:
现在,您必须从每个结果中提取与(...)组匹配的子字符串:
NSMutableArray *data = [NSMutableArray array];
for (NSTextCheckingResult *result in payloadRanges) {
[data addObject:[str substringWithRange:[result rangeAtIndex:1]]];
}
NSLog(@"data %@",data);
Output:
data (
0x112233,
0x112233,
0x112233
)
#2
1
Cool , Try this simple line of manually hardcoded without using of any reg exper.:
酷,尝试这个简单的手动硬编码线,而不使用任何reg专业知识:
NSString *convert =[str stringByReplacingOccurrencesOfString:@"<" withString:@""];
convert= [convert stringByReplacingOccurrencesOfString:@">" withString:@" "];
NSLog(@"con %@",convert);
NSMutableArray *array = (NSMutableArray *)[convert componentsSeparatedByString:@" "];
NSLog(@"%@" ,array);
for(NSString *str in array){
if([str length]<4){
[array removeObject:str];
}
}
NSLog(@"%@",array);
#3
0
Try this:-
NSString *str = @"<0x112233><0x112233><0x112233>0a";
NSArray *arr = [str componentsSeparatedByString:@"<"];
NSString *str1 = [arr lastObject];
NSArray *arr1 = [str1 componentsSeparatedByString:@">"];
NSLog(@"array is %@",arr1);
NSString *str2 = [[arr1 objectAtIndex:0] substringFromIndex:1];
NSString *str3=[str2 stringByReplacingOccurrencesOfString:@"0a" withString:@""];
NSLog(@"str3 is %@",str3);
[yourArray addObject:str3];
#4
0
Just another regex that should work with your requirements:
只是另一个符合您要求的正则表达式:
<([^>]*)>
#1
5
(As Pat Teen said in a comment), your pattern is greedy, which means that the first match gets as much as possible, which is the entire string.
(正如Pat Teen在评论中所说),你的模式是贪婪的,这意味着第一场比赛尽可能多,这就是整个弦乐。
You can fix that by changing the pattern @"<(.*)>"
to @"<(.*?)>"
.
您可以通过将模式@“<(。*)>”更改为@“<(。*?)>”来解决此问题。
Then payloadRanges
is an array of three NSTextCheckingResult
objects:
然后payloadRanges是一个包含三个NSTextCheckingResult对象的数组:
(
"<NSSimpleRegularExpressionCheckingResult: 0x10010af40>{0, 10}{<NSRegularExpression: 0x100108960> <(.*?)> 0x1}",
"<NSSimpleRegularExpressionCheckingResult: 0x10010c210>{10, 10}{<NSRegularExpression: 0x100108960> <(.*?)> 0x1}",
"<NSSimpleRegularExpressionCheckingResult: 0x10010c250>{20, 10}{<NSRegularExpression: 0x100108960> <(.*?)> 0x1}"
)
Now you have to extract the substring matching the (...)
group from each result:
现在,您必须从每个结果中提取与(...)组匹配的子字符串:
NSMutableArray *data = [NSMutableArray array];
for (NSTextCheckingResult *result in payloadRanges) {
[data addObject:[str substringWithRange:[result rangeAtIndex:1]]];
}
NSLog(@"data %@",data);
Output:
data (
0x112233,
0x112233,
0x112233
)
#2
1
Cool , Try this simple line of manually hardcoded without using of any reg exper.:
酷,尝试这个简单的手动硬编码线,而不使用任何reg专业知识:
NSString *convert =[str stringByReplacingOccurrencesOfString:@"<" withString:@""];
convert= [convert stringByReplacingOccurrencesOfString:@">" withString:@" "];
NSLog(@"con %@",convert);
NSMutableArray *array = (NSMutableArray *)[convert componentsSeparatedByString:@" "];
NSLog(@"%@" ,array);
for(NSString *str in array){
if([str length]<4){
[array removeObject:str];
}
}
NSLog(@"%@",array);
#3
0
Try this:-
NSString *str = @"<0x112233><0x112233><0x112233>0a";
NSArray *arr = [str componentsSeparatedByString:@"<"];
NSString *str1 = [arr lastObject];
NSArray *arr1 = [str1 componentsSeparatedByString:@">"];
NSLog(@"array is %@",arr1);
NSString *str2 = [[arr1 objectAtIndex:0] substringFromIndex:1];
NSString *str3=[str2 stringByReplacingOccurrencesOfString:@"0a" withString:@""];
NSLog(@"str3 is %@",str3);
[yourArray addObject:str3];
#4
0
Just another regex that should work with your requirements:
只是另一个符合您要求的正则表达式:
<([^>]*)>