【发布时间】:2015-02-03 02:45:03
【问题描述】:
我正在尝试解析 Objective-C 中的字符串以删除特定 #tagged 单词的完全匹配。我可以创建正则表达式并毫无问题地删除特定单词,但是当我尝试删除带有前导“#”的字符串时,它不起作用。
这是我的代码:
NSString *originalString = @"This is just a #test that isn't working";
NSString *hashTag = @"#test";
NSString *placeholder = @"\\b%@\\b";
NSString *pattern = [NSString stringWithFormat:placeholder, hashTag];
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:nil];
NSString *modifiedString = [regex stringByReplacingMatchesInString:originalString
options:0
range:NSMakeRange(0, [originalString length])
withTemplate:@""];
问题是即使原始字符串包含字符串#test,它也不会被删除。如果我用“test”交换“#test”,一切正常,但这不是我想要做的。我错过了什么?
【问题讨论】:
标签: ios objective-c regex nsstring