Here is my code:
NSRegularExpression * regex;
- (void)viewDidLoad {
NSError *error = NULL;
regex = [NSRegularExpression regularExpressionWithPattern:@"<*>" options:NSRegularExpressionCaseInsensitive error:&error];
}
- (IBAction)findWord {
NSString * fileContents=[NSString stringWithContentsOfFile:[NSString stringWithFormat:@"%@/report1_index1_page1.html", [[NSBundle mainBundle] resourcePath]]];
NSLog(@"%@",fileContents);
NSString * modifiedString = [regex stringByReplacingMatchesInString:fileContents
options:0
range:NSMakeRange(0, [fileContents length])
withTemplate:@"$1"];
NSLog(@"%@",modifiedString);
}
My ‘modifiedString’ is returning (null).Why?I want to replace any characters between ‘<‘ and ‘>’ including ‘<‘ and ‘>’ simply by a space.
I am guessing this has a lot to do with the fact that you are assigning an autoreleased object to
regexinviewDidLoad. Try adding aretainor move the line to thefindWordmethod.Regex
The regular expression for matching everything between
<and>is incorrect. The correct way would be,Replace by space
If you want to replace the matched string with
" "then you shouldn’t pass$1as the template. Rather, use" "as the template.