在正则表达式中接受重音 android
accepting acents in regex android
队友...
我在通过正则表达式接受 android 中的重音时遇到问题...我们为 java 尝试的所有事情都无法正常工作,并且 android 不要不想要我们带口音的人声..
我有以下正则表达式:
Pattern pattern = Pattern.compile("[a-zA-ZñÑáéíóúÁÉÍÓÚ]+");
关于如何在 android 中包含 ñ 和重音人声的任何提示?
非常感谢...
这是我们的验证函数:
public static boolean validarNombres(String nameToValidate){
byte step = 1;
byte minWords = 2;
byte maxWords = 5;
boolean validName = false;
String[] aux;
Matcher matcher = null;
Pattern pattern = Pattern.compile("[\p{L}\p{M}]+");
aux = nameToValidate.split(" ");
//PASO 2: check that the name has from 2 to 5 words
if(aux.length >= minWords && aux.length <= maxWords){
step++;
matcher = pattern.matcher(nameToValidate);
}
//PASO 3: check that the name matches out regex
if(step==2 && matcher.matches()){
validName = true;
}
return validName;
}
编辑:认为发现了错误...我们不包括名字和名字之间的空白 space...当我们只检查一个单词而不是全名时,它工作正常... 现在....
在我们的正则表达式中包含空白 space 的代码是什么?请
非常感谢
要验证由 2 到 5 个以空格分隔的单词组成的字符串,您可以使用
public static boolean validarNombres(String nameToValidate) {
return nameToValidate.matches("[\p{L}\p{M}]+(?:\s[\p{L}\p{M}]+){1,4}");
}
正则表达式与.matches()
方法一起使用时默认锚定,无需添加^
和$
。
图案详情:
[\p{L}\p{M}]+
- 1 个或多个字母 or/and 变音符号
(?:\s[\p{L}\p{M}]+){1,4}
- 1 到 4 个(因此,总共 2 到 5 个)序列:
\s
- 一个空格
[\p{L}\p{M}]+
- 1 个或多个字母 or/and 变音符号
参见regex demo。
队友...
我在通过正则表达式接受 android 中的重音时遇到问题...我们为 java 尝试的所有事情都无法正常工作,并且 android 不要不想要我们带口音的人声..
我有以下正则表达式:
Pattern pattern = Pattern.compile("[a-zA-ZñÑáéíóúÁÉÍÓÚ]+");
关于如何在 android 中包含 ñ 和重音人声的任何提示?
非常感谢...
这是我们的验证函数:
public static boolean validarNombres(String nameToValidate){
byte step = 1;
byte minWords = 2;
byte maxWords = 5;
boolean validName = false;
String[] aux;
Matcher matcher = null;
Pattern pattern = Pattern.compile("[\p{L}\p{M}]+");
aux = nameToValidate.split(" ");
//PASO 2: check that the name has from 2 to 5 words
if(aux.length >= minWords && aux.length <= maxWords){
step++;
matcher = pattern.matcher(nameToValidate);
}
//PASO 3: check that the name matches out regex
if(step==2 && matcher.matches()){
validName = true;
}
return validName;
}
编辑:认为发现了错误...我们不包括名字和名字之间的空白 space...当我们只检查一个单词而不是全名时,它工作正常... 现在.... 在我们的正则表达式中包含空白 space 的代码是什么?请
非常感谢
要验证由 2 到 5 个以空格分隔的单词组成的字符串,您可以使用
public static boolean validarNombres(String nameToValidate) {
return nameToValidate.matches("[\p{L}\p{M}]+(?:\s[\p{L}\p{M}]+){1,4}");
}
正则表达式与.matches()
方法一起使用时默认锚定,无需添加^
和$
。
图案详情:
[\p{L}\p{M}]+
- 1 个或多个字母 or/and 变音符号(?:\s[\p{L}\p{M}]+){1,4}
- 1 到 4 个(因此,总共 2 到 5 个)序列:\s
- 一个空格[\p{L}\p{M}]+
- 1 个或多个字母 or/and 变音符号
参见regex demo。