I try get numbers in string with space blank and characteres. For it, I try delete space blank in string with function replaceAll, but regular expression not working.
My string is:
String prueba = "JPI 101 BIS";
I try next:
String numero = prueba.replaceAll("^[a-zA-Z\\s]*", "");
However, the result is:
101 BIS
I need delete all space blanks and all characteres and get only numbers.
¿Help me please?
>Solution :
The symbol ^ matches the beginning of the string, so the regex matches only at the start. Remove it, and your regex will remove all non numbers, regardless of their position in the string.
String prueba = "JPI 101 BIS";
String numero = prueba.replaceAll("[a-zA-Z\\s]*", "");
System.out.println(numero);
Prints 101.