如何使用NSRegularExpression提取字符串的特定部分?

时间:2022-09-13 08:35:06

Say I have a bunch of different strings:

说我有一堆不同的字符串:

"http://website.com/283/comments/einp43/2398/32/34/23/4/4"
"http://website.com/23283/l34/comments/inhd3/3928/3/2/3"
"http://website.com/pics/283/comments/en43/a89st/389238/a823/"
"http://website.com/pics/hd/283/comments/as87/asd7j/3"

And I always want the portion that follows comments/ which is a valuable ID. But I don't want the comments part, I just want the ID.

我总是希望注释后面的部分是有价值的ID。但我不希望评论部分,我只想要ID。

How do I isolate/extract that?

我该如何分离/提取?

2 个解决方案

#1


4  

Assuming the website name is stored as an NSString called websiteName:

假设网站名称存储为名为websiteName的NSString:

NSArray *components = [websiteName componentsSeparatedByString:@"comments/"];  
NSString *valuableID = [components lastObject];

(Edit: I didn't notice the mention of NSRegularExpression in the title until after posting this answer, but I don't think regex is necessary in this case since you're looking for a single constant string and have no need for complex pattern recognition.)

(编辑:我没有注意到NSRegularExpression的标题提及,直到发布这个答案后,但我不认为正则表达式是必要的,这种情况下,因为你正在寻找一个常量字符串,而且不需要复杂的图案承认。)

#2


2  

You can do it with regex, try this code

您可以使用正则表达式执行此操作,请尝试此代码

NSString *string = @"http://website.com/23283/l34/comments/inhd3/3928/3/2/3";

NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"/comments/([^/]*)" options:NSRegularExpressionCaseInsensitive error:nil];
[regex enumerateMatchesInString:string options:0 range:NSMakeRange(0, string.length) usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
    NSString *subStr = [string substringWithRange:[result rangeAtIndex:1]];
    NSLog(@"commentId = %@", subStr);
}];

output:

commentId = inhd3

#1


4  

Assuming the website name is stored as an NSString called websiteName:

假设网站名称存储为名为websiteName的NSString:

NSArray *components = [websiteName componentsSeparatedByString:@"comments/"];  
NSString *valuableID = [components lastObject];

(Edit: I didn't notice the mention of NSRegularExpression in the title until after posting this answer, but I don't think regex is necessary in this case since you're looking for a single constant string and have no need for complex pattern recognition.)

(编辑:我没有注意到NSRegularExpression的标题提及,直到发布这个答案后,但我不认为正则表达式是必要的,这种情况下,因为你正在寻找一个常量字符串,而且不需要复杂的图案承认。)

#2


2  

You can do it with regex, try this code

您可以使用正则表达式执行此操作,请尝试此代码

NSString *string = @"http://website.com/23283/l34/comments/inhd3/3928/3/2/3";

NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"/comments/([^/]*)" options:NSRegularExpressionCaseInsensitive error:nil];
[regex enumerateMatchesInString:string options:0 range:NSMakeRange(0, string.length) usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
    NSString *subStr = [string substringWithRange:[result rangeAtIndex:1]];
    NSLog(@"commentId = %@", subStr);
}];

output:

commentId = inhd3