A NSTextCheckingResult
有多個項目索引到它。
[match rangeAtIndex:0];
是完整比賽。
[match rangeAtIndex:1];
(如果存在)是第一次捕獲組匹配。
等
您可以使用這樣的事情:
NSString *searchedString = @"domain-name.tld.tld2";
NSRange searchedRange = NSMakeRange(0, [searchedString length]);
NSString *pattern = @"(?:www\\.)?((?!-)[a-zA-Z0-9-]{2,63}(?<!-))\\.?((?:[a-zA-Z0-9]{2,})?(?:\\.[a-zA-Z0-9]{2,})?)";
NSError *error = nil;
NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern: pattern options:0 error:&error];
NSArray* matches = [regex matchesInString:searchedString options:0 range: searchedRange];
for (NSTextCheckingResult* match in matches) {
NSString* matchText = [searchedString substringWithRange:[match range]];
NSLog(@"match: %@", matchText);
NSRange group1 = [match rangeAtIndex:1];
NSRange group2 = [match rangeAtIndex:2];
NSLog(@"group1: %@", [searchedString substringWithRange:group1]);
NSLog(@"group2: %@", [searchedString substringWithRange:group2]);
}
的NSLog輸出:
匹配:域name.tld.tld2
域名
TLD。tld2
測試匹配範圍是否有效。
更簡單地在這種情況下:
NSString *searchedString = @"domain-name.tld.tld2";
NSRange searchedRange = NSMakeRange(0, [searchedString length]);
NSString *pattern = @"(?:www\\.)?((?!-)[a-zA-Z0-9-]{2,63}(?<!-))\\.?((?:[a-zA-Z0-9]{2,})?(?:\\.[a-zA-Z0-9]{2,})?)";
NSError *error = nil;
NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:&error];
NSTextCheckingResult *match = [regex firstMatchInString:searchedString options:0 range: searchedRange];
NSLog(@"group1: %@", [searchedString substringWithRange:[match rangeAtIndex:1]]);
NSLog(@"group2: %@", [searchedString substringWithRange:[match rangeAtIndex:2]]);
夫特3.0:
let searchedString = "domain-name.tld.tld2"
let nsSearchedString = searchedString as NSString
let searchedRange = NSMakeRange(0, searchedString.characters.count)
let pattern = "(?:www\\.)?((?!-)[a-zA-Z0-9-]{2,63}(?<!-))\\.?((?:[a-zA-Z0-9]{2,})?(?:\\.[a-zA-Z0-9]{2,})?)"
do {
let regex = try NSRegularExpression(pattern:pattern, options: [])
let matches = regex.matches(in:searchedString, options:[], range:searchedRange)
for match in matches {
let matchText = nsSearchedString.substring(with:match.range);
print("match: \(matchText)");
let group1 : NSRange = match.rangeAt(1)
let matchText1 = nsSearchedString.substring(with: group1)
print("matchText1: \(matchText1)")
let group2 = match.rangeAt(2)
let matchText2 = nsSearchedString.substring(with: group2)
print("matchText2: \(matchText2)")
}
} catch let error as NSError {
print(error.localizedDescription)
}
打印輸出:
匹配:域name.tld.tld2
matchText1:d omain名
matchText2:tld.tld2
更簡單地在這種情況下:
do {
let regex = try NSRegularExpression(pattern:pattern, options: [])
let match = regex.firstMatch(in:searchedString, options:[], range:searchedRange)
let matchText1 = nsSearchedString.substring(with: match!.rangeAt(1))
print("matchText1: \(matchText1)")
let matchText2 = nsSearchedString.substring(with: match!.rangeAt(2))
print("matchText2: \(matchText2)")
} catch let error as NSError {
print(error.localizedDescription)
}
打印輸出:
matchText1:域名
matchText2:tld.tld2
電子郵件驗證? – Hiren 2012-02-14 11:47:07
也許這有幫助嗎? http://stackoverflow.com/questions/5478170/regular-expression-in-ios – dom 2012-02-14 11:49:38
我認爲恐諾問如何將現有的正則表達式轉換爲ObjectiveC代碼。正則表達式的作用並不重要。 – 2012-02-14 11:56:05