RegEx。+不包括空格和换行符

时间:2014-04-18 13:24:01

标签: objective-c regex nsregularexpression

我的RegEx

\\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);

2 个答案:

答案 0 :(得分:3)

如果我在创建NSRegularExpression时使用NSRegularExpressionDotMatchesLineSeparators选项,那么正则表达式会匹配该字符串。

我使用的完整代码是:

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]);
}];

修改

此答案的先前版本也必须使用NSRegularExpressionAllowCommentsAndWhitespace选项,但这只是因为我不小心添加空格和正则表达式的结尾。不需要匹配字符串。

答案 1 :(得分: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*[{|;]"

这应该(希望;))适用于任何目标c函数,除了包含块的函数或除了明显参数之外的其他东西

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]);
}