flexinIT
flexinIT

Reputation: 431

Regex: PatternSyntaxException: Illegal repetition

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

Upvotes: 1

Views: 9459

Answers (2)

BlackVegetable
BlackVegetable

Reputation: 13054

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.

Upvotes: 0

FThompson
FThompson

Reputation: 28687

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.

Upvotes: 10

Related Questions