Tag: nsregularexpression

捕获不在NSRegularExpression中工作的组

为什么这个代码只是吐出整个正则expression式匹配而不是捕获组? input @"A long string containing Name:</td><td>A name here</td> amongst other things" 预期产量 A name here 实际产出 Name:</td><td>A name here</td> 码 NSString *htmlString = @"A long string containing Name:</td><td>A name here</td> amongst other things"; NSRegularExpression *nameExpression = [NSRegularExpression regularExpressionWithPattern:@"Name:</td>.*\">(.*)</td>" options:NSRegularExpressionSearch error:nil]; NSArray *matches = [nameExpression matchesInString:htmlString options:0 range:NSMakeRange(0, [htmlString length])]; for (NSTextCheckingResult *match in matches) […]

如何在Objective C(NSRegularExpression)中编写正则expression式?

我有这个正则expression式工作,当我在PHP中testing,但它不能在Objective C中工作: (?:www\.)?((?!-)[a-zA-Z0-9-]{2,63}(?<!-))\.?((?:[a-zA-Z0-9]{2,})?(?:\.[a-zA-Z0-9]{2,})?) 我试图逃脱转义字符,但这也没有帮助。 我应该逃避其他angular色吗? 这是我在Objective C中的代码: NSMutableString *searchedString = [NSMutableString stringWithString:@"domain-name.tld.tld2"]; NSError* error = nil; NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern:@"(?:www\\.)?((?!-)[a-zA-Z0-9-]{2,63}(?<!-))\\.?((?:[a-zA-Z0-9]{2,})?(?:\\.[a-zA-Z0-9]{2,})?)" options:0 error:&error]; NSArray* matches = [regex matchesInString:searchedString options:0 range:NSMakeRange(0, [searchedString length])]; for ( NSTextCheckingResult* match in matches ) { NSString* matchText = [searchedString substringWithRange:[match range]]; NSLog(@"match: %@", matchText); } – 更新 – 这个正则expression式在PHP中返回值为“domain-name”和“tld.tld2”的数组,但在Objective C中,我只获得一个值:“domain-name.tld.tld2” […]