如何替换java字符串中的所有数字

sky*_*ine 2 java regex

我有像这样的字符串s="ram123",d="ram varma656887" 我希望字符串像ram和ram varma所以如何从组合字符串中分离字符串我正在尝试使用正则表达式但它不工作

PersonName.setText(cursor.getString(cursor.getColumnIndex(cursor
                .getColumnName(1))).replaceAll("[^0-9]+"));
Run Code Online (Sandbox Code Playgroud)

ani*_*udh 10

您可以使用以下正则表达式:\d表示数字.在您使用的正则表达式中,您^将检查除charset之外的任何字符0-9

    String s="ram123";
    System.out.println(s);
    /* You don't need the + because you are using the replaceAll method */
    s = s.replaceAll("\\d", "");  // or you can also use [0-9]
    System.out.println(s);
Run Code Online (Sandbox Code Playgroud)


Lea*_*ros 7

选择所有数字的正确RegEx将是正确的[0-9],您可以跳过+,因为您使用replaceAll.

但是,你的使用replaceAll是错误的,它的定义如下:replaceAll(String regex, String replacement).您的示例中的正确代码将是:replaceAll("[0-9]", "").