Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c NSString替换正则表达式_Objective C_Regex - Fatal编程技术网

Objective c NSString替换正则表达式

Objective c NSString替换正则表达式,objective-c,regex,Objective C,Regex,我有这样一个字符串: NSString* msg = @"Hello this is a new message to @[123:Dr Zoidberg] and his nippers"; 我想使用-stringbyreplacingmatchesting: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";
我想使用
-stringbyreplacingmatchesting: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"];
有人能帮我弄一下@?“图案吗?

试着换一下

@\[\d+:(.+?)\]
使用
\1

说明:

匹配
@
后接
[
,然后是任意数字,后跟逗号。从这一点开始获取文本,直到找到结束方括号

如果数字和
之间可能存在任何类型的空格,则可以使用此空格

@\[\d+\t*:(.+?)\]

搜索
@[123:(.*?)\]
并替换为
\1

这是我所追求的模式:
@(.*?):(.*?)
。感谢访问。

模板格式是什么?在
@[123:Zoidberg博士]
中,什么可以取代
123
号码?它在记事本++上工作。它和什么匹配/不匹配?它看起来很像mmdemirbas所评论的,可能问题在于开头的方括号,在
123
之间是否有空格(或任何缩进字符):
?如果你可以这样写
@[\d+\t+?:(.+?)\]
那么
的意思是“不要贪心于+”,在这种情况下,事实上你可以忽略它(我的错误,我会编辑答案)认为我理解:你必须用模板替换
:@“$2”
模板替换:@“$1”
告诉你使用第一个替换组,不是第二个