regex - reged allow normal and accent letters in Spanish but block special characters and numbers -
hi need validation , make sure string not contain special character or numbers. using spanish language therefore need accent letters , normal letters allowed spaces well.
string text = "áa @s"; text = text.tolowercase(); if(text.matches(".*\\p{l}.*") && !text.matches(".*\\d+.*")) system.out.println("si");
the output here print "si" should not since includes "@" code exclude numbers , allow accent spanish letters want block special characters , exclude them well. appreciated. note : flexible method long achieve goal of having clean spanish string letters (including accent letters) , spaces. if there away in 1 regex appreciated.
\p{l}
matches unicode letter (as opposed symbol) , way go.
however, discovered didn't (always) matched accented letters. that's because in unicode, accented letters can represented 2 characters : letter without accent, followed combining mark represents accent.
class match these 1 \p{m}
, gives following code (considering comment said wanted include spaces) :
if(text.matches("(?:\\p{l}\\p{m}*\\s*)+")) { system.out.println("muy bien !"); }
more info here.
Comments
Post a Comment