我不擅长正则表达式,我正在尝试将java.lang.String
replaceAll()
方法与代码段一起使用,如下所示
一个是:
public class method3 {
public static int addTwoNumbers(int one, int two){
return one+two;
}
public static void main (String[] args){
int total = addTwoNumbers(1, 3);
System.out.println(total);
}
}
替换为 B
null
public class method3 {
/* some writting */
public static int addTwoNumbers(int one, int two){
return one+two;
}
/*more text*/
public static void main (String[] args){
int total = addTwoNumbers(1, 3);
System.out.println(total);
}//end of
}
这给了我以下错误
java.util.regex.PatternSyntaxException: Illegal repetition
我猜这和/**/
角色有关?
您如何对此进行排序,还有其他我需要注意的角色吗?
编辑:这是错误信息
Exception in thread "AWT-EventQueue-0" java.util.regex.PatternSyntaxException: Illegal repetition near index 25
null
public class method2 {
public static int addTwoNumbers(int one, int two){
return one+two;
}
public static void main (String[] args){
int total = addTwoNumbers(1, 3);
System.out.println(total);
}
}
^
at java.util.regex.Pattern.error(Pattern.java:1924)
at java.util.regex.Pattern.closure(Pattern.java:3104)
at java.util.regex.Pattern.sequence(Pattern.java:2101)
at java.util.regex.Pattern.expr(Pattern.java:1964)
at java.util.regex.Pattern.compile(Pattern.java:1665)
at java.util.regex.Pattern.<init>(Pattern.java:1337)
....
调用 replaceAll()
String a = readFile(directoryToAddFile,startOfCode, endOfCode);
String b = textarea.getText().toString();
String content = IOUtils.toString(new FileInputStream(directoryToAddFile));
content = content.replaceAll(a, b);
IOUtils.write(content, new FileOutputStream(directoryToAddFile));