Regex to remove everything but numbers

This example will show how write a regular expression to remove everything but numbers from a java String. This is particular helpful if you want to change a string to a number and avoid NumberFormatException.

Remove all chars

@Test
public void remove_chars_string() {

    String digits = "1a2b3c".replaceAll("[^\\d]", "");
    
    assertEquals(123, Integer.parseInt(digits));
}