Regular expression that accepts only numbers and / or letters in Java

3

How to develop a regular expression that allows a string to have only numbers and / or letters in any position and quantities?

Examples:

  

a) 000000000a

     

b) 000000000A

     

c) AAAAAAAAA0

     

d) 1AAAAA1113

     

e) 1111111111111111111111111a

     

f) aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa4

     

g) 1234567678909090


EDITED

The expression must have numbers and optionally letters without graphic accentuation (cedilla does not enter, for example). My goal is to create a method to evaluate whether a given string matched the pattern, that is, the return is true or false.

    
asked by anonymous 16.07.2014 / 23:05

3 answers

3

Requiring at least one integer:

Pattern p = Pattern.compile("^[A-Za-z0-9]*\d+[A-Za-z0-9]*$"); // ou ^[^\W_]*\d+[^\W_]*$ seguindo a ideia do mgibsonbr
return p.matcher(textoDeTeste).matches();

You can test it on link , but remember to flag the multline flag there in the upper right corner if you want to test test with multiple different lines at the same time (each line being an entry).

Rubular link: link

    
16.07.2014 / 23:13
4

Normally, the default \w (word) matches any letter, number, and underscore ( _ ). That way, \w* would match a string with any number of characters of that type (including the empty string), and \w+ the same thing, but would require at least one character.

This would be the most complete solution because defining letters in a range (eg, [a-zA-Z] ) would only consider ASCII characters, without accepting accented letters ( á ). To make Java agree to match \w with Unicode letters, just prefix the default with (?U) [source] . If you are not interested in Unicode (i.e. just want ASCII letters yourself), just omit this prefix (or use the alternative solutions given in the other answers - which would also be correct in that case).

If the presence of the underscore is a problem, we can eliminate it through a "double negation":

[^\W_]

That is: "Case everything that is not a 'no word' nor an underscore." Rubular example . (Note: if it is not clear, \w - in lowercase - sets the character class "word"; \W - in uppercase - reverses, matching everything that is not of that character class; [...] home a of a set of characters; [^...] reverses, marrying anything that is not one of these characters)

To use it, the simplest method is String.matches " (checks whether the integer string matches the expression passed as a parameter), or if using Pattern and Matcher for other behaviors:

"abc".matches("(?U)[^\W_]*"); // true

Pattern p = Pattern.compile("(?U)[^\W_]*");

p.matcher("abc").matches();   // ok, a string inteira casa com o padrão
p.matcher("$a$").find();      // ok, o padrão pode ser achado na string
p.matcher("ab$").lookingAt(); // ok, a string começa com o padrão
    
16.07.2014 / 23:41
2
public class Regex {

    public static boolean validaString(String str) {
        return str.matches("[a-zA-Z0-9]+");
    }

    public static void main(String[] args) {
        String[] teste = {"asdfads89as89", "", "asdf 98s", "asd©áßsas90", 
                          "asdfas78237", "2342", "abc",};
        //tudo o que não for de a->z ou de A->Z ou de 0->9 será removido
        for(String s: teste){
            String resultado = (validaString(s))?"\' é válida" :"\' é inválida";
            System.out.println("A String \'" + s + resultado);
        }
    }
}

Output:

A String 'asdfads89as89' é válida
A String '' é inválida
A String 'asdf 98s' é inválida
A String 'asd©áßsas90' é inválida
A String 'asdfas78237' é válida
A String '2342' é válida
A String 'abc' é válida
    
16.07.2014 / 23:10