I am not good at regex and I am trying to use java.lang.String replaceAll() method with code segments as follows
A is:
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);
}
}
to replace with 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
}
And this is giving me the following error
java.util.regex.PatternSyntaxException: Illegal repetition
Im guessing this has something to do with the /**/ characters?
How do you sort this and is there any other characters i need to look out for?
EDIT: This is the error message
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)
....
call to 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));
*has a special meaning in regular expressions... but it may be that regexes aren't the best approach here anyway.System.out.printlnandone+two.