Gondim
Gondim

Reputation: 3048

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

Upvotes: 0

Views: 3696

Answers (3)

Dmitrij Golubev
Dmitrij Golubev

Reputation: 694

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}]++

Upvotes: 1

Thomas
Thomas

Reputation: 88707

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.

Upvotes: 3

Tim Pietzcker
Tim Pietzcker

Reputation: 336158

Pattern regex = Pattern.compile("[ÁÀÃÉÊÍÓÕÔÚÇ]");
Matcher regexMatcher = regex.matcher(subjectString.toUpperCase());
if (regexMatcher.find()) {
    // Successful match
} else {
    // Match attempt failed
} 

Upvotes: 3

Related Questions