I’m trying to detect the text in a text view whether it contains anything beyond a pattern of hex value \u00 - \u7f or not and then do something. Please take a look at this code:
NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[\x00-\x7f]"
options:NSRegularExpressionCaseInsensitive
error:&error];
NSRange rangeOfFirstMatch = [regex rangeOfFirstMatchInString:textView.text
options:0
range:NSMakeRange(0, [textView.text length])];
if (!NSEqualRanges(rangeOfFirstMatch, NSMakeRange(NSNotFound, 0)))
{
// do statement 1
}
else
{
// do statement 2
}
From above, if the text view contains both text inside and outside [\u00 - \u7f] this will do statement 1 but what I want is do statement 2.
In my opinion, it should have the regular expression opposite to this pattern but I don’t know what it is. Any suggestions are welcome, thank you.
A carat (‘^’) negates a character class, so
[^\u00-\u7f]will match any character except those in the range ‘\u00’ through ‘\u7f’.You could also use
rangeOfCharacterFromSet:orcanBeConvertedToEncoding:to check whether a string has any non-ASCII characters.rangeOfCharacterFromSet:canBeConvertedToEncoding: