1

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));
14
  • the codes snippets are the same arent they? Commented Sep 4, 2012 at 17:38
  • 1
    It would help if you'd show the code which is failing. Yes, * has a special meaning in regular expressions... but it may be that regexes aren't the best approach here anyway. Commented Sep 4, 2012 at 17:38
  • 1
    Where is the call to replaceAll ? Commented Sep 4, 2012 at 17:39
  • where are you using patterns at all? I only see System.out.println and one+two. Commented Sep 4, 2012 at 17:41
  • the code is the same i am trying to replace just that it has some omments in it... this is just a simple example Commented Sep 4, 2012 at 17:42

2 Answers 2

10

Currently, your code attempts to analyze the "to replace" string as a regex.

To replace a string literally rather than as a regex, you can use Pattern#quote(String).

content = content.replaceAll(Pattern.quote(a), b);

Also, as a side note, you might find String#replace(CharSequence, CharSequence) more appropriate than replaceAll in this situation.

Sign up to request clarification or add additional context in comments.

3 Comments

I have tried both your suggestions about yet none of them work(well not working), I am not getting the error no more but it is not replacing any of the text for the content
In that case, the replace methods simply aren't finding your string in the other string. That's a different question altogether though, so if you wish to ask it, create a new question rather than editing this one. However, that's an issue easily solvable through some simple debugging.
yes, there was white space at the end of one of the strings so after i .trim() on the string it worked, cheers
0

Your issue is likely stemming from not escaping several of the characters in what you are trying to replace, as well as what you are trying to replace it with. Regex may not be the tool you want to use here. From www.regular-expressions.info, the characters to be aware of are []\^$.|?*+()

Read up on that website to see the uses/limitations of regex.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.