I’d like to replace angle brackets and spaces in a string. For example @"< something >" goes to @"something". I’m currently trying:
NSString *s1 = @"< something >";
NSString *s2 =
[s1 stringByReplacingOccurrencesOfString:@"[<> ]"
withString:@""
options:NSRegularExpressionSearch
range:NSMakeRange(0, s1.length)];
However I’m brand new to regular expressions and @"[<> ]" doesn’t seem to work. What’s the correct regular expression to use to remove angle brackets and spaces?
Note the documentation for
NSRegularExpressionSearch:So even if you had the right regex, you wouldn’t be able to use it with this method.
For the situation you describe, it’ll probably be easier to use
-stringByTrimmingCharactersInSet:with a character set that includes ‘<‘ and ‘ ‘.Update: Joshua Weinberg points out that you’d like to remove all occurrences of the characters ‘<‘, ‘>’, and ‘ ‘. If that’s the case, you’ll want to look to NSRegularExpression’s
-stringByReplacingMatchesInString:options:range:withTemplate:method:(I haven’t tested that, but it should point you in the right direction.)