【发布时间】:2013-12-06 12:13:07
【问题描述】:
我有以下字符串:
NSString *string = @"she seemed \x3cem\x3ereluctant\x3c/em\x3e to discuss the matter";
我希望最终的字符串是:"she seemed reluctant to discuss the matter"
我有以下模式:
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"/\\x[0-9a-f]{2}/"
options:NSRegularExpressionCaseInsensitive
error:&error];
NSArray *matches = [regex matchesInString:string options:0 range:NSMakeRange(0, [string length])];
for (NSTextCheckingResult *match in matches) {
NSRange matchRange = [match range];
NSLog(@"%@", NSStringFromRange(matchRange));
}
但是,我收到一条错误消息,提示该模式无效。我做错了什么?
【问题讨论】:
-
相当肯定 Objective C 不需要 // 分隔符,试试不带?
-
@r3mus 你的意思是像
\\x[0-9a-f]{2}?还是不行:NSInvalidValue=\x[0-9a-f]{2}} -
即使
\\x也不起作用:NSInvalidValue=\x -
诚然不是在计算机上进行测试 - 但我认为 \x 是为 Unicode 保留的。也尝试转义
\\\x。
标签: objective-c regex nsregularexpression