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”
告诉你使用第一个替换组,不是第二个