3

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);
Iulian Onofrei
  • 9,188
  • 10
  • 67
  • 113
Zeeshan
  • 586
  • 7
  • 15

2 Answers2

3

Your regex matches that string for me if I use the NSRegularExpressionDotMatchesLineSeparators options when creating the NSRegularExpression.

The full code I used was:

NSString *searchText = @"- (void)tableView:(UITableView *)tableView\ncommitEditingStyle:(UITableViewCellEditingStyle)editingStyle\nforRowAtIndexPath:(NSIndexPath *)indexPath\n{";

NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\\s*[+-].+\\s*\\n*\\s*[{]"
                                                                       options:NSRegularExpressionDotMatchesLineSeparators
                                                                         error:&error];


[regex enumerateMatchesInString:searchText
                        options:0
                          range:NSMakeRange(0, [searchText length])
                     usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
    NSLog(@"%@", [searchText substringWithRange:result.range]);
}];

Edit

A previous version of this answer had to use the NSRegularExpressionAllowCommentsAndWhitespace option as well but that was only needed because of me accidentally adding a space and the end of the regex. It should not be needed to match the string.

David Rönnqvist
  • 56,267
  • 18
  • 167
  • 205
0
@"\\s*[+|-]\\s*[(]\\s*[a-z0-9_]*\\s*[*]{0,2}\\s*[)]\\s*([a-z0-9_]*\\s*([:]\\s*[(]\\s*[a-z0-9_]*\\s*[*]{0,2}\\s*[)]\\s*[a-z0-9_]*){0,1})+\\s*[{|;]"

this should( Hopefully ;) ) work for any objective c function except for function containing blocks or something different other than obvious parameters

NSError *error = nil;
NSRegularExpression *regularExpression = [NSRegularExpression regularExpressionWithPattern:@"\\s*[+|-]\\s*[(]\\s*[a-z0-9_]*\\s*[*]{0,2}\\s*[)]\\s*([a-z0-9_]*\\s*([:]\\s*[(]\\s*[a-z0-9_]*\\s*[*]{0,2}\\s*[)]\\s*[a-z0-9_]*){0,1})+\\s*[{|;]"
                                                                                   options:NSRegularExpressionCaseInsensitive
                                                                                     error:&error];

if( error == nil ){
    NSString *function1 = @"- (void)tableView:(UITableView *)tableView\ncommitEditingStyle:(UITableViewCellEditingStyle)editingStyle\nforRowAtIndexPath:(NSIndexPath *)indexPath\n{ NSLog(@\"Hello\"}";
    NSString *function2 = @"+ (NSRegularExpression *)regularExpressionWithPattern:(NSString *)pattern options:(NSRegularExpressionOptions)options error:(NSError **)error {";

    [regularExpression enumerateMatchesInString:function1
                                        options:0
                                          range:NSMakeRange( 0, [function1 length])
                                     usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
                                         NSString *subString = [function1 substringWithRange:[result range]];
                                         NSLog(@"%@ :: %@\n",subString,result);
                                     }];
    NSLog(@"\n\n");
    [regularExpression enumerateMatchesInString:function2
                                        options:0
                                          range:NSMakeRange( 0, [function2 length])
                                     usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
                                         NSString *subString = [function2 substringWithRange:[result range]];
                                         NSLog(@"%@ :: %@\n",subString,result);
                                     }];
}
else {
    NSLog(@"Error :: %@",[error localizedDescription]);
}
Rakesh.P
  • 183
  • 1
  • 2
  • 11