Skip to content

Instantly share code, notes, and snippets.

@monsoir
Created December 1, 2016 16:40
Show Gist options
  • Save monsoir/168d8a06d798ee3c6f07edc5168a630b to your computer and use it in GitHub Desktop.
Save monsoir/168d8a06d798ee3c6f07edc5168a630b to your computer and use it in GitHub Desktop.
// Create Regular Expression
NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:<# 正则表达式 #> options:<# 选项 #> error:&error];
// Counting the number of matches in a given range of a string
NSUInteger numberOfMatches = [regex numberOfMatchesInString:<# 需要检查的字符串 #> option:<# NSMatchingOptions #> range:<# 检查的范围 #>];
// Get the first match range and the string, method 1
NSRange rangeOfFirstMatch = [regex rangeOfFirstMatchInString:<# 需要检查的字符串 #> options:0 range:NSMakeRange(0, [<# 需要检查的字符串 #> length])];
if (!NSEqualRanges(rangeOfFirstMatch, NSMakeRange(NSNotFound, 0))) {
NSString *substringForFirstMatch = [<# 需要检查的字符串 #> substringWithRange:rangeOfFirstMatch];
}
// Get the first match range and the string, method 2
NSTextCheckingResult *match = [regex firstMatchInStirng:<# 需要检查的字符串 #> options:0 range:NSMakeRange(0, [string length])];
// Get all matching results
NSArray *matches = [regex matchesInString:<# 需要检查的字符串 #> options:0 range:NSMakeRange(0, [<# 需要检查的字符串 #> length])];
for (NSTextCheckingResult *match in matches) {
NSRange matchRange = [match range];
NSRange firstHalfRange = [match rangeAtIndex:1];
NSRange secondHalfRange = [match rangeAtIndex:2];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment