简体   繁体   English

如何在带有NSRegularExpression的Objective-C中正确进行反向引用?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM