[英]How do I do backreferences correctly in Objective-C with NSRegularExpression?
In PHP I'd do something like this: 在PHP中,我将执行以下操作:
But in Objective-C, I tried this: 但是在Objective-C中,我尝试了以下方法:
regex = [NSRegularExpression regularExpressionWithPattern:@"\\.([a-zA-Z0-9])" options:NSRegularExpressionCaseInsensitive error:&error];
result = [regex stringByReplacingMatchesInString:result options:0 range:NSMakeRange(0, [result length]) withTemplate:@". \1"];
But it ends up simply removing the first letter of the next sentence (such as "end. Chris" -> "end. hris"). 但这最终只是删除下一个句子的第一个字母(例如“ end。Chris”->“ end。hris”)。 Why is this?
为什么是这样?
Use $1
, $2
, etc. instead of \\1
, \\2
, etc. for back references. 使用
$1
, $2
等代替\\1
, \\2
等进行反向引用。
See the docs for NSRegularExpression
. 请参阅有关
NSRegularExpression
的文档。 Look under the "Template Matching Format" section. 在“模板匹配格式”部分下查看。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.