无法在Objective-C中使正则表达式工作

时间:2015-03-31 05:46:08

标签: objective-c regex nsregularexpression

出于某种原因,我无法使正则表达式适用于多行字符串。它在网络上运行良好

我有这样的文字:

    First Line //remove leading spaces
Second Line //nothing to remove
  Third Line //remove leading spaces
Fourth Line
should be 1 line //remove leading spaces and new line next char is not a capital letter

 Fifth line //remove leading spaces, keep new line between the two sentences 

我正在尝试使用此正则表达式

^ +|\b\n\b

http://rubular.com/r/u4Ao5oqeBi

等在线正则表达式编辑器中,它的工作方式几乎没有问题(除了删除了比需要更多的新行)

但它只删除第一行的前导空格 这是我的代码:

        NSString *myText = @"    First Line (remove leading spaces\nSecond Line (nothing to remove\n  Third Line (remove leading spaces\n Fourth Line\nshould be 1 line (remove leading spaces and new line next char is not a capital letter\n\n Fifth line (remove leading spaces, keep new line between the two sentences";
        NSLog(myText);

        NSString *resultText = [myText stringByReplacingOccurrencesOfString: @"^ +|\\b\\n\\b"
                                                                 withString: @""
                                                                    options: NSRegularExpressionSearch
                                                                      range: NSMakeRange(0, myText.length)];

        NSLog(resultText);

1 个答案:

答案 0 :(得分:1)

您的代码不起作用,因为您在stringByReplacingOccurrencesOfString参数中使用带有正则表达式字符串的target而不是输入字符串。它不接受正则表达式。请参阅此功能的Foundation Framework Reference帮助。

您可以使用以下代码删除所有前导空格:

NSError *error = nil;
NSString *myText = @"    First Line (remove leading spaces\nSecond Line (nothing to remove\n  Third Line (remove leading spaces\n Fourth Line\nshould be 1 line (remove leading spaces and new line next char is not a capital letter\n\n Fifth line (remove leading spaces, keep new line between the two sentences";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"^ +" options:NSRegularExpressionCaseInsensitive|NSRegularExpressionAnchorsMatchLines error:&error];
NSString *modifiedString = [regex stringByReplacingMatchesInString:myText options:0 range:NSMakeRange(0, [myText length]) withTemplate:@""];
NSLog(@"%@", modifiedString);

我正在使用NSRegularExpressionAnchorsMatchLines选项使^与行的开头匹配,而不是整个字符串的开头(请参阅此Regular Expressions Metacharacters表)。 输出:

First Line (remove leading spaces                                                                                                                                                                                     
Second Line (nothing to remove                                                                                                                                                                                                                         
Third Line (remove leading spaces                                                                                                                                                                                                                      
Fourth Line                                                                                                                                                                                                                                            
should be 1 line (remove leading spaces and new line next char is not a capital letter                                                                                                                                                                 

Fifth line (remove leading spaces, keep new line between the two sentences