问题陈述 :-
我们将 0x8140~0x84BE、0x889F~0x9872、0x989F~0x9FFC、0xE040~0xEAA4、0x8740~0x879C、0xED40~0xEEFC、0xFA40~0xFC4B、0xF040~0xF9FC 称为范围。
我想验证输入字符串是否包含不在上述范围内的汉字。
以下是输出结果不在上述范围内的输入汉字字符示例:-
龚 --> 好的
鑫 --> 好的
璐 --> 需要改变
对于所有这些,预期结果应该是“需要更改”。请帮忙。
这是一个代码: -
import java.io.UnsupportedEncodingException;
import java.util.regex.*;
//import java.util.regex.Pattern;
public class RegExpDemo2 {
private boolean validateMnpName(String name) {
try {
byte[] utf8Bytes = name.getBytes("UTF-8");
String string = new String(utf8Bytes, "UTF-8");
byte[] shiftJisBytes = string.getBytes("Shift-JIS");
String strName = new String(shiftJisBytes, "Shift-JIS");
System.out.println("ShiftJIS Str name : "+strName);
final String regex = "([\\x{8140}-\\x{84BE}]+)|([\\x{889F}-\\x{9872}]+)|([\\x{989F}-\\x{9FFC}]+)|([\\x{E040}-\\x{EAA4}]+)|([\\x{8740}-\\x{879C}]+)|([\\x{ED40}-\\x{EEFC}]+)|([\\x{FA40}-\\x{FC4B}]+)|([\\x{F040}-\\x{F9FC}]+)";
if (Pattern.compile(regex).matcher(strName).find()) {
return true;
} else
return false;
}
catch (Exception e) {
e.printStackTrace();
return false;
}
}
public static void main(String args[]) {
RegExpDemo2 obj = new RegExpDemo2();
if (obj.validateMnpName("ロ")) {
System.out.println("OK");
} else {
System.out.println("Need Change");
}
}
}