regular expressions – Regex too complicated error – replaceFirst() in matcher

        List<String> lcLetter = new List<String>{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k','l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
        Map<String, Integer> index = new Map<String, Integer>();
        String input="{~TEST TYPE=1}<br> {~TEST Rank=1}<br>";           
        Pattern p1 = Pattern.compile('\{\~TEST[\s\w\=]*\}');
        Matcher m1 = p1.matcher(input);
        String level;
   
        if(input != null){
            while(m1.find()){
                level="";

                if(String.isBlank(level)){
                    level="0";
                }
                if(index.get(level) == null){
                    index.put(level, 0);
                }
                m1.replaceFirst(lcLetter.get(index.get(level)));
                // . . .
                }
            }

“Regex too complicated” error located in replaceFirst(). The goal is to replace the first {~Test something} with the letter ‘a’ – once every loop so that in the end the input is 'a<br> a<br>'.

I tried m1.replaceFirst('\{\~TEST[\s\w\=]*\}', lcLetter.get(index.get(level))); and I got an error:

Method does not exist or incorrect signature: void replaceFirst(String, String) from the type System.Matcher

Looks like the problem is related to the while loop (m1.find())

Read more here: Source link