【发布时间】:2014-06-03 00:30:13
【问题描述】:
我的正则表达式
\\s*[+-].+\\s*\\n*\\s*[{]
无法找到 UITableView 的以下方法,但它读取了写在一行中的所有其他方法。
- (void)tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
我想为文件的每个方法添加注释,并尝试通过 RegEx 读取所有方法。
有什么帮助吗?
完整代码
Test.txt
- (id)initWithNibName : (NSString *) nibNameOrNil
bundle : (NSBundle *) nibBundleOrNil
{
}
- (void)tableView:(NSTableView *)tableView
commitEditingStyle:(NSString*)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
}
- (void)tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
}
代码
NSString *filepath = [[NSBundle mainBundle] pathForResource:@"Test" ofType:@"txt"];
NSError *err = nil;
// Total methods 13
NSString *content = [NSString stringWithContentsOfFile:filepath encoding:NSUTF8StringEncoding error:&err];
//NSRegularExpressionDotMatchesLineSeparators | NSRegularExpressionAllowCommentsAndWhitespace
NSRegularExpressionOptions regexOptions = NSRegularExpressionCaseInsensitive;
NSString *pattern = nil;
// matches 6
//pattern = [NSString stringWithFormat:@"\\s*[+-].*[{]"];
// matches 10
pattern = [NSString stringWithFormat:@"\\s*[+-].*\\s*\\n*\\s*[{]"];
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:regexOptions error:&err];
if (err) {
NSLog(@"Couldn't create regex with given string and options");
}
NSRange visibleTextRange = NSMakeRange(0, content.length);
NSInteger count = [regex numberOfMatchesInString:content options:0 range:visibleTextRange];
NSLog(@"Total Found: %ld", (long)count);
【问题讨论】:
-
发布失败的代码。
-
如果我们不知道主题如何找到解决方案?
标签: objective-c regex nsregularexpression