【问题标题】:String matching and replace in JavaJava中的字符串匹配和替换
【发布时间】:2014-08-14 13:21:37
【问题描述】:

我有一个这样的字符串:

String a = "Barbara Liskov (born Barbara Jane Huberman on November 7, 1939"
+" in California) is a computer scientist.[2] She is currently the Ford"
+" Professor of Engineering in the MIT School of Engineering's electrical"
+" engineering and computer science department and an institute professor"
+" at the Massachusetts Institute of Technology.[3]";

我想用空格替换所有这些元素:[1][2][3] 等等。

我试过了:

if (a.matches("([){1}\\d(]){1}")) {
    a = a.replace("");
}

但它不起作用!

【问题讨论】:

    标签: java string match string-matching


    【解决方案1】:

    您的Pattern 完全错误。

    试试这个例子:

    String input = 
          "Barbara Liskov (born Barbara Jane Huberman on November 7, 1939 in California) "
        + "is a computer scientist.[2] She is currently the Ford Professor of Engineering "
        + "in the MIT School of Engineering's electrical engineering and computer "
        + "science department and an institute professor at the Massachusetts Institute " 
        + "of Technology.[3]";
    //                                   | escaped opening square bracket
    //                                   |  | any digit
    //                                   |  |   | escaped closing square bracket
    //                                   |  |   |      | replace with one space
    System.out.println(input.replaceAll("\\[\\d+\\]", " "));
    

    输出(为清楚起见添加了换行符)

    Barbara Liskov (born Barbara Jane Huberman on November 7, 
    1939 in California) is a computer scientist. 
    She is currently the Ford Professor of Engineering in the MIT 
    School of Engineering's electrical engineering and computer science 
    department and an institute professor at the Massachusetts Institute of Technology.
    

    【讨论】:

      【解决方案2】:

      关于你的模式([){1}\\d(]){1}

      • {1} 总是无用的,因为总是隐含的
      • [] 需要用反斜杠转义(因为在字符串文字中,它本身必须用另一个反斜杠转义)
      • \\d 没有明确的基数,因此 [12] 不匹配,因为有两位数

      所以,最好试试:\\[\\d+\\]

      【讨论】:

        【解决方案3】:

        使用String replaceAll(String regex, String replacement)

        您只需a=a.replaceAll("\\[\\d+\\]", " ")

        您可以阅读Javadoc了解更多信息。

        【讨论】:

          【解决方案4】:

          很简单:

             a = a.replaceAll("\\[\\d+\\]","");
          

          变化:

          1. 使用replaceAll 而不是replace

          2. 转义 [] - 它们是正则表达式特殊字符。伙伴关系并没有逃脱它们。

          3. 您的正则表达式中不需要{1} [{1} == [ - 两者都指定该字符应该是一次

          4. 添加到d++ 用于多于一位的数字,例如[12]

          【讨论】:

            【解决方案5】:

            使用这个:

            String a = "Barbara Liskov (born Barbara Jane Huberman on November 7, 1939 in California) is a computer scientist.[2] She is currently the Ford Professor of Engineering in the MIT School of Engineering's electrical engineering and computer science department and an institute professor at the Massachusetts Institute of Technology.[3]";
            
                    for(int i =1 ; i<= 3; i++){
                           a= a.replace("["+i+"]","");
                    }
                    System.out.println(a);
            

            这会起作用。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2016-08-27
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多