![](/img/trans.png)
[英]How do I escape special characters in an NSString using RegexKitLite?
[英]How do I match non-ASCII characters with RegexKitLite?
我正在使用RegexKitLite,并且正在尝试匹配模式。
以下正则表达式模式未捕获包含连词号N的单词:ñ。 我缺少字符串转换吗?
subjectString = @"define_añadir";
//regexString = @"^define_(.*)"; //this pattern does not match, so I assume to add the ñ
//regexString = @"^define_([.ñ]*)"; //tried this pattern first with a range
regexString = @"^define_((?:\\w|ñ)*)"; //tried second
NSString *captured= [subjectString stringByMatching:regexString capture:1L];
//I want captured == añadir
对我来说似乎是编码问题。 您正在将源代码保存为无法处理该字符的编码(例如ASCII),或者编译器使用了错误的编码来读取源文件。 回到原始正则表达式,尝试创建如下主题字符串:
subjectString = @"define_a\xC3\xB1adir";
或这个:
subjectString = @"define_a\u00F1adir";
如果可行,请检查源代码文件的编码,并确保其与编译器期望的编码相同。
编辑:我从未使用过iPhone技术堆栈,但是根据此文档,您应该使用stringWithUTF8String
方法创建NSString,而不是@""
文字语法。 实际上,它表示您不应在代码中使用非ASCII字符(即,不在0x00..0x7F
范围内的任何0x00..0x7F
); 这样,您就不必担心源文件的编码。 无论您使用哪种语言或工具集,这都是一个很好的建议。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.