【问题标题】:RegEx .+ does not include space and new line charactersRegEx .+ 不包括空格和换行符
【发布时间】: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


【解决方案1】:

如果我在创建 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 选项,但这只是因为我不小心添加了空格和正则表达式的结尾才需要。应该不需要它来匹配字符串。

【讨论】:

  • 我的 RegEx 会跳过其中包含换行符的方法。
  • 如果 NSRegularExpressionDotMatchesLineSeparators 匹配行分隔符,那么为什么我们需要在正则表达式中指定 \\n?
  • @Zeeshan 在您的正则表达式中 .+ 之后没有 \\n
  • 适用于您的代码使用 \\n*.如果我用 \n 删除新行空格(通过按回车键添加),那么它可以工作。
  • 天才!!我不会想到存在这个小选项。你缓解了我的头痛
【解决方案2】:
@"\\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]);
}

【讨论】:

  • [+|-][()a-z0-9_\\s*][{|;] 如果您不关心 +|- _____ {
  • [+|-][()a-z0-9_\\s*:]*[{|;] 忽略上面那个
猜你喜欢
  • 1970-01-01
  • 2016-04-30
  • 2011-03-14
  • 2012-10-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-12
相关资源
最近更新 更多