Menu

#59 Doesn't Properly Match Beginning-Of-Line

RegexKitLite 4.0
open-invalid
5
2011-02-26
2011-02-26
Taldar
No

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.

Discussion

  • John Engelhart

    John Engelhart - 2011-02-26

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

     
  • John Engelhart

    John Engelhart - 2011-02-26
    • status: open --> closed-invalid
     
  • Taldar

    Taldar - 2011-02-26

    I apologize for reporting this bug prematurely. I simply misunderstood the default behavior of the ^ and $ flags.

     
  • Taldar

    Taldar - 2011-02-26
    • status: closed-invalid --> open-invalid
     

Log in to post a comment.