My RegEx
\\s*[+-].+\\s*\\n*\\s*[{]
is not able to find below method of UITableView, but it reads all other methods written in a single line.
- (void)tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
I want to add comment on each method of a file, and to achieve that i am trying to read all method through RegEx.
Any help?
Full Code
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
{
}
Code
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);