Regular Expression: check if a string contains any given characters
I开发者_运维技巧 need to check if a string contains any of this characters:
Á,À,Ã,É,Ê,Í,Ó,Õ,Ô,Ú,Ç
I was thinking of doing a
"blá".contains(regexExpression)
Am I thinking right? If so, how can I do it? I don't know how will be the regular Expression
Take a look at regular-expressions.info. There you find a good reference on how you can achieve certain things using a regex.
Note that matches(regex)
will only return true, if the whole string matches the regex. If you just want to know if one of the specified characters is in the String, use this:
String input = "blá";
input.toUpperCase().matches(".*[ÁÀÃÉÊÍÓÕÔÚÇ].*");
Edit: if you need to match more unicode characters, have a look at the regular-expressions.info unicode reference.
Pattern regex = Pattern.compile("[ÁÀÃÉÊÍÓÕÔÚÇ]");
Matcher regexMatcher = regex.matcher(subjectString.toUpperCase());
if (regexMatcher.find()) {
// Successful match
} else {
// Match attempt failed
}
I my experience, better don't use a character, but use a hex representation.
for example:
'Á' - 0x00C1
'á' - 0x00E1
hex code for an any symbol you can find here http://www.fileformat.info/info/unicode. Just put letter to search field.
Your regex will be:
[\x{00c1}\x{00e1}]++
This will work in PHP. In Java will be \u00c1\u00e1, if sure to www.regular-expressions.info
Also you can use range:
ÀÁÂÃÄÅÆ will be [\u00c0-\u00c6]++
Latin Supplement
If you need to find an any symbol from a Latin-1 Supplement range, you can use the following re:
[\p{InLatin-1_Supplement}]++
精彩评论