Doesn't Properly Match Beginning-Of-Line
Status: Beta
Brought to you by:
jengelhart
Given an NSMutableString *longString that consists of code-like indented text, when I perform a replace to remove leading tabs, spaces, and extra line breaks, nothing gets changed:
NSString *whitespaceSearch = @"^\\s+";
[longString replaceOccurrencesOfRegex:whitespaceSearch withString:@""];
No matches are found at all. But when I change whitespaceSearch to @"\\n\\s+" and the replacement string to @"\\n", the method works correctly, except of course on the first line. TextWrangler/BBEdit's find/replace shows that ^\s+ ought to, indeed, catch empty space at the beginning of every line. I have not tested this particular issue on regular NSStrings.
This is not a bug in RegexKitLite.
In order to get the behavior you're looking for, you need to do either:
NSString *whitespaceSearch = @"(?m)^\\s+";
[longString replaceOccurrencesOfRegex:whitespaceSearch withString:@""];
... or ...
NSString *whitespaceSearch = @"^\\s+";
[longString replaceOccurrencesOfRegex:whitespaceSearch withString:@"" options:RKLMultiline range:NSMakeRange(0, [longString length]) error:NULL];
I apologize for reporting this bug prematurely. I simply misunderstood the default behavior of the ^ and $ flags.