I am using RegexKitLite and I’m trying to match a pattern.
The following regex patterns do not capture my word that includes N with a titlde: ñ.
Is there a string conversion I am missing?
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
Looks like an encoding problem to me. Either you’re saving the source code in an encoding that can’t handle that character (like ASCII), or the compiler is using the wrong encoding to read the source files. Going back to the original regex, try creating the subject string like this:
or this:
If that works, check the encoding of your source code files and make sure it’s the same encoding the compiler expects.
EDIT: I’ve never worked with the iPhone technology stack, but according to this doc you should be using the
stringWithUTF8Stringmethod to create the NSString, not the@""literal syntax. In fact, it says you should never use non-ASCII characters (that is, anything not in the range0x00..0x7F) in your code; that way you never have to worry about the source file’s encoding. That’s good advice no matter what language or toolset you’re using.