3

I read this answer on stackoverflow that explained how a Japanese letter on a string could be detected. I tried searching for something similar to string.match() in the String Class documentation but didn't find anything.

I also tried implementing the Best Answer from this post but it didn't work as well, does anyone have a suggestion on how can this be achieved? the output of the code below is false but it should be true.

String message = 'EnglishLetters - みずずほダイレクト';
String regex = '/[\u3040-\u30ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff\uff66-\uff9f]/';
Pattern regexPattern = Pattern.compile(regex);
Matcher regexMatcher = regexPattern.matcher(message);

String replacedMessage;

if(regexMatcher.find()) {
   system.debug('true');
}
else{
 System.debug('false');   
}

//output: false
3

You don't wrap expressions in forward slashes when using Apex.

String regex = '/[............]/';
//              ^ remove these ^

Working example:

String message = 'EnglishLetters - みずずほダイレクト';
String regex = '[\u3040-\u30ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff\uff66-\uff9f]';
Pattern regexPattern = Pattern.compile(regex);
Matcher regexMatcher = regexPattern.matcher(message);

system.assert(regexMatcher.find());

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.