如何在Objective-C中正确转义这个正则表达式? [英] How can I escape this regex properly in Objective-C?

查看:124
本文介绍了如何在Objective-C中正确转义这个正则表达式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下正则表达式我想在Objective-C中转义

I have the following regex I would like to escape in Objective-C

/\B\$((?:[0-9]+(?=[a-z])|(?![0-9\.\:\_\-]))(?:[a-z0-9]|[\_\.\-\:](?![\.\_\.\-\:]))*[a-z0-9]+)/ig;

不完全确定如何转义它,所以它的工作原理Objective-C

Not exactly sure how to escape it so it works in Objective-C

更新:

NSString* pattern = @"/\\B\\$((?:[0-9]+(?=[a-z])|(?![0-9\\.\\:\\_\\-]))(?:[a-z0-9]|[\\_\\.\\-\\:](?![\\.\\_\\.\\-\\:]))*[a-z0-9]+)/ig;";
NSRegularExpression *usernameRegex = [[[NSRegularExpression alloc] initWithPattern:pattern
                                                                  options:NSRegularExpressionCaseInsensitive 
                                                                    error:nil];
                                                                        error:nil];

给我一​​个关于的错误Parse Issue - Unexpected Identifier

推荐答案

反斜杠在C字符串中用作转义字符。要将包含反斜杠的正则表达式作为正则表达式转义,您需要将它们加倍。

Backslashes are used as escape characters in C strings. To make a regexp that contains backslashes as regex escapes, you need to double them.

这篇关于如何在Objective-C中正确转义这个正则表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆