简体   繁体   English

Java中的字符串匹配和替换

[英]String matching and replace in Java

I have a String like this: 我有一个像这样的字符串:

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]";

I would like to replace all of these elements: [1] , [2] , [3] , etcetera, with a blank space. 我想用空格替换所有这些元素: [1][2][3]等。

I tried with: 我尝试过:

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

but it does not work! 但它不起作用!

Your Pattern is all wrong. 你的Pattern都错了。

Try this example: 试试这个例子:

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+\\]", " "));

Output (newlines added for clarity) 输出(为清晰起见添加了换行符)

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.

Very simple: 很简单:

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

The changes: 变化:

  1. Use replaceAll instead of replace 使用replaceAll而不是replace

  2. Escape the [] - they are regex special chars. 逃避[] - 他们是正则表达式特殊字符。 the partnerships are not escaping them. 伙伴关系并没有逃避他们。

  3. No need of {1} on your regex [{1} == [ - both are specifying that the character should be one time 你的正则表达式[{1} ==不需要{1} [ - 两者都指定该字符应该是一次

  4. The + added to d+ is for more than one digits numbers such as [12] +添加到d+是用于多于一个的位数的数字,如[12]

About your pattern ([){1}\\\\d(]){1} : 关于您的模式([){1}\\\\d(]){1}

  • {1} is always useless since always implicit {1}始终是无用的,因为总是隐含的
  • [ and ] needs to be escaped with a backslash (which must itself be escaped with another backslash since in a string literal) []需要使用反斜杠进行转义(因为在字符串文字中,它本身必须使用另一个反斜杠进行转义)
  • \\\\d has no explicit cardinality, so [12] for example won't match since there are two digits \\\\d没有明确的基数,所以[12]例如不匹配,因为有两位数

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

Use the String replaceAll(String regex, String replacement) . 使用String replaceAll(String regex, String replacement)

All you got to do is a=a.replaceAll("\\\\[\\\\d+\\\\]", " ") . 你所要做的就是a=a.replaceAll("\\\\[\\\\d+\\\\]", " ")

You can read Javadoc for more information . 您可以阅读Javadoc以获取更多信息。

Use this: 用这个:

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);

This will work. 这会奏效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM