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

Cannot make regular expression work in Objective-C

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

我有这样的文字:

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

您的代码不起作用,因为您在 target 参数中使用 stringByReplacingOccurrencesOfString 和正则表达式字符串而不是输入字符串。它不接受正则表达式。请参阅有关此函数的 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 table)。 输出:

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