Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Get numbers in string with space blank and characters in java

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading