I'm trying to do a regular expression replace all function, getting the search and replace text from a NSSearchField, but I can't seem to manage using \n in the replace.
I must be missing something obvious.
Any ideas on how to do what I want?
Replace text is
Search text is
Example text in textview is
if I use 1, 2 or 4 '\' chars to escape the replace still doesn't actually work, properly - 1 inserts 'n', 2 inserts '\n' and 4 inserts '\\n'
None actually replace with a newline character
If I alter the function to use directly @"$1\n\n$2" (instead of getting same text from NSSearchField) then it would work as expected
The Action/function is as follows
I must be missing something obvious.
Any ideas on how to do what I want?
Replace text is
Code:
$1\n\n$2
Search text is
Code:
([a-z])([A-Z])
Example text in textview is
Code:
just a sampleShould be broken
if I use 1, 2 or 4 '\' chars to escape the replace still doesn't actually work, properly - 1 inserts 'n', 2 inserts '\n' and 4 inserts '\\n'
None actually replace with a newline character
If I alter the function to use directly @"$1\n\n$2" (instead of getting same text from NSSearchField) then it would work as expected
The Action/function is as follows
Code:
- (IBAction)clickedReplaceAll:(id)sender {
NSString *searchString = [findSearchField stringValue];
NSString *replaceString = [findReplace2Field stringValue];
NSError *error;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:searchString options:NSRegularExpressionAnchorsMatchLines error:&error];
NSString *allText = _textview.string;
NSString *modifiedString = [regex stringByReplacingMatchesInString:allText options:0 range:NSMakeRange(0, [allText length]) withTemplate: replaceString];
[_textview setString:modifiedString];
}