Find a character that is not a space or a letter, including accents and diacritics of Spanish

3

I want to include the space and only accept letters, numbers and accents and the special character > ñ . What I have done is the following:

if (primernombre.length() == 0 || a.matches(".*[^a-zñáéíóúA-ZÑÁÉÍÓÚ].*")) {
        primernombre.setError("Ingrese un nombre valido. Solo letras.");

What I need is to include the space and the numbers.

For example: this is a text so far it does not accept it, and my regular expression accepts only thisis a text

    
asked by Ashley G. 04.01.2017 в 16:12
source

1 answer

5

Have you tried adding space in your set of characters?

Your expression would look something like this

a.matches("^[a-zñáéíóúüA-ZÑÁÉÍÓÚÜ0-9 ]$")

Edited

You can also try this expression:

^\w+( \w+)*$

This expression will allow a series of at least one word and the words will be divided by spaces.

^ Mark the beginning of the string

\w+ Indicates a series of at least one word of a character or number

( \w+)* indicates a group that repeats 0 or more times. In the group a space is expected followed by a series of at least one character or number.

$ indicates the end of the string

This can also be translated by the following expression:

^[a-zA-Z0-9ñáéíóúüÜÑÁÉÍÓÚ]+( [a-zA-Z0-9ñáéíóúüÜÑÁÉÍÓÚ]+)*$
    
answered by 04.01.2017 / 16:18
source