accepting acents in regex android -
mates...
i having troubles pass through regex accepting accents in android... things have try java not working properly, , android don't want our accented vocals ..
i have following regex:
pattern pattern = pattern.compile("[a-za-zñÑáéíóúÁÉÍÓÚ]+");
any tip of how include ñ , accents vocals in android?
thanks in advance...
here our validation function:
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 name has 2 5 words if(aux.length >= minwords && aux.length <= maxwords){ step++; matcher = pattern.matcher(nametovalidate); } //paso 3: check name matches out regex if(step==2 && matcher.matches()){ validname = true; } return validname; }
edit: think found mistake... not including blank space bettwen first , second name ... works fine when check word, not full name... now.... whats code include blank space on our regex?, please
thanks
to validate string consisting of 2 5 words separated whitespace(s), may use
public static boolean validarnombres(string nametovalidate) { return nametovalidate.matches("[\\p{l}\\p{m}]+(?:\\s[\\p{l}\\p{m}]+){1,4}"); }
the regex anchored default when used .matches()
method, no need adding ^
, $
.
pattern details:
[\\p{l}\\p{m}]+
- 1 or more letters or/and diacritics(?:\\s[\\p{l}\\p{m}]+){1,4}
- 1 4 (so, 2 5 in total) sequences of:\\s
- single whitespace[\\p{l}\\p{m}]+
- 1 or more letters or/and diacritics
see regex demo.
Comments
Post a Comment