objective-c - NSString 替换正则表达式

标签 objective-c regex

我有这样一个字符串:

NSString* msg = @"Hello this is a new message to @[123:Dr Zoidberg] and his nippers";

我想使用 -stringByReplacingMatchesInString:options:range:withTemplate: 将此模式转换为:

NSString* msg = @"Hello this is a new message to Dr Zoidberg and his nippers"; 

这是我目前所拥有的:

NSString* msg = @"Hello this is a new message to @[123:Dr Zoidberg] and his nippers";
NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern: @"????"
                                                                       options: NSRegularExpressionCaseInsensitive
                                                                         error: nil];

NSString* plainText = [regex stringByReplacingMatchesInString: msg
                                                      options: 0
                                                        range: NSMakeRange(0, [msg length])
                                                 withTemplate: @"$2"];

谁能帮我解决@"????"模式?

最佳答案

这就是我所追求的模式:@(.*?):(.*?)]。感谢转至 this question .

关于objective-c - NSString 替换正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12143182/

相关文章:

ios - 为什么 Obj-C 属性默认所有权为 "assign"而不是 "strong"

objective-c - 在 OSX 10.6+ 中识别当前事件应用程序的 'correct' 方法是什么?

ios - 关闭 subview 时如何设置计时器

regex - Perl 正则表达式对带或不带引号的项目进行分组并忽略空格

python - 提取 2 个特定标签之间的行

regex - 在 Emacs 中查找和替换整个单词(不是子字符串)

c# - 允许特殊字符的正则表达式?

ios - 如何阻止代码等待连接

ios - UILabel 无法像在 Interface Builder 中那样在 Emulator 和真实设备中正确显示文本

javascript - 如何在 JavaScript 中编写包含括号的正则表达式?